简体   繁体   English

SSH 依赖 session - Python Paramiko

[英]SSH Dependant session - Python Paramiko

Wondering if anyone has done it, or am I to descover the hot water here.想知道是否有人做过,还是我要在这里发现热水。 The below script is my base for executing commands ( for now on one device cause I don`t know how to pass multiple ), but I need to make it a dependant session.下面的脚本是我执行命令的基础(现在在一个设备上,因为我不知道如何传递多个),但我需要让它成为一个依赖的 session。 Essentially I have a jump host through which the SSH to my end device should occur.本质上,我有一个跳转主机,通过它可以将 SSH 连接到我的终端设备。 SSH to jump host, and from it SSH to end device(s). SSH 跳转到主机,然后从它 SSH 到终端设备。 Ideas?想法?

#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

You could use direct-tcpip channel to create a "socket", which you could then give to paramiko.SSHClient :您可以使用direct-tcpip通道创建一个“套接字”,然后您可以将其提供给paramiko.SSHClient

proxy = client1.get_transport().open_channel('direct-tcpip',
                                             dest_addr=(dest_ip, dest_port),
                                             src_addr=('localhost', 0))
client2 = paramiko.SSHClient()
client2.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client2.connect(username='user', password='pass', sock=proxy)

See paramiko.Transport.open_channel docs . 请参阅paramiko.Transport.open_channel文档

For that to work though, the server that the client1 is connected to must allow opening of that kind of a channel.但是,要使其正常工作, client1所连接的服务器必须允许打开这种通道。 Most do, but in OpenSSH you can have it disabled for "security reasons" (which has been shown time and time again to provide no additional security at all, but it's possible).大多数都可以,但是在 OpenSSH 中,您可以出于“安全原因”将其禁用(这已一次又一次地显示为根本不提供额外的安全性,但这是可能的)。

In case your server has this option disabled, you could use any jumphost's process, that has an ability to talk to the target server via TCP, eg Netcat.如果您的服务器禁用了此选项,您可以使用任何能够通过 TCP 与目标服务器通信的跳转主机进程,例如 Netcat。 See my self-answered question for the code of that . 有关 that 的代码,请参阅我的自我回答问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM