用例
在unix服务器上,当手动登录时,会打开自己的命令shell来运行该命令。
我正试图通过使用paramiko来自动化这一点,但是,不知何故,我无法使用paramiko在命令shell上执行命令
我做了什么?
我创建了一个能够建立连接的简单脚本,但是它没有在Vshell上执行命令,因为输出总是空的。
import paramiko import sys ssh_client=paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=sys.argv[1],port=sys.argv[2],username=sys.argv[3],password=sys.argv[4]) command="show hwid" stdin,stdout,stderr=ssh_client.exec_command(command) out=stdout.read() print out err=stderr.read() print err ssh_client.close()
当在服务器上使用相同的脚本 vshell 没有被使用
有什么帮助或建议吗?
stdin,stdout,stderr=ssh_client.exec_command(command)
关于这一行代码,我怀疑SSH服务器没有正确配置为允许以这种方式执行命令(这相当于 ssh myserver show hwid ,而不是在登录后将其键入终端)。
ssh myserver show hwid
您可能希望模拟登录到服务器后键入命令的行为,为此,我认为这是适当的:
shell = ssh_client.invoke_shell() stdin, stdout, stderr = shell.exec_command(command)