2016-06-26 53 views
0

我是新來的Python使用腳本SSH(W /或期望的paramiko)如預期

我想寫Python腳本中使用SSH 此重新啓動機器不工作就是我的功能:

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) 

我的問題是不工作 如果我手動運行相同的命令一切運作良好

輸出示例:

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

如果我只是複製/輸出貼吧的作品 我的機器和我的遠程均爲RedHat6.4 它是一個沒有任何安全性的變化,我知道

有什麼問題或者有什麼其他辦法這個可以做到 ?


編輯: 閱讀評論後,我已經搬到(回)來使用paramiko_1.17.1 module 但是......問題仍然存在

下面

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

這裏是我的新代碼:

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) 

輸出:

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

自動化SSH是公共密鑰,因爲這樣你不容易不得不輸入密碼。 – melpomene

+0

公鑰中的含義是什麼?我不熟悉 – LordTitiKaka

+1

@LordTitiKaka它的ssh可以用什麼加密它的數據。請參閱https://www.youtube.com/watch?v=GSIDS_lvRv4 –

回答

0

經過一番搜索和閱讀,我發現一些作品

原因我的問題是未知(我會嘗試更新,如果我找到的東西,是與版本相關的什麼通用)

原代碼:

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

修改後的代碼:

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) 

如果別人在爲什麼工作,我花費想知道:)