Created
August 3, 2021 17:03
-
-
Save Tapia641/2554692b53825f3a14a7a36e5a760afd to your computer and use it in GitHub Desktop.
Paramiko double ssh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas | |
from vspk import v5_0 as vsdk | |
import paramiko | |
import time | |
import re | |
from sshtunnel import SSHTunnelForwarder | |
from socket import * | |
# Lab | |
jump_host = {'name': 'jump-vm-namer', 'host': 'HOST-IP', 'port': HOST-IP, 'user': 'user', 'password': 'password'} | |
hosts = [ | |
{'name': 'name1', 'host': 'IP', 'port': PORT, 'user': 'user', 'password': 'pass'}, | |
{'name': 'name2', 'host': 'IP', 'port': PORT, 'user': 'user', 'password': 'pass'}, | |
{'name': 'name3', 'host': 'IP', 'port': PORT, 'user': 'user', 'password': 'pass'}, | |
{'name': 'name4', 'host': 'IP', 'port': PORT, 'user': 'user', 'password': 'pass'} | |
] | |
def check_uptime(host, port, user, password, sock): | |
client = paramiko.SSHClient() | |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
client.connect(hostname=host, port=port, | |
username=user, password=password, | |
look_for_keys=False, allow_agent=False, sock=sock) | |
shell = client.invoke_shell() | |
output = '' | |
if shell.send("show uptime\r\n") > 0: | |
time.sleep(5) | |
data = shell.recv(60000) | |
output += str(data.decode()) | |
output_lines = output.strip().split("\r\n") | |
output_lines = [x.strip() for x in output_lines] | |
shell.send('logout') | |
client.close() | |
return output_lines | |
# Configurations of JUMP VM | |
jumpbox=paramiko.SSHClient() | |
jumpbox.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
jumpbox.connect(jump_host['host'], username=jump_host['user'], password=jump_host['password']) | |
# Each host | |
for _vsc in hosts: | |
jumpbox_transport = jumpbox.get_transport() | |
src_addr = ('localhost', 22) # local inside of the jump vm | |
dest_addr = (_vsc['host'], _vsc['port']) # destination of the second ssh | |
jumpbox_channel = jumpbox_transport.open_channel("direct-tcpip", dest_addr, src_addr) | |
output = check_uptime(_vsc['host'], _vsc['port'], _vsc['user'], _vsc['password'], jumpbox_channel) | |
print(_vsc['name'], output) | |
jumpbox.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment