# -*- coding: utf-8 -*-
import logging
import telnetlib
import time
import sys
import os host_ip = '10.63.194.1'
username = 'ROOT'
password = '123@'
telnet_client = []
curPath = os.path.dirname(os.path.abspath(sys.argv[0]))
def getFileTime():
curTime = time.strftime('%Y%m%d_%H%M%S',time.localtime(time.time()))
return curTime #打印中文要txt打开,或改格式,把打印改为英文
logPath = curPath + '\\..\\log\\' + getFileTime() +'.log'
print(logPath)
logging.basicConfig(level = logging.DEBUG,
format='[%(asctime)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S,%a',
filename = logPath,
filemode='w')
class TelnetClient():
def __init__(self,):
self.tn = telnetlib.Telnet()
# 此函数实现telnet登录主机
def login_host(self,host_ip,username,password):
try:
# self.tn = telnetlib.Telnet(host_ip,port=23)
self.tn.open(host_ip,port=23)
except:
logging.warning('%s failed to connect !'%host_ip)
return False
# 等待login出现后输入用户名,最多等待10秒
self.tn.read_until(b'Username:',timeout=10)
self.tn.write(username.encode('ascii') + b'\n')
# 等待Password出现后输入用户名,最多等待10秒
self.tn.read_until(b'Password:',timeout=10)
self.tn.write(password.encode('ascii') + b'\n')
# 延时两秒再收取返回结果,给服务端足够响应时间
time.sleep(2)
# 获取登录结果
# read_very_eager()获取到的是的是上次获取之后本次获取之前的所有输出
command_result = self.tn.read_very_eager().decode('ascii')
if 'error' not in command_result:
logging.warning('%s connected ssuccess !'%host_ip)
return True
else:
logging.warning('%s failed to login,username or password error !'%host_ip)
return False
# 此函数实现执行传过来的命令,并输出其执行结果
def execute_some_command(self,command):
# 执行命令
self.tn.write(command.encode('ascii')+b'\n')
time.sleep(2)
# 获取命令结果
command_result = self.tn.read_very_eager().decode('ascii')
logging.debug('%s' % command_result)
# 退出telnet
def logout_host(self):
self.tn.write(b"exit\n")
def writeC(command):
telnet_client.execute_some_command(command)
def inputCommand():
cont = 'con t' aclNum = ' 300'
writeC(cont)
writeC(aclNum)
for index in range(255):
index =index + 1
writeC('no rule ' + str(index))
if __name__ == '__main__':
telnet_client = TelnetClient()
# 如果登录结果返加True,则执行命令,然后退出
if telnet_client.login_host(host_ip,username,password):
inputCommand()
telnet_client.logout_host()

转载:http://www.cnblogs.com/lsdb/p/9258964.html  

【转】python实现Telnet操作的更多相关文章

  1. Python实现telnet命令测试防火墙

    Python实现telnet命令测试防火墙 telnet主要用于测试主机端口是否开通 ping主要是用来测试网络是否畅通和主机是否正在使用 使用Python实现Telnet测试主机端口是否开通的功能. ...

  2. python高级之操作数据库

    python高级之操作数据库 本节内容 pymysql介绍及安装 使用pymysql执行sql 获取新建数据自增ID fetch数据类型设置 1.pymysql介绍及安装 在python2中连接数据库 ...

  3. Python 文件常见操作

    # -*-coding:utf8 -*- ''''' Python常见文件操作示例 os.path 模块中的路径名访问函数 分隔 basename() 去掉目录路径, 返回文件名 dirname()  ...

  4. Python :open文件操作,配合read()使用!

    python:open/文件操作 open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) 如何打开文件 handle=open(file_name,a ...

  5. 第九篇:python高级之操作数据库

    python高级之操作数据库   python高级之操作数据库 本节内容 pymysql介绍及安装 使用pymysql执行sql 获取新建数据自增ID fetch数据类型设置 1.pymysql介绍及 ...

  6. (转载)Python 列表(list)操作

    (转载)http://blog.csdn.net/facevoid/article/details/5338048 创建列表sample_list = ['a',1,('a','b')] Python ...

  7. 使用telnet操作memcache,一般不常用

    使用telnet操作memcache,一般不常用   1.使用方法 1. 连接到memcached telnet 192.168.1.100 11211 add name 0 60 5     [说明 ...

  8. python之数据库操作(sqlite)

    python之数据库操作(sqlite) 不像常见的客户端/服务器结构范例,SQLite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的一个主要部分.所以主要的通信协议是在编程语言内的直接A ...

  9. python通过openpyxl操作excel

    python 对Excel操作常用的主要有xlwt.xlrd.openpyxl ,前者xlwt主要适合于对后缀为xls比较进行写入,而openpyxl主要是针对于Excel 2007 以上版本进行操作 ...

随机推荐

  1. Winform中设置和获取DevExpress的RadioGroup的选中项的value值

    场景 Winform中实现读取xml配置文件并动态配置ZedGraph的RadioGroup的选项: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article ...

  2. Effective Python 编写高质量Python代码的59个有效方法

    Effective Python 编写高质量Python代码的59个有效方法

  3. vue学习指南:第六篇(详细) - Vue的组件 component

    1. 什么是组件?有两种解释 1. 第一种解释: 什么是组件? 1. 组件是 vue 中的一个可复用的实例,所以new Vue() 是vue中最大的那个组件(根组件),有名字,使用的时候以单标签或双标 ...

  4. 【学习笔记】兄弟连LINUX视屏教程(沈超 李明)

    发现自己的linux水平楞个瓜皮,找个视屏教程学习一哈 1 linux系统简介 1.1 UNIX和Linux发展史 unix发展历史:1969年,美国贝尔实验室的肯.汤普森开发出unix系统,1971 ...

  5. Linux命令——ethtool

    转自:https://www.cnblogs.com/kelamoyujuzhen/p/10116423.html 参考:9 Linux ethtool Examples to Manipulate ...

  6. AssetBundleMaster_Introduce_EN

    This is an integrated solution for building AssetBundles and loading Assets. what it can do is about ...

  7. conan使用(二)--创建私有仓库

    前面我们已经能够使用conan来从公共服务器上拉取C/C++包来集成进我的工程中,但是在实际开发中,我们可能需要自己封装或使用非公开的库,那么自己搭建一个私服是个很现实的需求. 搭建conan私服有几 ...

  8. c# 第19节 Arraylist数组

    本节内容: 1:ArrayList是什么 2:ArrayList数组的添加 3:ArrayList的方法 4:ArrayList 的删除 4:ArrayList 的遍历与查找 1:ArrayList是 ...

  9. SpringBoot设置支持跨域请求

    跨域:现代浏览器出全的考虑,在http/https请求时必须遵守同源策略,否则即使跨域的http/https 请求,默认情况下是被禁止的,ip(域名)不同.或者端口不同.协议不同(比如http.htt ...

  10. 重新学习SpringMVC——基础

    2. SpringMVC_HelloWorld3. SpringMVC_RequestMapping_修饰类4. SpringMVC_RequestMapping_请求方式5. SpringMVC_R ...