Paramiko with subprocess - python

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

Related

SSH from shell terminal to another server & execute commands using python paramiko [duplicate]

This question already has an answer here:
Connecting to a server via another server using Paramiko
(1 answer)
Closed 2 years ago.
import time
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('x.x.x.x',22, username='root', password='12345')
ssh_stdin ,ssh_stdout, ssh_stderr = ssh.exec_command('ssh -tt root#y.y.y.y')
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('test#123')
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('!pwd')
This is the code i want to run i want to login to shell terminal of x.x.x.x server & ssh to server y.y.y.y & excute several commands. please help me on this
Try sshpass package.
The problem looks like that after you execute the first command, it is expecting the password, but the command is not returned to the client.
You can try installing sshpass first into x.x.x.x, and then use it like this:
sshpass -p 'YourPassword' ssh user#y.y.y.y
to login to y.y.y.y.

stdout readlines for windows hangs python 3.4

i have a problem with the below script which is used to connect to one of our nodes using ssh and perform a command. when i execute the last line of the script the script hangs. could you please advise if there is a solution in order to solve this issue?
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect('IP',port= 22, username='xxx',password='xxx')
stdin, stdout, stderr = ssh.exec_command('xxxx')
output=stdout.readlines()

Using Plink to run multiple remote commands in interactive shell session

Sudo Python code to connect to remote server but I am not getting any output. I am able to connect to remote using pexpect but on windows it does not work.
from subprocess import Popen,PIPE
command = "plink.exe -ssh test#test.com -pw root123"
sh = Popen(command, stdin=PIPE, stdout=PIPE)
sh.stdin.write('ls /\n')
sh.stdin.write('ls /usr\n')
sh.stdin.close()
out = sh.stdout.read()

execute a program on a remote machine 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

Is there a way to restart tomcat using paramiko?

when I try to run the following code:
import paramiko
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname="192.168.30.68", username="root", password="123456")
stdin, stdout, stderr = s.exec_command("sh /Application/tomcat/bin/startup.sh")
print stdout.read()
s.close()
I get the following output:
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
The tomcat on the remote server is not started. Is there a way to resolve this problem?

Categories