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!
Related
I am trying to copy a file from a virtual machine to another remote machine using python paramiko. I am getting Authentication failed error message. Can you please suggest how to fix this issue.
Here is my sample code:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='MYHOST', username='username#Domain', Password='xxx', Port=22)
sftp_client = ssh.open_sftp()
sftp_client.get('\\xxxxxx\xxx\Copy.txt', 'C:\Knowledge\Script\PA.txt')
sftp_client.close()
ssh.close()
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
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()
I am running python 2.6.6 (64 bit) on windows 7 with paramiko version 1.12.0.
I have a script that runs an executable on a remote unix machine. The script works most of the time (50-70%) but doesn't work occasionally . Here is a simplified version of my script.
import paramiko
import logging
logger = paramiko.util.logging.getLogger()
logger.setLevel(logging.DEBUG)
paramiko.util.log_to_file("filename.log")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, 22, username, password)
chan = ssh.invoke_shell()
chan.send("cd /home/usr/exe_loc\n")
buff = _read_buffer(chan)
print(buff)
send_code = chan.send("nohup exe_name -r1234 &\n")
print("exec exit code = %d"%send_code)
_read_buffer prints buffer and works as expected.
Even when the script fails to run the executable, the 'cd' command is still printed by the buffer and the send_code is > 0 meaning the command was sent successfully(?).
I compared 'filename.log' file generated by successful run, failed run. The only difference is the failed run has an additional line (below) at the end of the file.
DEB [20160212-16:04:31.512] thr=1 paramiko.transport: EOF in transport thread
Also, this happens more frequently when I run the script after a long break. When I run this back to back for debugging it rarely fails.
Any ideas on why this could be going on here? Let me know if I can provide additional information.
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?