LordTitiKaka LordTitiKaka - 1 month ago 12
Python Question

using ssh in script (w/ expect or paramiko) not working as expected

I'm new to Python

I want to write

Python
script to reboot machine using
SSH

this is my function :

def sendCommandViaSSH(usr,psswrd,command):
line = 'ssh '+ usr +'@'+ RIp + ' ' +"\""+ command+"\""
print_message(line)
child = pexpect.spawn (line)
#i = child.expect('Are you sure you want to continue connecting (yes/no)?')
i = child.expect (['Are you sure you want to continue connecting (yes/no)?', pexpect.EOF, pexpect.TIMEOUT],2)
print_message(i)
if i == 0:
child.sendline ('yes')
i = child.expect(['s password:', pexpect.EOF, pexpect.TIMEOUT])
print_message(i)
if i == 0:
print_message(psswrd)
child.sendline (psswrd)


My problem is that is not working
If I run the same command manually everything working well

example output:

ssh MyUsr@XXX.XXX.XXX.XXX "shutdown -k now"
2
0
MyPsswrd


If I just copy/paste from output it works
my machine and my remote are both
RedHat6.4

it is without any security changes that I know of

what is the problem or what other way this can be done ?




EDIT:
After reading the comments I've moved (back) to use
paramiko_1.17.1 module

but ... problem persists

below :

1.code that i wrote so far
2.output that I get using it (while encountering the same problem of not working commands )


here is my new code :

import paramiko
import logging
logging.basicConfig(level=logging.DEBUG)

def exec_paramiko(ssh, command):
print_message("executing '{0}' on remote machine".format(command))
chan = ssh.get_transport().open_session()
chan.get_pty()
f = chan.makefile()
chan.exec_command(command)
flag=True
exit_code=None
output=""
while True:
if chan.recv_ready():
output+=(chan.recv(4096).decode('ascii') + "\n")
if chan.recv_stderr_ready():
output+=("error: {0}".format(chan.recv_stderr(4096).decode('ascii')))
if chan.exit_status_ready():
output+=(chan.recv(4096).decode('ascii') + "\n")
error_string=0
if chan.recv_stderr_ready():
error_string=chan.recv_stderr(4096).decode('ascii')
exit_code = chan.recv_exit_status()
if exit_code != 0:
output+="\nerror: {0}".format(error_string)
break
return exit_code, output

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname="10.xx.xx.xx",username="usr",password="psswrd",allow_agent=False,look_for_keys=False, timeout=20)
exit_code, output = exec_paramiko(ssh,'shutdown -k now' )
print "error code: {0} message: {1}".format(exit_code, output)





output :

DEBUG:paramiko.transport:starting thread (client mode): 0xdb17c310L
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_1.17.1
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-OpenSSH_5.3
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_5.3)
DEBUG:paramiko.transport:kex algos:[u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa', u'ssh-dss'] client encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] server encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] client mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] server mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] client compress:[u'none', u'zlib@openssh.com'] server compress:[u'none', u'zlib@openssh.com'] client lang:[u''] server lang:[u''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group1-sha1
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-md5
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:kex engine KexGroup1 specified hash_algo <built-in function openssl_sha1>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-rsa host key for xx.xx.xx.xx: 0c13035c7e4c0e3999d1c85215e97394
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
INFO:paramiko.transport.sftp:[chan 0] Opened sftp connection (server version 3)
shutting down
executing 'shutdown -k now' on remote machine
DEBUG:paramiko.transport:[chan 1] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 1] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 1 opened.
DEBUG:paramiko.transport:[chan 1] Sesch channel 1 request ok
DEBUG:paramiko.transport:[chan 1] Sesch channel 1 request ok
DEBUG:paramiko.transport:[chan 1] EOF received (1)
DEBUG:paramiko.transport:[chan 1] EOF sent (1)
error code: 0 message:

DEBUG:paramiko.transport:EOF in transport thread

Answer

After much searching and reading I've found something that works

The reason for my problem is unknown (I'll try to update if I find something that is version related or something generic)

original code:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.exec_command(command)

modified code:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())    
chan = ssh.get_transport().open_session()
#instead of ssh.exec_command(command)
chan.exec_command(command)

If someone can expend on why is working I would like to know :)

Comments