【发布时间】:2018-03-18 09:54:41
【问题描述】:
我正在尝试使用 paramiko 和 AWS,但无法让 SSH 正常工作。当我手动 SSH 到服务器并且我没有收到任何错误消息时,它可以工作。我可以帮我弄清楚我做错了什么吗?
我已经尝试过 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 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
【问题讨论】:
-
有来自 stderr 的东西吗?
-
b'bash: ifconfig: command not found\n' 看起来它需要完整路径。再试一次。
标签: python amazon-web-services amazon-ec2 ssh paramiko