Python遠程linux執行命令

來源:互聯網
上載者:User

標籤:package   密鑰驗證   site   pwd   ddp   hostname   names   exec   ret   

1、遠程登入到linux上,使用到的模組paramiko

#遠程登陸作業系統def ssh(sys_ip,username,password,cmds):    try        #建立ssh用戶端        client = paramiko.SSHClient()        #第一次ssh遠程時會提示輸入yes或者no        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())        #密碼方式遠端連線        client.connect(sys_ip, 22, username=username, password=password, timeout=20)        #互信方式遠端連線        #key_file = paramiko.RSAKey.from_private_key_file("/root/.ssh/id_rsa")        #ssh.connect(sys_ip, 22, username=username, pkey=key_file, timeout=20)        #執行命令        stdin, stdout, stderr = ssh.exec_command(cmds[key])        #擷取命令執行結果,返回的資料是一個list        result = stdout.readlines()        return result    except Exception, e:        print e    finally:        client.close()if __name__=="__main__":    sys_ip = "192.168.0.102"    username = "root"    password = "1"    cmds = "pwd"    print ssh(sys_ip,username,password,cmds)

此處有個主意點,我遇到了,在密碼登陸的時候,同樣的python串連代碼,放在python指令碼裡面執行就是報下面的錯,但是將代碼拷貝到python的互動模式下執行就是成功的,這個時候就是需要看下要遠端使用者ssh配置是不是只支援鍵盤互動、密鑰驗證,如果是這樣的話,使用密碼遠程登入就會報下面的錯誤

File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 337, in connect    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)  File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 528, in _auth    raise saved_exceptionAuthenticationException: Authentication failed.  File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 337, in connect    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)  File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 528, in _auth    raise saved_exceptionparamiko.SSHException: No existing session

這個是因為linux會檢測遠端連線有沒有tty(鍵盤互動),指令碼方式啟動並執行時候就沒有鍵盤互動,看網上有在connect加,allow_agent=False,look_for_keys=False這2個參數解決的,但是我的沒有解決,ssh.connect(‘localhost‘,username=name,password=pw,allow_agent=False,look_for_keys=False)

2、使用ssh,需要使用到shell命令expect

首先建立一個shell指令碼remotExect.sh

#!/usr/bin/expectset timeout 2set local_file  [lindex $argv 0]set username    [lindex $argv 1] set password    [lindex $argv 2] set hostname    [lindex $argv 3]set remote_file [lindex $argv 4]spawn scp $local_file [email protected]$hostname:$remote_fileexpect {"yes/no"#是為了捕獲首次登入,要手動輸入yes/no的情況{send "yes\r";}"password:"#為例捕獲需要輸入密碼的行為{send "$password\r";}}expect eof

然後在python中執行

import oscpFileCmd = "./remotecp.sh 1.txt root 1 192.168.0.102 /opt/1.txt"os.system(cpFileCmd)

  

Python遠程linux執行命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.