I have a python script which activates vpn connection if it is not already activated. I need to keep it running 24/7 and to achieve this goal i use NSSM.
from time import sleep
from requests import get
def ip_validator(country_name):
ip_restriction = True
if not ip_restriction:
return True
request = get('https://api.myip.com')
response = request.json()
result = True if response['country'] == country_name else False
if result:
return result
else:
print("ip is not valid")
vpn_path = 'path/to/vpn'
command = f'{vpn_path} --command connect tr57.nordvpn.com.tcp' # activates vpn connection
exec_result = os.popen(command).read().strip()
print(exec_result)
sleep(10)
ip_validator('Germany')
When i run this script in cmd directly it work just fine but when i user nsssm it can't get the vpn activated.
this is the nssm log file:
C:\Users\mycomputer\AppData\Local\Programs\Python\Python310\python.exe
C:\Users\mycomputer\Desktop\a.py
ip is not valid
C:\Users\mycomputer\AppData\Local\Programs\Python\Python310\python.exe
C:\Users\mycomputer\Desktop\a.py
ip is not valid
C:\Users\mycomputer\AppData\Local\Programs\Python\Python310\python.exe
C:\Users\mycomputer\Desktop\a.py
ip is not valid
C:\Users\mycomputer\AppData\Local\Programs\Python\Python310\python.exe
C:\Users\mycomputer\Desktop\a.py
ip is not valid
I also tried it with virtual environment and .bat file but the result was the same.