stdout readlines for windows hangs python 3.4 - python

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()

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

Paramiko Segmentation Fault during Connect

I am trying to use paramiko to execute an ssh command remotely and get the results back. I run into an issue when trying to connect to the host. My code is as follows:
from paramiko import SSHClient
import paramiko
from socket import gethostbyaddr
root = '<path>/<to>/<key_file>'
client = SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
hostname = gethostbyaddr(instance.public_ip)[0]
client.connect(hostname=hostname, username=instance.user, key_filename=root)
stdin, stdout, stderr = client.exec_command("ls")
print(stdout.readlines())
client.close()
When I call client.connect(...) a segmentation fault occurs. I have tracked down the issue as far as self.start() on line 488 of transport.py with paramiko version 2.1.2 and python version 2.7.12.
Any help is appreciated!

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?

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()

Categories