Python中专门提供了telnetlib库,用来完成基于telnet协议的通信功能。
python3下使用telnetlib模块连接网络设备经常会遇到字节与字符不匹配的问题
问题提示如下:
import telnetlib Host = "10.10.10.10" # 连接Telnet服务器 tn = telnetlib.Telnet(Host, port=23, timeout=10) tn.set_debuglevel(0) # 输入登录用户名 tn.read_until(b'login: ') tn.write(b"admin" + b'\n') # 输入登录密码 tn.read_until(b'Password: ') tn.write(b"Admin@1234" + b'\n') tn.read_until(b'#') tn.write(b"cd /home/sd" + b'\n') tn.read_until(b'#') tn.write(b"ls -al" + b'\n') r = tn.read_until(b'#').decode('ASCII') r1 = r.split(r"\r\n") for i in r1: print(i) tn.close()
以下是设备实例:
>>> tn=telnetlib.Telnet("10.10.0.6",timeout=2) >>> tn.read_until(b'login: ',timeout=2) b"\r\n****************************************************************** ****\r\n* Copyright (c) 2004-2018 New H3C Technologies Co., Ltd. All rig rved.*\r\n* Without the owner's prior written consent, *\r\n* no decompiling or reverse-engineering shall be allowed. *\r\n********************************************************** ************\r\n\r\nlogin: " >>> tn.write(b'admin'+b'\n') >>> tn.read_until(b'Password: ',timeout=2) b'jgtl\r\r\nPassword: ' >>> tn.write(b'Admin@123'+b'\n') >>> tn.read_until(b'>') b'\r\n<bangong-01>' >>> tn.write(b'ping 10.10.0.7') >>> tn.read_until(b'>')
以上是命令行执行的过程。写成脚本需要考虑两个问题,一个是变量的替换如何编码解封,一个是输出结果加解码
#-*- coding:utf-8 -*- import telnetlib import re import csv import sys import time from datetime import datetime host_dict={ "ip":"10.10.0.6", "user":"admin", "pwd":"Admin@123" } def get_loss(addrlist): host=host_dict["ip"] user=host_dict["user"] pwd=host_dict["pwd"] print (host) resultlist = [] #try: tn = telnetlib.Telnet(host, timeout=2) print ("AA") if len(host_dict["pwd"]) and len(host_dict["user"]): print ("BB") tn.read_until(b"login: ", timeout=3) #tn.write(b"admin"+b"\n") tn.write(user.encode()+b"\n") tn.read_until(b"Password: ", timeout=3) #tn.write(b"Admin@123"+b"\n") tn.write(pwd.encode()+ b"\n") # p_error = re.compile("found at") if tn.read_until(b">", timeout=4).find(b">") != -1: print("Connect to {host} ...... ".format(host=host)) tn.write(b"ping 127.0.0.1\n") print (tn.read_until(b'01>')) else: print("%s Wrong username or password!!!" % host) return "" #tn.read_until(b">") if len(addrlist) != 0: for i in range(len(addrlist)-1): tep = {} command = "ping " + addrlist[i] print("command:", command) tn.write(command.encode() + b"\n") result = str(tn.read_until(b"01>")) print(result) re_loss = re.compile("\d+\.\d+%") loss = re_loss.findall(result) tep[host] = loss[0] resultlist.append(tep) #if p_error.search(result.decode()): # print("There is a error in this command: {0}".format(c.decode())) tn.close() #except Exception as e: #if e: # print ("Connect to {host} Failed!!!".format(host=host),e) #return "" return resultlist if __name__=="__main__": addrlist=['10.10.0.2','10.10.0.5'] print ("get_loss",get_loss(addrlist))
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
本文向大家介绍Python如何使用paramiko模块连接linux,包括了Python如何使用paramiko模块连接linux的使用技巧和注意事项,需要的朋友参考一下 python程序需要连接linux时,需要使用密码或者秘钥验证以登录os进行命令操作或者文件传输,python中实现此功能的模块为paramiko;下面是该模块的基础用法 下面是通过密码进行linux登录执行命令和文件传输示例
问题内容: 我想捕获某些NIC收到的帧。从中提取一些信息(当前我需要捕获源MAC和源IP地址);将这些信息保存在一些公共数据结构中;并让框架向上移动到TCP / IP堆栈。 我以前使用过Netfilter,但显然它不提供Link层挂钩。 有什么办法可以做到吗? 我将其编写为内核模块;运行Linux内核2.6.32 问题答案: 实际上,Netfilter应该可以正常工作,因为它接收了整个数据包(内部
问题内容: 通常的问题是如何模拟(作为一组测试用例的一部分)缺乏网络连接,因为这是某些测试用例中的重要考虑因素。有没有一种方法可以通过Java API(或通过JVM选项)来执行,以便某些测试用例可以在网络断开的情况下运行?(模拟还是真实?)。 更具体的情况(如果没有普遍适用的解决方案)是我正在做大量的XML文件处理(包括XSD验证),并且我需要确保没有从网络上获取任何内容,特别是属性值(提示)未使
我的MySQL模块有一些问题,我的本地JBoss。 在$jboss_home中,我创建了com/mysql/main,并删除了mysql-connector-java-5.1.26-bin.jar和module.xml 然后配置数据源。 NetBeans:7.3.1;Mysql连接器:5.1.26;应用服务器:JBoss EAP 6.1.0;MySQL:5.6.14;Windows 7;Java
问题内容: 我最近一直在研究OSGi,认为对于模块化Java应用程序来说,这似乎是一个非常不错的主意。 但是,我想知道OSGi如何在Web应用程序中工作,而您不仅仅需要担心代码-HTML,图像,CSS之类的事情。 在工作中,我们正在构建一个具有多个“标签”的应用程序,每个标签都是应用程序的一部分。我认为这可以从采用OSGi的方法中真正受益- 但是我真的不确定什么是处理所有常规Web应用程序资源的最
问题内容: 我在通过JSch创建到远程SFTP服务器的会话时遇到问题: 我用来通过外壳连接sftp服务器的命令是: 及其正常工作,但是当我尝试使用Java(JSch)时,我遇到了超时异常。Java代码是 我得到的异常是: com.jcraft.jsch.JSchException:超时:未建立套接字 我想它来了,因为我没有在使用shell命令时设置我正在使用的with 选项。如果是这样,那么如何在