2017-10-06 150 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。