簡體   English   中英

無法在python ssh的輸出中得到任何東西

[英]not able to get anything in output in python ssh

我正在嘗試連接到遠程服務器,然后嘗試在該計算機上登錄sql服務器。 但是我沒有得到這個腳本的輸出

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy() )
ssh.connect(hostname='172.18.109.244', username='bgf', password='bgf')
print "Logged In to EMS"
cmd = 'mysql -uroot -root'

stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.write("show databases;")
stdin.write('\n')
stdin.flush()
print stdout.read()

如@Charles的評論中所述,

read()將內容消耗到EOF。 這里沒有EOF,因為MySQL仍處於打開狀態。 簡單的答案是不僅刷新標准輸入,而且關閉它。

您不能這樣做,因為MySQL仍處於打開狀態,您的腳本將掛在那里。 因此,如果您只想獲取數據庫,則將查詢傳遞給MySQL連接,即可正常工作:)。

    import paramiko
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy() )
    ssh.connect(hostname='172.18.109.244', username='somuser',password='bgf')
    print "Logged In to EMS"
    cmd = 'mysql -h somehost.example.com -uroot -proot -e \'show databases;\''

    stdin, stdout, stderr = ssh.exec_command(cmd)
    # stdin.write("show databases;")
    stdin.write('\n')
    stdin.flush()
    print stdout.read()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM