我正在编写一个GUI程序,以生成和监视SSH隧道,该组用户过于胆怯而无法使用命令行.不幸的是,所讨论的服务器非常严格.通过RSA SecurID令牌进行的两因素身份验证是打开SSH连接的唯一官方认可的方法.不允许使用无密码的R...

我正在编写一个GUI程序,以生成和监视SSH隧道,该组用户过于胆怯而无法使用命令行.
不幸的是,所讨论的服务器非常严格.通过RSA SecurID令牌进行的两因素身份验证是打开SSH连接的唯一官方认可的方法.不允许使用无密码的RSA公钥/私钥身份验证.
因此,我的程序有必要从文本输入框中读取密码,并将其发送给子SSH进程.不幸的是,ssh竭尽全力确保密码仅来自真实的键盘.
我强烈不希望使用第三方模块.我知道paramiko和pexpect(它们都是对类似问题的可能解决方案),但是试图向我的用户解释如何从源代码安装Python模块实在令人头疼.
因此:如何仅使用标准python子进程模块将密码发送到ssh子进程?有什么方法可以欺骗子进程,使我认为我正在使用TTY?是否可以使用SSH_ASKPASS读取我的程序?
还允许使用其他标准库模块(例如,带有os模块的低级命令).
解决方法:
最后,我能够使用pty模块通过伪终端控制ssh.我在pexpect中编写了一个解决方案,然后通过查看pexpect源代码并从this StackOverflow answer获得帮助,我能够弄清楚该怎么做.这是相关代码的摘录(作为对象方法的一部分执行;因此是对自身的引用).
command = [
'/usr/bin/ssh',
'{0}@{1}'.format(username, hostname),
'-L', '{0}:localhost:{1}'.format(local_port, foreign_port),
'-o', 'NumberOfPasswordPrompts=1',
'sleep {0}'.format(SLEEP_TIME),
]
# PID = 0 for child, and the PID of the child for the parent
self.pid, child_fd = pty.fork()
if not self.pid: # Child process
# Replace child process with our SSH process
os.execv(command[0], command)
while True:
output = os.read(child_fd, 1024).strip()
lower = output.lower()
# Write the password
if lower.endswith('password:'):
os.write(child_fd, self.password_var.get() + '\n')
break
elif 'are you sure you want to continue connecting' in lower:
# Adding key to known_hosts
os.write(child_fd, 'yes\n')
elif 'company privacy warning' in lower:
pass # This is an understood message
else:
tkMessageBox.showerror("SSH Connection Failed",
"Encountered unrecognized message when spawning "
"the SSH tunnel: '{0}'".format(output))
self.disconnect()
本文标题为:通过Python子进程的SSH密码


基础教程推荐
- 使用Python解析JSON详解 2023-09-04
- Python全栈开发——进程与线程(2) 2023-09-03
- 使用anaconda 3 for python 3.4在ubuntu 15.04上安装caffe-找不到模块caffe 2023-11-11
- anaconda安装后打不开解决方式(亲测有效) 2022-10-20
- 通过python-turtle库实现绘制图画 2023-08-09
- Django利用LogEntry生成历史操作实战记录 2023-08-09
- Python Linux系统管理与自动化运维 2023-09-04
- 人脸识别实战之Opencv+SVM实现人脸识别 2023-08-04
- 基于OpenCV实现小型的图像数据库检索功能 2023-08-04
- Python类使用相同的内存 2023-11-12