Using Python for Scripting

How to customize and extend your OpenVPN installation.

Moderators: TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech

Post Reply
jgelinas3838
OpenVpn Newbie
Posts: 19
Joined: Sun Mar 12, 2017 3:29 pm

Using Python for Scripting

Post by jgelinas3838 » Wed Apr 26, 2017 2:40 am

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.

TiTex
OpenVPN Super User
Posts: 310
Joined: Tue Apr 12, 2011 6:22 am

Re: Using Python for Scripting

Post by TiTex » Wed Apr 26, 2017 4:57 am

did you try with
client-connect 'C:\\path\\to\\python.exe C:\\openvpn\\scripts\\testparse.py'
?

jgelinas3838
OpenVpn Newbie
Posts: 19
Joined: Sun Mar 12, 2017 3:29 pm

Re: Using Python for Scripting

Post by jgelinas3838 » Wed Apr 26, 2017 3:13 pm

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.

jgelinas3838
OpenVpn Newbie
Posts: 19
Joined: Sun Mar 12, 2017 3:29 pm

Re: Using Python for Scripting

Post by jgelinas3838 » Wed Apr 26, 2017 3:22 pm

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?

jgelinas3838
OpenVpn Newbie
Posts: 19
Joined: Sun Mar 12, 2017 3:29 pm

Re: Using Python for Scripting

Post by jgelinas3838 » Wed Apr 26, 2017 10:27 pm

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.

jgelinas3838
OpenVpn Newbie
Posts: 19
Joined: Sun Mar 12, 2017 3:29 pm

Re: Using Python for Scripting

Post by jgelinas3838 » Wed Apr 26, 2017 11:10 pm

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)

TiTex
OpenVPN Super User
Posts: 310
Joined: Tue Apr 12, 2011 6:22 am

Re: Using Python for Scripting

Post by TiTex » Thu Apr 27, 2017 7:59 am

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

pruebasunodostres123
OpenVpn Newbie
Posts: 1
Joined: Tue Mar 26, 2019 9:42 am

Re: Using Python for Scripting

Post by pruebasunodostres123 » Tue Mar 26, 2019 9:45 am

Hi,

I trying a connect with python to my openvpn server.

This script functions?

Please, can you help me?

Regards,

SkyChaser
OpenVpn Newbie
Posts: 4
Joined: Sat Mar 23, 2019 6:14 pm

Re: Using Python for Scripting

Post by SkyChaser » Wed Mar 27, 2019 3:57 am

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

Post Reply