ssh登录期间Python输出到终端 [英] Python output to terminal during ssh login

查看:42
本文介绍了ssh登录期间Python输出到终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个问题,但没有找到解决方案.我在 Apple iPod 上使用 python 2.5.1,我正在尝试使用 SSH 连接到主机.首先,我从 import os 开始.接下来我os.system('ssh 192.168.1.13).执行此命令后,我接下来尝试 os.system('password').这不起作用并且 SSH 要求输入密码,在我退出 SSH 会话后,我的 password 被打印出来.有什么办法可以得到它,以便将密码发送到 SSH?我不想向 python 添加任何其他模块.谢谢!

I have been looking everywhere for this and have not found a solution. I am using python 2.5.1 on an Apple iPod and I am trying to connect to a host with SSH. First I start off with import os. Next I os.system('ssh 192.168.1.13). After this command is executed I next try to os.system('password'). This does not work and SSH asks for the password, after I exit the SSH session then my password is printed. Is there any way that I can get it so that it sends the password to SSH? I don't want to add any other modules to python. Thanks!

完整文件:

import os

os.system('ssh 192.168.1.13')
os.system('password')

推荐答案

看看 pexpect,它应该以您期望的方式发挥作用.

take a look at pexpect, it should do the trick in the manner you expect it.

文档中的示例:

child = pexpect.spawn('scp foo myname@host.example.com:.')
child.expect ('Password:')
child.sendline (mypassword)

是的,这又是一个模块......但这是完成你想要的最简洁的方式.

Yes, that's yet another module.. but the cleanest way to accomplish what you want.

这篇关于ssh登录期间Python输出到终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆