python - SSH 依赖会话 - Python Paramiko
问题描述
想知道是否有人做过,还是我要在这里发现热水。下面的脚本是我执行命令的基础(现在在一个设备上,因为我不知道如何传递多个),但我需要使它成为一个依赖会话。本质上,我有一个跳转主机,通过它可以进行到我的终端设备的 SSH。SSH 跳转主机,并从它 SSH 到终端设备。想法?
#Importing modules
import paramiko
import sys
import time
#setting parameters like host IP, username, passwd and number of iterations to gather cmds
HOST = "1.1.1.1"
USER = "admin"
PASS = "passwd"
ITERATION = 3
#A function that logins and execute commands
def fn():
client1=paramiko.SSHClient()
#Add missing client key
client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#connect to switch
client1.connect(HOST,username=USER,password=PASS)
print "SSH connection to %s established" %HOST
#Gather commands and read the output from stdout
stdin, stdout, stderr = client1.exec_command('show version\n')
print stdout.read()
stdin, stdout, stderr = client1.exec_command('show alarms | no-more\n')
print stdout.read()
stdin, stdout, stderr = client1.exec_command( 'show processes memory | no-more\n')
print stdout.read()
client1.close()
print "Logged out of device %s" %HOST
#for loop to call above fn x times. Here x is set to 3
for x in xrange(ITERATION):
fn()
print "%s Iteration/s completed" %(x+1)
print "********"
time.sleep(5) #sleep for 5 seconds
解决方案
推荐阅读
- javascript - 时间错了
- https - 如何解释 Cloudflare 的 SVCB/HTTPS DNS 记录类型?
- swift - SwiftUI iOS14 Widget - 使用相同的 WidgetFamily 创建多布局
- derby - 我的删除按钮按钮不起作用无效光标状态 - 没有当前行
- javascript - TypeError: undefined is not an object (evaluate '_useContext.dispatch') when using context in react native
- spring - Spring Security:从数据库表中获取当前用户的字段
- fonts - 新博客界面中的字体问题
- javascript - 如何检查 JavaScript/Flask 中是否存在多个元素
- javascript - 如何根据提示输入数据制作数组?
- visual-studio - 如何在 Visual Studio 中使用“//”而不是“/*”来注释多行