Paramiko: read from standard output of remotely executed command

You have closed the connection before reading lines: import paramiko client=paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) com=”ls ~/desktop” client.connect(‘MyIPAddress’,MyPortNumber, username=”username”, password=’password’) output=”” stdin, stdout, stderr = client.exec_command(com) print “ssh succuessful. Closing connection” stdout=stdout.readlines() client.close() print “Connection closed” print stdout print com for line in stdout: output=output+line if output!=””: print output else: print “There was no output for this command”

Paramiko authentication fails with “Agreed upon ‘rsa-sha2-512’ pubkey algorithm” (and “unsupported public key algorithm: rsa-sha2-512” in sshd log)

Imo, it’s a bug in Paramiko. It does not handle correctly absence of server-sig-algs extension on the server side. Try disabling rsa-sha2-* on Paramiko side altogether: ssh_client.connect( server, username=ssh_user, key_filename=ssh_keypath, disabled_algorithms=dict(pubkeys=[“rsa-sha2-512”, “rsa-sha2-256”])) (note that there’s no need to specify port=22, as that’s the default) I’ve found related Paramiko issue: RSA key auth failing from paramiko … Read more

How to run sudo with Paramiko? (Python)

check this example out: ssh.connect(‘127.0.0.1′, username=”jesse”, password=’lol’) stdin, stdout, stderr = ssh.exec_command( “sudo dmesg”) stdin.write(‘lol\n’) stdin.flush() data = stdout.read.splitlines() for line in data: if line.split(‘:’)[0] == ‘AirPort’: print line Example found here with more explanations: http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/ Hope it helps!

Nested SSH session with Paramiko

I managed to find a solution, but it requires a little manual work. If anyone have a better solution, please tell me. ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(‘first.com’, username=”luser”, password=’secret’) chan = ssh.invoke_shell() # Ssh and wait for the password prompt. chan.send(‘ssh second.com\n’) buff=”” while not buff.endswith(‘\’s password: ‘): resp = chan.recv(9999) buff += resp # … Read more

Implement an interactive shell over ssh in Python using Paramiko?

import paramiko import re class ShellHandler: def __init__(self, host, user, psw): self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(host, username=user, password=psw, port=22) channel = self.ssh.invoke_shell() self.stdin = channel.makefile(‘wb’) self.stdout = channel.makefile(‘r’) def __del__(self): self.ssh.close() def execute(self, cmd): “”” :param cmd: the command to be executed on the remote computer :examples: execute(‘ls’) execute(‘finger’) execute(‘cd folder_name’) “”” cmd = cmd.strip(‘\n’) … Read more

Execute command and wait for it to finish with Python Paramiko

Use exec_command: http://docs.paramiko.org/en/1.16/api/channel.html stdin, stdout, stderr = ssh.exec_command(“my_long_command –arg 1 –arg 2″) The following code works for me: from paramiko import SSHClient, AutoAddPolicy import time ssh = SSHClient() ssh.set_missing_host_key_policy(AutoAddPolicy()) ssh.connect(‘111.111.111.111’, username=”myname”, key_filename=”/path/to/my/id_rsa.pub”, port=1123) sleeptime = 0.001 outdata, errdata=””, ” ssh_transp = ssh.get_transport() chan = ssh_transp.open_session() # chan.settimeout(3 * 60 * 60) chan.setblocking(0) chan.exec_command(‘ls -la’) while … Read more

Execute (sub)commands in secondary shell/command on SSH server in Python Paramiko

I assume that the gotoshell and hapi_debug=1 are not top-level commands, but subcommands of the stcli. In other words, the stcli is kind of a shell. In that case, you need to write the commands that you want to execute in the subshell to its stdin: stdin, stdout, stderr = ssh.exec_command(‘stcli’) stdin.write(‘gotoshell\n’) stdin.write(‘hapi_debug=1\n’) stdin.flush() If … Read more

Execute multiple dependent commands individually with Paramiko and find out when each command finishes

It seems that you want to implement an interactive shell, yet you need to control individual commands execution. That’s not really possible with just SSH interface. “shell” channel in SSH is black box with an input and output. So there’s nothing in Paramiko that will help you implementing this. If you need to find out … Read more