2016-06-26 29 views
0

erwartet funktioniert Ich bin neu zu Pythonssh in Skript (w/erwarten oder paramiko) nicht als

ich Python Skript schreiben wollen Computer neu zu starten SSH dies mit meiner Funktion ist:

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) 

Mein Problem ist, dass nicht funktioniert ist, wenn ich den gleichen Befehl manuell alles gut funktioniert

Beispiel Ausgang laufen:

012.351.
ssh [email protected] "shutdown -k now" 
2 
0 
MyPsswrd 

Wenn ich nur Kopieren/Einfügen von Ausgangs funktioniert es meine Maschine und meine Fernbedienung sind beide RedHat6.4 es ohne Sicherheitsänderungen ist, die ich kenne

, was das Problem ist, oder was diese andere Art und Weise kann gemacht werden ?


EDIT: Nach dem Lesen der Kommentare Ich habe (zurück) bewegt paramiko_1.17.1 module zu verwenden, aber ... Problem weiterhin besteht

unter:

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

hier ist mein neuer 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) 

Ausgang:

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'[email protected]'] 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'[email protected]'] client mac:[u'hmac-md5', u'hmac-sha1', u'[email protected]', u'hmac-ripemd160', u'[email protected]', u'hmac-sha1-96', u'hmac-md5-96'] server mac:[u'hmac-md5', u'hmac-sha1', u'[email protected]', u'hmac-ripemd160', u'[email protected]', u'hmac-sha1-96', u'hmac-md5-96'] client compress:[u'none', u'[email protected]'] server compress:[u'none', u'[email protected]'] 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 
+0

Automatisieren ssh mit öffentlichen Schlüsseln einfacher ist, weil man dann don‘ t muss ein Passwort eingeben. – melpomene

+0

was bedeutet in öffentlichen Schlüsseln? Ich bin nicht vertraut mit dem – LordTitiKaka

+1

@LordTitiKaka, was ssh seine Daten mit verschlüsseln kann. Siehe https://www.youtube.com/watch?v=GSIDS_lvRv4 –

Antwort

0

Nach langem Suchen und Lesen ich etwas gefunden habe, die

arbeitet

Der Grund für mein Problem ist unbekannt (ich werde versuchen, zu aktualisieren, wenn ich etwas finden, das Version verwandt oder etwas generisch)

Original-Code:

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

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

Wenn jemand auf, warum ich aufwenden kann arbeiten würde gerne wissen :)