如何使用 Python 库(例如 Paramiko)与 Telnet 和 SSH 进行链式连接 [英] How do I use Python libs such as Paramiko for chain connections with Telnet and SSH

查看:122
本文介绍了如何使用 Python 库(例如 Paramiko)与 Telnet 和 SSH 进行链式连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于此处提出的问题:使用 python SSH 和 telnet 到本地主机

Similar to a question asked here: SSH and telnet to localhost using python

我正在努力寻找解决以下问题的方法:

I'm trying to find a solution to the following problem:

从服务器 A(完全权限)到 Jumhost B(无 sudo),我想使用 Python 连接到多个网络设备(一个接一个就足够了,不必同时连接).仅使用 SSH 就没有问题,但很多设备只使用 Telnet(我知道这不安全,但我没有决定这样做).

From Server A (full rights) over Jumhost B (no sudo), I want to connect to several Network devices using Python (one after another is enough, it doesn't have to be in the same time). With SSH only this would be no problem but a lot of devices use Telnet only (I know that this isn't secure, but it wasn't my decision to do it like that).

经过研究,我遇到了多种用于链式 SSH 连接的解决方案,例如 Paramiko、Netmiko、Pxssh 等.但是我找不到使用 Telnet 实现最后一步的正确方法.目前我有以下代码:

After research I came across multiple solutions for chain SSH connections, such as Paramiko, Netmiko, Pxssh etc. But I can't find a proper way to achieve the last step with Telnet. Currently I have the following code:

class SSHTool():
def __init__(self, host, user, auth,
             via=None, via_user=None, via_auth=None):
    if via:
        t0 = ssh.Transport(via)
        t0.start_client()
        t0.auth_password(via_user, via_auth)
        # setup forwarding from 127.0.0.1:<free_random_port> to |host|
        channel = t0.open_channel('direct-tcpip', host, ('127.0.0.1', 0))
        self.transport = ssh.Transport(channel)
    else:
        self.transport = ssh.Transport(host)
    self.transport.start_client()
    self.transport.auth_password(user, auth)

def run(self, cmd):
    ch = self.transport.open_session()
    ch.set_combine_stderr(True)
    ch.exec_command(cmd)
    retcode = ch.recv_exit_status()
    buf = ''
    while ch.recv_ready():
        buf += str(ch.recv(1024))

    return (buf, retcode)


host = ('192.168.0.136', 22)
via_host = ('192.168.0.213', 22)

ssht = SSHTool(host, '', '',
via=via_host, via_user='', via_auth='')

output=ssht.run('ls')
print(output)

有了这个,我可以通过我的 Jumphost 链接,但我不知道如何实现 Telnet 连接.有人知道正确的解决方案吗?

With this I am able to chain through my Jumphost, but I don't know how to implement then a Telnet connection. Does anyone know a proper solution?

推荐答案

Telnet 类不能使用channel"类.Telnet 类需要连接到一个主机:端口.因此,您需要开始侦听本地临时端口并将其转发到频道"类.Paramiko forward.py demo 正是为了这个目的:

You cannot use "channel" class with Telnet class. Telnet class needs to connect to a host:port. So you need to start listening on a local temporary port and forward that to "channel" class. There's a ready-made forward_tunnel function in Paramiko forward.py demo exactly for this purpose:

forward_tunnel(local_unique_port, telnet_host, 23, t0)
telnet = Telnet("localhost", local_unique_port)

这篇关于如何使用 Python 库(例如 Paramiko)与 Telnet 和 SSH 进行链式连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆