Page 1 of 1

Using Python for Scripting

Posted: Wed Apr 26, 2017 2:40 am
by jgelinas3838
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.

Re: Using Python for Scripting

Posted: Wed Apr 26, 2017 4:57 am
by TiTex
did you try with
client-connect 'C:\\path\\to\\python.exe C:\\openvpn\\scripts\\testparse.py'
?

Re: Using Python for Scripting

Posted: Wed Apr 26, 2017 3:13 pm
by jgelinas3838
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.

Re: Using Python for Scripting

Posted: Wed Apr 26, 2017 3:22 pm
by jgelinas3838
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?

Re: Using Python for Scripting

Posted: Wed Apr 26, 2017 10:27 pm
by jgelinas3838
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.

Re: Using Python for Scripting

Posted: Wed Apr 26, 2017 11:10 pm
by jgelinas3838
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):

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)

Re: Using Python for Scripting

Posted: Thu Apr 27, 2017 7:59 am
by TiTex
how can i save the temp file ? ( sys.argv[1] )

i tried with

Code: Select all

from shutil import copyfile
...

    copyfile(TempFile, '/etc/openvpn/tmp.txt')
the tmp.txt file is created but empty

Re: Using Python for Scripting

Posted: Tue Mar 26, 2019 9:45 am
by pruebasunodostres123
Hi,

I trying a connect with python to my openvpn server.

This script functions?

Please, can you help me?

Regards,

Re: Using Python for Scripting

Posted: Wed Mar 27, 2019 3:57 am
by SkyChaser
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