我正在尝试使用netmiko(基于paramiko的ssh会话管理器)构建一个函数,并将其作为模块导入。这里是我要导入的功能:
import sys, socket, getpass
from netmiko import ConnectHandler
def sshHost(hostname):
username = raw_input("Username: ")
password = getpass.getpass()
hostIp = socket.gethostbyname(hostname)
sshSession = ConnectHandler(device_type = 'juniper', ip = hostIp, username = username, password = password)
versionHostname = sshSession.send_command("show version | match Hostname:")
print "Session established with ",versionHostname.strip(), "IP: ", hostIp
return sshSession
所以,如果我运行sshHost(sys.argv[1])
,该功能会按预期工作
但是,当我将其作为外部模块导入时,脚本会超时
#!/usr/bin/env python
import sys
from netmiko import ConnectHandler
from sshHostModuleJUNOS import sshHost
def showVersion(host):
out = host.send_command("show version")
print out
showVersion(sshHost(sys.argv[1]))
我很确定使用sshHost模块的方式有问题,但我无法弄清楚是什么
错误paramiko.ssh_exception.SSHException: No existing session