Connecting to OpenVPN config using python - python

Is there any libraries for python that can be use for connecting to OpenVPN service using .ovpn configuration file?
The openvpn-api library needs OpenVPN installed on your system.

Good news - you don't need any external libs to connect openvpn.
You may just run cmd command from python script like this:
# write the command to a variable
cmd = 'start /b cmd /c "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect config.ovpn'
# split the command to parameters (It's not a necessity, it's just a rule of good taste)
args = shlex.split(cmd)
# run and remember the process as 'x'
x = subprocess.Popen(args, shell=True)

Related

Using MobaXterm as Linux shell on Windows

I have a Python script which leverages subprocess to call MobaXterm and use it to run a command to my server through SSH. The script works fine when using the Windows Subsystem for Linux (WSL), but fails when using Moba. This is the code:
import subprocess
moba_path = "C:\Program Files (x86)\Mobatek\MobaXterm\MobaXterm.exe"
subprocess.run(f'{moba_path} -exec ssh my_server "mkdir test_dir"')
It opens the MobaXterm window but does not show any sign of command execution. I checked, and the command has not been executed (the folder has not been created).
Any ideas?
So I found a way to execute my commands using Moba in a programmatic way using the -newtab command. It starts a new tab which then runs the specified command.
import subprocess
moba_path = "C:\Program Files (x86)\Mobatek\MobaXterm\MobaXterm.exe"
subprocess.run(f'{moba_path} -newtab ssh my_server "mkdir test_dir"')

Unable to SSH via Python subprocess

I need to ssh into a machine via a bastion. Therefore the command is rather very long for this:
ssh -i <pemfile location> -A -o 'proxycommand ssh -i <pemfile location> ec2-user#<bastion ip address> -W %h:%p' hadoop#<machine ip>
This command is rather very long. So I tried to write a python script which takes ip addresses and pemfile location as inputs and does ssh.
#!/usr/local/bin/python3
import argparse
import subprocess
import os
import sys
import errno
parser = argparse.ArgumentParser(description="Tool to ssh into EMR via a bastion host")
parser.add_argument('master', type=str, help='IP Address of the EMR master-node')
parser.add_argument('bastion', type=str, help='IP Address of bastion EC2 instance')
parser.add_argument('pemfile', type=str, help='Path to the pemfile')
args = parser.parse_args()
cmd_list = ["ssh", "-i", args.pemfile, "-A", "-o", "'proxycommand ssh -i {} ec2-user#{} -W %h:%p'".format(args.pemfile, args.bastion), "hadoop#{}".format(args.master)]
command = ""
for w in cmd_list:
command = command + " " + w
print("")
print("Executing command : ", command)
print("")
subprocess.call(cmd_list)
I get the following error when I run this script :
command-line: line 0: Bad configuration option: 'proxycommand
But I am able to run the exact command via bash.
Why is the ssh from python script failing then?
You are making the (common) mistake of mixing syntactic quotes with literal quotes. At the command line, the shell removes any quotes before passing the string to the command you are running; you should simply do the same.
cmd_list = ["ssh", "-i", args.pemfile, "-A",
"-o", "proxycommand ssh -i {} ec2-user#{} -W %h:%p".format(
args.pemfile, args.bastion), "hadoop#{}".format(args.master)]
See also When to wrap quotes around a shell variable? for a discussion of how quoting works in the shell, and perhaps Actual meaning of 'shell=True' in subprocess as a starting point for the Python side.
However, scripting interactive SSH sessions is going to be brittle; I recommend you look into a proper Python library like Paramiko for this sort of thing.

How to launch gnome-terminal from dbus plugin, a systemd service, using Python

I use Python to develop a simple dbus based plugin (dbus.service.Object) that takes IPC calls from a command-line script. The dbus service is configured to host in systemd. It is running as expected. However, when I add a capability in the plugin to launch a gnome-terminal it failed. It seems the execution is stuck in any of these subprocess calls, no matter it is call() or Popen(). Wonder how can I make the plugin launch gnome-terminal correctly?
#subprocess.call(['gnome-terminal', '--', '/bin/bash', '-c', 'ls', '-al', '&'])
#subprocess.call(['systemd-run', '--user', '--service-type=forking', 'gnome-terminal', '-t', "Test",'--','/bin/bash', '-c', 'ls', '-al', "&"])
#subprocess.call(['DISPLAY=:0', 'gnome-terminal', '--', '/bin/bash', '-c', 'ls', '-al', '&'])
Things I also tried to launch a python script from the plugin and have the script to launch gnome-terminal but still failed.
Fail means that the terminal does not open and the ps doesn't show terminal. I also tried with os.system. It works if running directly from a console.
os.system('gnome-terminal -- /bin/bash -c "python /data/scripts/test.a.1.py -f /data/station_profile_A.json"')
The journal log shows the error below:
Dec 21 09:34:20 ubuntu coordinator[5380]: Unable to init server: Could not connect: Connection refused
Dec 21 09:34:20 ubuntu coordinator[5380]: # Failed to parse arguments: Cannot open display:
If I add 'DISPLAY=:0' in front of gnome-terminal, it doesn't help.
To connect to X, you need to set both DISPLAY and XAUTHORITY.
For example XAUTHORITY=$HOME/.Xauthority and DISPLAY=:0.0.
The file named by XAUTHORITY must be readable. The file is mode 0600.
I would suggest that you have your DBUS API include handing over a copy of the magic cookie, the contents of the XAUTHORITY file, to the service which then would write it to a temporary file and then with DISPLAY and XAUTHORITY set would run the command.

How do I copy files from windows system to any other remote server from python script?

I don't want to use external modules like paramiko or fabric. Is there any python built in module through which we can transfer files from windows. I know for linux scp command is there like this is there any command for windows ?
Paramiko is stable, simple and supports Linux, OS X and Windows.
You can install via pip:
pip install paramiko
Simple Demo:
import base64
import paramiko
key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
client = paramiko.SSHClient()
client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
client.connect('ssh.example.com', username='strongbad', password='thecheat')
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout:
print('... ' + line.strip('\n'))
client.close()
Something similar to scp is the Copy-Item cmdlet that's available in Powershell, You could execute powershell and run a Copy-Item command to copy a file from your local windows system to another directory or a remote server directory.
You need to first set the PowerShell for unrestricted access by doing Set-ExecutionPolicy Unrestriced after which you can use the subprocess module of python to make a call to execute the required script.
Maybe this answer is of help to you.
Server
python -m http.server
this will create a http server on port 8000
client
python -c "import urllib; urllib.urlretrieve('http://x.x.x.x:8000/filename', 'filename')"
where x.x.x.x is your server ip, filename is what you want to download

How to execute remote pysftp commands under a specific shell

I use python module pysftp to connect to remote server. Below you can see python code :
import pysftp
import sys
import sqr_common
srv = pysftp.Connection(host="xxxxxx", username="xxxx",
password="xxxxx")
command = "/usr/bin/bash"
command2="APSHOME=/all/aps/msc_2012; export APSHOME; "
srv.execute(command)
srv.execute(command2)
srv.close()
Problem is that command /usr/bin/bash is an infinite process , so my script will never be executed. Can anyone help me how to choose shell on remote server for example bash and execute command in bash on remote server?? Is there any pysftp function that allows me chosing shell??
try this
/usr/bin/bash -c "APSHOME=/all/aps/msc_2012; export APSHOME; "
This problem is not specific to Python, but more like how to execute commands under specific shell.
If you need to run only single command you can run using bash -c switch
bash -c "echo 123"
You can run multiple commands ; separated
bash -c "echo 123 ; echo 246"
If you need to many commands under a specific shell, remotely create a shell script file (.bash file) an execute it
bash myscript.bash

Categories