不知道是谁干的,还是我要在这里除掉热水。下面的脚本是我执行命令的基础(目前在一个设备上,因为我不知道如何传递多个命令),但我需要使它成为一个依赖会话。从本质上说,我有一个跳转主机,通过它可以进行到我的终端设备的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
您可以使用
direct-tcpip
通道创建一个“socket”,然后将其提供给paramiko.SSHClient
:See ^{} docs 。
但是,要使其工作,连接到
client1
的服务器必须允许打开此类通道。大多数是这样的,但是在OpenSSH中,你可以因为“安全原因”而禁用它(一次又一次地显示,它根本不提供额外的安全性,但这是可能的)。如果服务器禁用了此选项,则可以使用任何jumphost进程,该进程能够通过TCP(如Netcat)与目标服务器进行通信。See my self-answered question for the code of that。
相关问题 更多 >
编程相关推荐