Python3.10实现Telnet

2022/5/10 11:04:33

本文主要是介绍Python3.10实现Telnet,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

"""说明:
    调用示例:
        result = telnet_connect(ip_host='地址', ip_port='端口(默认23)', ip_user='用户名', ip_password='密码', ip_command=['命令1', '命令2'])
        print(result)
        建议采用关键字方式传递调用参数。
    调用参数说明:
        telnet_connect(ip_host, [ip_port], ip_user, ip_password, [ip_command[]])
        ip_hont       字符串格式                 必选参数      指定Telnet目的地址;
        ip_port       字符串格式                 可选参数      指定Telnet端口                          默认值23;
        ip_user       字符串格式                 必选参数      指定远程登录用户名;
        ip_password   字符串格式                 必选参数      指定远程登录密码;
        ip_command    列表格式,每个元素为字符串    可选参数      指定登录后执行的命令,每个元素为一条命令      默认由用户接管命令输入
    返回值说明:
        如远程设备无法通过Telnet连接       返回:'远程连接失败!'
        如远程设备登陆失败                返回:'登陆失败!'
        如命令执行成功                   返回:命令执行后的输出结果        执行多条命令时,将在最后一条命令执行完成后一次返回全部结果
        用户操作退出                     返回:'用户退出。'
    其他说明:
        执行命令过程中不会判断命令是否执行成功;
        如命令返回信息不能在一页内显示完,只显示部分内容。
"""
# _*_coding:utf-8_*_


import telnetlib
import time


def telnet_connect(ip_host, ip_user, ip_password, ip_port='23', ip_command=['user_control']):
    try:
        tn = telnetlib.Telnet(host=ip_host, port=ip_port, timeout=1)  # 启动Telnet连接
    except:
        return '远程连接失败!'
    else:
        time.sleep(0.5)  # 等待远端设备输出
        print(tn.read_very_eager().decode())
        tn.read_until(b'Username: ', timeout=0.5)  # 等待提示Username,等待1秒
        tn.write(ip_user.encode() + b"\n")  # 输入用户名
        time.sleep(0.5)  # 等待远端设备输出
        print(tn.read_very_eager().decode())
        tn.read_until(b"Password: ", timeout=0.5)  # 等待提示Password,等待1秒
        tn.write(ip_password.encode() + b"\n")  # 输入密码
        time.sleep(0.5)  # 等待远端设备输出
        print(tn.read_very_eager().decode())
        v_result = tn.read_very_eager().decode()  # 接收登录返回结果
        if 'failed' in v_result:
            return '登录失败!'
        else:
            tel_result = ''
            if ip_command[0] == 'user_control':
                print('现在开始交由用户控制' + "\n")
                # tn.interact()
                while True:
                    time.sleep(0.1)
                    from pip._vendor.distlib.compat import raw_input
                    user_command = raw_input('请输入命令:')
                    time.sleep(0.2)
                    if user_command == 'exit':
                        print('用户退出。')
                        break
                    else:
                        tn.write(user_command.encode() + b"\n")  # 执行每条命令
                        time.sleep(0.5)
                        tel_result = (tn.read_very_eager().decode() + "\n")  # 输出命令执行结果
                        print(tel_result + "\n")  # 输出命令执行结果)
                tn.close()
            else:
                for cmd in ip_command:
                    tn.write(cmd.encode() + b"\n")  # 执行每条命令
                    time.sleep(1)
                    tel_result = tel_result + tn.read_very_eager().decode() + "\n"
                tn.close()
                return tel_result
# print(telnet_connect(ip_host='1.1.1.1', ip_user='admin', ip_password='admin'))


这篇关于Python3.10实现Telnet的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程