Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python paramiko ssh

Tags:

i'm new on python. i wrote a script to connect to a host and execute one command

ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=user, password=pw)  print 'running remote command'  stdin, stdout, stderr = ssh.exec_command(command) stdin.close()  for line in stdout.read().splitlines():     print '%s$: %s' % (host, line)     if outfile != None:         f_outfile.write("%s\n" %line)  for line in stderr.read().splitlines():     print '%s$: %s' % (host, line + "\n")     if outfile != None:         f_outfile.write("%s\n" %line)  ssh.close()  if outfile != None:     f_outfile.close()  print 'connection to %s closed' %host  except:    e = sys.exc_info()[1]    print '%s' %e 

works fine when then remote command doesn't need a tty. i found an invoke_shell example Nested SSH session with Paramiko. i'm not happy with this solution, because if a server has an prompt that isn't specified in my script -> infinite loop or a specified prompt in the script is a string in the return text -> not all data will be received. is there a better solution maybe where stdout and stderr are send back like in my script?

like image 988
user1008764 Avatar asked May 24 '12 20:05

user1008764


2 Answers

There is something wrong with the accepted answer, it sometimes (randomly) brings a clipped response from server. I do not know why, I did not investigate the faulty cause of the accepted answer because this code worked perfectly for me:

import paramiko  ip='server ip' port=22 username='username' password='password'  cmd='some useful command'   ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip,port,username,password)  stdin,stdout,stderr=ssh.exec_command(cmd) outlines=stdout.readlines() resp=''.join(outlines) print(resp)  stdin,stdout,stderr=ssh.exec_command('some really useful command') outlines=stdout.readlines() resp=''.join(outlines) print(resp) 
like image 161
Raiden Core Avatar answered Sep 23 '22 13:09

Raiden Core


There is extensive paramiko API documentation you can find at: http://docs.paramiko.org/en/stable/index.html

I use the following method to execute commands on a password protected client:

import paramiko  nbytes = 4096 hostname = 'hostname' port = 22 username = 'username'  password = 'password' command = 'ls'  client = paramiko.Transport((hostname, port)) client.connect(username=username, password=password)  stdout_data = [] stderr_data = [] session = client.open_channel(kind='session') session.exec_command(command) while True:     if session.recv_ready():         stdout_data.append(session.recv(nbytes))     if session.recv_stderr_ready():         stderr_data.append(session.recv_stderr(nbytes))     if session.exit_status_ready():         break  print 'exit status: ', session.recv_exit_status() print ''.join(stdout_data) print ''.join(stderr_data)  session.close() client.close() 
like image 45
ThePracticalOne Avatar answered Sep 21 '22 13:09

ThePracticalOne