2017-10-06 63 views
0

我正在嘗試paramiko和AWS,並且我無法使SSH工作。它在我手動SSH進入服務器並且沒有收到任何錯誤消息時起作用。我可以幫忙弄清楚我做錯了什麼嗎?從python paramiko獲得標準輸出的問題

我試過stdout.read()和stdout.readlines()。兩人都空虛。

代碼:

#Load the key into a file and attempt to SSH in 
key = paramiko.RSAKey.from_private_key_file('TestInstanceKey.pem') 
ssh = paramiko.SSHClient() 
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
print("Connecting") 
#Try to ssh in and run and display ifconfig 
try: 
    ssh.connect(hostname=instance.public_ip_address, username="ec2-user", 
    pkey=key) 
    print("Connected") 
    print("Executing ifconfig") 
    stdin, stdout, stderr = ssh.exec_command("ifconfig") 
    lines = stdout.read() 
    print(lines) 
    for line in lines: 
     print(line) 
    ssh.close() 
except Exception as e: 
    print(e) 
    input("Press enter to continue...") 

輸出:

Connecting 
Connected 
Executing ifconfig 
b'' 

輸出,當我SSH到服務器:

ssh -i TestInstanceKey.pem [email protected] 

     __| __|_ ) 
     _| ( / Amazon Linux AMI 
     ___|\___|___| 

https://aws.amazon.com/amazon-linux-ami/2016.09-release-notes/ 
22 package(s) needed for security, out of 67 available 
Run "sudo yum update" to apply all updates. 
Amazon Linux version 2017.09 is available. 
[[email protected] ~]$ ifconfig 
eth0  Link encap:Ethernet HWaddr XXXXXXXXXXX 
      inet addr:XXXXXXXXX Bcast:Xxxxxxxxx.255 Mask:255.255.240.0 
      inet6 addr: xxxxxxx Scope:Link 
      UP BROADCAST RUNNING MULTICAST MTU:9001 Metric:1 
+0

有什麼來自stderr? – Kyle

+0

b'bash:ifconfig:找不到命令\ n' 看起來它需要完整路徑。再試一次。 – Smipims

回答

0

我需要做的完整路徑。所以/ sbin/ifconfig不只是ifconfig。