[英]Python paramiko module using multiple commands
我有一个创建连接的类。 我可以在通道关闭之前连接并执行1命令。 在另一个系统上,我可以执行多个命令,并且通道不会关闭。 显然它是我尝试连接的系统的配置问题。
class connect:
newconnection = ''
def __init__(self,username,password):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect('somehost', username=username,password=password,port=2222,timeout=5)
except:
print "Count not connect"
sys.exit()
self.newconnection = ssh
def con(self):
return self.newconnection
然后我使用'ls'命令打印一些输出
sshconnection = connect('someuser','somepassword').con()
stdin, stdout, stderr = sshconnection.exec_command("ls -lsa")
print stdout.readlines()
print stdout
stdin, stdout, stderr = sshconnection.exec_command("ls -lsa")
print stdout.readlines()
print stdout
sshconnection.close()
sys.exit()
在第一个exec_command运行后,它将打印目录列表的预期输出。 当我在第一个exec_command之后打印stdout时,它看起来像是关闭了通道
<paramiko.ChannelFile from <paramiko.Channel 1 (closed) -> <paramiko.Transport at 0x2400f10L (cipher aes128-ctr, 128 bits) (active; 0 open channel(s))>>>
就像我在另一个系统上说的那样,我能够继续运行命令并且连接不会关闭。 有没有办法让我保持开放? 或者更好的方式,我可以看到它关闭的原因?
编辑 :所以看起来每个SSHClient.exec_command只能运行1个命令...所以我决定使用get_transport()。open_session()然后运行一个命令。 第一个总是有效的。 第二个总是失败,脚本只是挂起
在exec_command
执行后只有paramiko
,关闭通道并且ssh
返回auth提示符。
似乎只有paramiko
,尝试fabric
或其他工具是paramiko
。
** fabric
也没用 。
请参阅下面的参考资料,因为它提供了一种在Paramiko中执行此操作的方法:
netmiko(在Windows上测试)是可能的。 此示例是为连接到cisco设备而编写的,但其原理也适用于其他设备。
import netmiko
from netmiko import ConnectHandler
import json
def connect_enable_silent(ip_address,ios_command):
with open ("credentials.txt") as line:
line_1 = json.load(line)
for k,v in line_1.items():
router=(k,v)
try:
ssh = ConnectHandler(**router[1],device_type="cisco_ios",ip=ip_address)
ssh.enable()
except netmiko.ssh_exception.NetMikoAuthenticationException:
#incorrect credentials
continue
except netmiko.ssh_exception.NetMikoTimeoutException:
#oddly enough if it can log in but not able to authenticate to enable mode the ssh.enable() command does not give an authentication error
#but a time-out error instead
try:
ssh = ConnectHandler(username = router[1]['username'],password = router[1]['password'],device_type="cisco_ios", ip=ip_address)
except netmiko.ssh_exception.NetMikoTimeoutException:
# connection timed out (ssh not enabled on device, try telnet)
continue
except Exception:
continue
else:
output = ssh.send_command(ios_command)
ssh.disconnect()
if "at '^' marker." in output:
#trying to run a command that requires enble mode but not authenticated to enable mode
continue
return output
except Exception:
continue
else:
output = ssh.send_command(ios_command)
ssh.disconnect()
return output
output = connect_enable_silent(ip_address,ios_command)
for line in output.split('\n'):
print(line)
凭据文本用于存储不同的凭据,以防您计划调用此功能来访问多个设备,而不是所有设备都使用相同的凭据。 它的格式为:
{"credentials_1":{"username":"username_1","password":"password_1","secret":"secret_1"},
"credentials_2":{"username":"username_2","password":"password_2","secret":"secret_2"},
"credentials_3": {"username": "username_3", "password": "password_3"}
}
可以更改异常以执行不同的操作,在我的情况下,我只需要它不返回错误并继续尝试下一组,这就是为什么大多数异常都被静音的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.