简体   繁体   中英

Issues getting stdout from python paramiko

I'm experimenting with paramiko and AWS and I'm having trouble getting SSH to work. It works when I manually SSH into the server and I'm not getting any error messages. Could I get a little help figuring out what I'm doing wrong?

I've tried both stdout.read() and stdout.readlines(). Both come up empty.

Code:

#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...")

Output:

Connecting
Connected
Executing ifconfig
b''

Output when I SSH into the server:

ssh -i TestInstanceKey.pem ec2-user@XXXXXXXXXX

       __|  __|_  )
       _|  (     /   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.
[ec2-user@ip-XXXXXXXXX ~]$ 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

I needed to do the full path. So /sbin/ifconfig not just ifconfig.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM