0

I did everything to use subprocess module of python to run a VPN from Protonvpn in ubuntu 20.04 but unfortunately I failed! Actually I could run the VPN through (subprocess.run(['sudo', 'openvpn', '*.com.udp.ovpn'])) but the VPN needs ID and password inside itself to make me connected(manually should be typed inside the terminal), I did everything to to give those ID and passwords to the vpn, but I failed! I don't know how can I write my Id and password inside the VPN after running the VPN by the code I mentioned above. Using this code : subprocess.run(["my_ID", "\n", "my_password", "\n"], shell=True) doesn't help, because It doesn't run until the ID and password has been typed inside the vpn (through terminal). In manual way to connect to the VPN, I should first type in terimnal: sudo openvpn *.com.udp.ovpn, then enter, then type my ID, then enter, then my password and again enter, and I will be connected. My purpose is to do the whole process inside a python file, and when I run the python file, the whole process works automatically. thank you very much for time you spend in this question.

amir m
  • 1
  • 3

2 Answers2

1

Try doing this to send input to a process.

from subprocess import Popen, PIPE, STDOUT
p = Popen(['myapp'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
stdout_data = p.communicate(input='data_to_write')[0]

How do I write to a Python subprocess' stdin?

sean-7777
  • 700
  • 4
  • 13
  • Thank you very much for your answer. After performing the code you suggest I receive this error. File "/usr/lib/python3.8/subprocess.py", line 858, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) PermissionError: [Errno 13] Permission denied: '/home/amir/*.protonvpn.com.udp.ovpn' – amir m Mar 24 '22 at 12:22
  • It is most likely that either you do not have permission to open it, or the path to the executable is not a executable, like a folder or a glob. – sean-7777 Mar 25 '22 at 21:05
  • I don't know what you exactly mean by not having permission, I can open it manually, and I even can run it in terminal through running a python file, but I cannot give my ID and password to it after opening it. by running this code : p1 = subprocess.run(['sudo', 'openvpn', '*.protonvpn.com.udp.ovpn']) In terminal I will get: Sun 02:16:09 2022 OpenVPN 2.4.7 (my system information)[SSL (OpenSSL)] ... built on Jul 19 2021 Sun Mar 27 02:16:09 2022 library versions: OpenSSL 1.1.1f 31 Mar 2020, LZO 2.10 Enter Auth Username: my problem is after that how to give in id and then password. – amir m Mar 26 '22 at 21:50
  • And consequently, the second idea, that I don't have permission to execute in the folder, cannot be true either. Sorry for commenting in two separate comments, I was forced to do that because I reached to the maximum word number which is allowed. I personally guess that I should give in the ID and password respectively, and after ID there should be an "enter" then password, and then again "enter". But probably I cannot give in this information properly, and that's why the permission will be denied!Thank you very much for the time you are spending in the issue. – amir m Mar 26 '22 at 21:55
  • I think the most likely problem is that OpenVPN is communicating directly with the terminal, not Standard Input, so you would need a pseudo terminal, which is quite diffucult to setup. I recommend you use a [package](https://github.com/Jamie-/openvpn-api) for this. – sean-7777 Mar 26 '22 at 22:46
  • Yeah as I see, you are completely right, the Openvpn is communicating directly with the terminal,but When the Openvpn is opened, It goes to somehow in a pseudo terminal mood, That I don't know how to give in data to it. Thank you for your time. – amir m Mar 27 '22 at 12:01
0

I finally succeeded to solve the problem! I first tried subprocess and used sys.executable, stdout, stdin, popen and so on, but they didn't work. Then I used communicate method but it didn't work either. Finally I used xdotool with subprocess and it worked. I don't really know how really it works! And I will be happy to find out exactly what happens through these lines! because they are quite counterintuitive! and doesn't seem right at first glance. But despite of that it works and consequently I am completely thrilled and exited about it. I will explain more why the program is so strange!

subprocess.call(["xdotool", "type", "myIdInProtonvpn"])
subprocess.call(["xdotool", "type", "\n"])
subprocess.call(["xdotool", "type", "myPassInProtonvpn"])
subprocess.call(["xdotool", "type", "\n"])

subprocess.run(['sudo', 'openvpn', 'thefile.com.udp.ovpn'])

as you can see, it seems that the line in the end, should be placed in the first line! but it doesn't work in that way! The thing that really happens, is that, first myId and MyPass appear(in two separate lines respectively) in the terminal(before the file opens) and then file opens, and in the file platform in the terminal will appear myId and myPassword again respectively in front of the "Id enter:" and "password enter:" exactly where they should be placed and it automatically connects to the server.

Edit: If somebody has operating system password too, and when he wants to perform a code in terminal , the system asks for inserting the password, the following code can be used to pass the password, the same trick(used above) has been implemented to pass the password automatically in the python code.

subprocess.call(["xdotool", "type", "your operating system password"])

subprocess.call(["xdotool", "type", "\n"])
# this code below, has been used to just activate the password request from terminal
subprocess.run(['sudo', 'apt', 'update'])
#this code below, is not necessary, it just makes the process more clear
time.sleep(1)

so the entire code will be as following:

import subprocess
import time

subprocess.call(["xdotool", "type", "yourOperatingSystemPassword"])
subprocess.call(["xdotool", "type", "\n"])
# this code below, has been used to just activate the password request from terminal
subprocess.run(['sudo', 'apt', 'update'])
#this code below, is not necessary, it just makes the process more clear
time.sleep(1)

subprocess.call(["xdotool", "type", "myIdInProtonvpn"])
subprocess.call(["xdotool", "type", "\n"])
subprocess.call(["xdotool", "type", "myPassInProtonvpn"])
subprocess.call(["xdotool", "type", "\n"])

subprocess.run(['sudo', 'openvpn', 'thefile.com.udp.ovpn'])

Note that don't use this code if you don't have operating system password on your system(because it won't work), and just use the one before and it will work properly for you. The code above is just for the people who have to enter their password in terminal when running codes(so the process will be automated in the python file), and remember when you want to run the python, you should be sure that the password terminal will be asked in the terminal, otherwise the code won't work! For doing so if you are in the middle of your work and you are not sure that the password request would be, just close terminal and then open it again and run the python file and it will work properly.

As an aside, I want to automate the process above to make sure that the program is stable even if the user does a mistake there, for this purpose I need some code to close the terminal and then again open it(to make sure the password request happens by the terminal) but unfortunately I haven't managed to do so. I used many method such as "xdotool type", "xdotool key(ctrl+D or alt+F4)", or even "xdotool mousemove" and so on, but they all fail when the code is going to run from a python file and especially when the terminal is running(the terminal seemingly doesn't allow to be closed while running!). Note that some of these methods work even in python code alone, but they won't work when the process is running, when terminal is running, the terminal will refuse to be closed!

amir m
  • 1
  • 3