execute a program on a remote machine python - python

I can execute a script from python environment locally using subprocess but due to cross platform issues, I have to execute it on a remote server and get back the results on my local machine.
The directory parserpath contains some third party modules that can be executed using a script run.sh present in parserpath directory. However this parserpath directory is present on a remote server.
This is what I have, but this will work only if parserpath is a local directory. How can I ssh to a remote directory and run the script run.sh?
def run_parser(filename):
current_dir = os.getcwd()
parser_path="/parserpath"
os.chdir(parser_path)
subprocess.call("./run.sh " + filename, shell=True)
os.chdir(current_dir)

With most linux shells, you can run a command in a different working directory by executing a subshell as in
/home/usr> (cd /usr/local/bin;pwd)
/usr/local/bin
/home/usr>
You can do the same thing through ssh to the remote system. Depending on which ssh client you use, you may thin that up a bit. For instance, with paramikos exec_command, a new remote shell is created for each command so cd /path/on/remote/machine;./run.sh is sufficient.
A minimalist example for paramiko on python 2.x is
import sys
import paramiko
try:
hostname, username, password, targetpath = sys.argv[1:5]
except ValueError:
print("Failed, call with hostname username password targetpath")
command = "cd {};pwd".format(targetpath)
print("Command to send: {}".format(command))
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, username=username, password=password)
stdin, stdout, stderr = ssh.exec_command("cd {};pwd".format(targetpath))
print(stdout.read())
ssh.close()
python3 should be similar. There are other options like libssh2 bindings for python, pexpects ssh support and etc...

Use SSH keys to automate the process of logging in via SSH. Here is the following code to execute a script remotely.
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

Try, ssh user#host sh path/run.sh

Related

Paramiko with subprocess

I want to run a program on a remote server and send command to it from my computer using subprocess and Paramiko. Is below can be usefull?
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('host', username='user', password="password")
myprogramme = subprocess.Popen("myprogramme.exe", stdin=subprocess.PIPE)
myprogramme.stdin.write(ssh_stdout.read())
myprogramme.communicate("some_inputs\n")
myprogramme.kill
You cannot run a program on a remote server over SSH with subprocess.
Use SSHClient.exec_command to execute your command.
Then you can feed your command to the process using the returned stdin:
Pass input/variables to command/script over SSH using Python Paramiko

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

Copy a directory structure including files from one remote machine to another using python

I want to copy a directory recursively from one remote machine to another remote machine with Python, probably using paramiko.
I am looking for something similar to the following scp command, but using python instead:
scp user#10.3.0.1:/path/to/file user#10.3.0.2/path/to/file
Run this command from the machine where u want to copy these file(not from source machine).
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('10.3.0.1', username='user', password='password')
stdin, stdout, stderr = client.exec_command('rsync -rav pi#10.3.0.1:path/to/file ~/')
for line in stdout:
print '... ' + line.strip('\n')
client.close()

java command not found Paramiko

I'm trying to run a .sh file using paramiko. with this code:
import paramiko
cmd = "cd path ; ./ file.sh"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(server,username.password)
stdin, stdout, stderr = ssh.exec_command(cmd)
print stdout.readlines()
ssh.close()
but I got this error:
java command not found.
file is passing parameters to loadtestrunner.sh and the error is refering to a line in loadtest runner which is:
java $JAVA_OPTS -cp $SOAPUI_CLASSPATH com.eviware.soapui.SoapUIProLoadTestRunner "$#"
java is installed on server. and loadtestrunner succesfuly runs directly from server
You may have some shell profile that loads java into PATH. If not, create one. Mine is ~/.bash_profile and it contains the line export PATH=${PATH}:/usr/java/jdk1.6.0_21/bin.
Simply append source ~./bash_profile \n to your command and you should be able to use java from paramiko's ssh. (Note that your profile my be ~/.bashrc or a non-bash alternative)

Python subprocess on remote MS Windows host

I'm trying to run netsh command on remote windows hosts (windows domain environment with admin rights). The following code works fine on local host but I would like to run it on remote hosts as well using python.
import subprocess
netshcmd=subprocess.Popen('netsh advfirewall show rule name=\”all\”', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE )
output, errors = netshcmd.communicate()
The problem is that I'm no sure how/what method to use to initiate the connection to remote hosts and then run the subprocess commands. I cannot use ssh or pstools and would like try to implement it using existing pywin32 modules if possible.
I have used WMI module in a past which makes it very easy to query remote host but I couldn't find any way to query firewall policies over WMI and that's why using subprocess.
First you login the remote host machine using of pxssh modules Python: How can remote from my local pc to remoteA to remoteb to remote c using Paramiko
remote login of windows:
child = pexpect.spawn('ssh tiger#172.16.0.190 -p 8888')
child.logfile = open("/tmp/mylog", "w")
print child.before
child.expect('.*Are you sure you want to continue connecting (yes/no)?')
child.sendline("yes")
child.expect(".*assword:")
child.sendline("tiger\r")
child.expect('Press any key to continue...')
child.send('\r')
child.expect('C:\Users\.*>')
child.sendline('dir')
child.prompt('C:\Users\.*>')
Python - Pxssh - Getting an password refused error when trying to login to a remote server
and send your netsh command
I will recommend using Fabric, it's a powerful python tool with a suite of operations for executing local or remote shell commands, as well as auxiliary functionality such as prompting the running user for input, or aborting execution:
install fabric : pip install fabric
write the following script named remote_cmd.py:
"""
Usage:
python remote_cmd.py ip_address username password your_command
"""
from sys import argv
from fabric.api import run, env
def set_host_config(ip, user, password):
env.host_string = ip
env.user = user
env.password = password
def cmd(your_command):
"""
executes command remotely
"""
output = run(your_command)
return output
def main():
set_host_config(argv[1], argv[2], argv[3])
cmd(argv[4]))
if __name__ == '__main__':
main()
Usage:
python remote_cmd.py ip_address username password command

Categories