Using Python for Scripting
Moderators: TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech
-
- OpenVpn Newbie
- Posts: 19
- Joined: Sun Mar 12, 2017 3:29 pm
Using Python for Scripting
This might seem naive of me, but I'm expecting that OpenVPN can use a python script (py file) for something like client-connect. Is this not correct?
In my server.config file I have:
script-security 2
client-connect c:\\openvpn\\scripts\\testparse.py
yet the python file does not seem to get hit.
In my server.config file I have:
script-security 2
client-connect c:\\openvpn\\scripts\\testparse.py
yet the python file does not seem to get hit.
-
- OpenVPN Super User
- Posts: 310
- Joined: Tue Apr 12, 2011 6:22 am
Re: Using Python for Scripting
did you try with
client-connect 'C:\\path\\to\\python.exe C:\\openvpn\\scripts\\testparse.py'
?
client-connect 'C:\\path\\to\\python.exe C:\\openvpn\\scripts\\testparse.py'
?
-
- OpenVpn Newbie
- Posts: 19
- Joined: Sun Mar 12, 2017 3:29 pm
Re: Using Python for Scripting
tried with
script-security 2
client-connect 'c:\\python27\\python.exe c:\\openvpn\\scripts\\testparse.py'
Still nothing. It's a very simple script just to test the theory.
script-security 2
client-connect 'c:\\python27\\python.exe c:\\openvpn\\scripts\\testparse.py'
Still nothing. It's a very simple script just to test the theory.
-
- OpenVpn Newbie
- Posts: 19
- Joined: Sun Mar 12, 2017 3:29 pm
Re: Using Python for Scripting
OK, I simplified the script even more and that did work. Next question is, how do I access the arguments that are incoming from the client-connect command?
-
- OpenVpn Newbie
- Posts: 19
- Joined: Sun Mar 12, 2017 3:29 pm
Re: Using Python for Scripting
OK, i've finally figured out how to at least get the environment variables that are being passed. Still can't seem to get the command line argument filename that is passed though.
-
- OpenVpn Newbie
- Posts: 19
- Joined: Sun Mar 12, 2017 3:29 pm
Re: Using Python for Scripting
This was definitely trial and error - lots of error so i'm putting this in for the next bastard who knows how to program but knows nothing about python.
I've set script security to 3 although that might be overkill.
On the server config I have:
[oconf=]script-security 3
client-connect 'c:\\python27\\python.exe c:\\openvpn\\scripts\\clientconnect.py'[/oconf]
The script is as follows (remember, this is expermintation):
I've set script security to 3 although that might be overkill.
On the server config I have:
[oconf=]script-security 3
client-connect 'c:\\python27\\python.exe c:\\openvpn\\scripts\\clientconnect.py'[/oconf]
The script is as follows (remember, this is expermintation):
Code: Select all
import os
import sys
TempFile = 'None found'
CommonName = 'None found'
TrustedIP = 'None found'
TimeConnected = 'None found'
NumberOfArgsPassedIn = 0
TempFile = 'None Found'
WhatToWriteToFile = ''
try:
NumberOfArgsPassedIn = len(sys.argv)
TempFile = sys.argv[1] # this will be the temp file that the documentation talks about
CommonName = os.getenv('common_name','0') #environment variable set
TrustedIP = os.getenv('trusted_ip','0') #environment variable set
TimeConnected = os.getenv('time_ascii','0') #environment variable set
WhatToWriteToFile = 'New Connection' + '\r\n' + \
'Number of Args Passed In: ' + str(NumberOfArgsPassedIn) + '\r\n' + \
'Temp File: ' + TempFile + '\r\n' + \
'CommonName: ' + CommonName + '\r\n' + \
'Trusted IP of Client: ' + TrustedIP + '\r\n' + \
'Time Connected: ' + TimeConnected
except Exception1, Argument:
WhatToWriteToFile = Argument
finally:
save_file_path = "c:\OpenVPN\ConnectionLog.txt"
testfile = open(save_file_path,"a")
testfile.write(WhatToWriteToFile)
testfile.close()
sys.exit(0)
-
- OpenVPN Super User
- Posts: 310
- Joined: Tue Apr 12, 2011 6:22 am
Re: Using Python for Scripting
how can i save the temp file ? ( sys.argv[1] )
i tried with
the tmp.txt file is created but empty
i tried with
Code: Select all
from shutil import copyfile
...
copyfile(TempFile, '/etc/openvpn/tmp.txt')
-
- OpenVpn Newbie
- Posts: 1
- Joined: Tue Mar 26, 2019 9:42 am
Re: Using Python for Scripting
Hi,
I trying a connect with python to my openvpn server.
This script functions?
Please, can you help me?
Regards,
I trying a connect with python to my openvpn server.
This script functions?
Please, can you help me?
Regards,
-
- OpenVpn Newbie
- Posts: 4
- Joined: Sat Mar 23, 2019 6:14 pm
Re: Using Python for Scripting
sorry for the hijack, but could using python script help achive what would like to do in this thread?
viewtopic.php?f=15&t=28102
viewtopic.php?f=15&t=28102