我正在使用Genymotion on demand aws实例,并尝试从python连接它。我想从python向实例发送adb命令。这里有一个从任何计算机连接实例的教程:https://www.genymotion.com/help/on-demand/tutorial/enable-disable-adb/
用于为实例创建ssh隧道的ssh命令是:
ssh -i key.pem -NL 5555:localhost:5555 root@instance_ip
我尝试使用paramiko库,以及来自repo(https://github.com/paramiko/paramiko/blob/master/demos/forward.py)的forward.py演示。
首先,我尝试通过cli创建隧道,这就是我为创建ssh隧道而输入的内容:
python forward.py <instance_ip> -r localhost:5555 -u root -p 5555 -K "path/to/mykey.pem"
回应是:
Connecting to ssh host <instance_ip>:22 ...
*** Failed to connect to <instance_ip>:22: EOFError()
我无法弄清楚这里有什么问题。
有人可以帮助我吗?非常感谢!!
答案 0 :(得分:0)
您可以使用以下代码段从Python发送到EC2实例。
key = paramiko.RSAKey.from_private_key_file(path/to/mykey.pem)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect/ssh to an instance
try:
client.connect(hostname=instance_ip, username="root", pkey=key)
# Execute a command(cmd) after connecting/ssh to an instance
stdin, stdout, stderr = client.exec_command(cmd)
print stdout.read()
# close the client connection once the job is done
client.close()
break
except Exception, e:
print e