【转】python实现Telnet操作
# -*- 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操作的更多相关文章
- Python实现telnet命令测试防火墙
Python实现telnet命令测试防火墙 telnet主要用于测试主机端口是否开通 ping主要是用来测试网络是否畅通和主机是否正在使用 使用Python实现Telnet测试主机端口是否开通的功能. ...
- python高级之操作数据库
python高级之操作数据库 本节内容 pymysql介绍及安装 使用pymysql执行sql 获取新建数据自增ID fetch数据类型设置 1.pymysql介绍及安装 在python2中连接数据库 ...
- Python 文件常见操作
# -*-coding:utf8 -*- ''''' Python常见文件操作示例 os.path 模块中的路径名访问函数 分隔 basename() 去掉目录路径, 返回文件名 dirname() ...
- Python :open文件操作,配合read()使用!
python:open/文件操作 open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) 如何打开文件 handle=open(file_name,a ...
- 第九篇:python高级之操作数据库
python高级之操作数据库 python高级之操作数据库 本节内容 pymysql介绍及安装 使用pymysql执行sql 获取新建数据自增ID fetch数据类型设置 1.pymysql介绍及 ...
- (转载)Python 列表(list)操作
(转载)http://blog.csdn.net/facevoid/article/details/5338048 创建列表sample_list = ['a',1,('a','b')] Python ...
- 使用telnet操作memcache,一般不常用
使用telnet操作memcache,一般不常用 1.使用方法 1. 连接到memcached telnet 192.168.1.100 11211 add name 0 60 5 [说明 ...
- python之数据库操作(sqlite)
python之数据库操作(sqlite) 不像常见的客户端/服务器结构范例,SQLite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的一个主要部分.所以主要的通信协议是在编程语言内的直接A ...
- python通过openpyxl操作excel
python 对Excel操作常用的主要有xlwt.xlrd.openpyxl ,前者xlwt主要适合于对后缀为xls比较进行写入,而openpyxl主要是针对于Excel 2007 以上版本进行操作 ...
随机推荐
- Asp.Net中Global报错,关键字也不变色问题
原因是我把Global名字改了,使用默认名字就好了
- django中视图函数中装饰器
方法一 给指定方法加 from django.utils.decorators import method_decorator class xx(View): @method_decorator(装饰 ...
- Linux搭建www,mail,ftp三大DNS服务器
##############################-----服务器端----###############################1. 安装bind# yum install bin ...
- C# 获取社会统一信用代码
时间不多,废话少说: 网络请求代码如下: using System; using System.Collections.Generic; using System.Linq; using System ...
- maven 学习---Eclispe IDE集成Maven
Eclipse提供了一个很好的插件m2eclipse 无缝将Maven和Eclipse集成在一起. m2eclipse一些特点如下 您可以从Eclipse运行Maven目标. 可以使用其自己的控制台查 ...
- LeetCode——Department Highest Salary(花式使用IN以及GROUP BY)
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- [20190509]rman备份的疑问5.txt
[20190509]rman备份的疑问5.txt --//别人跟我提到的rman备份问题,我开始以为是assm与mssm的问题,实际测试情况不是.--//开始备份时生成的备份集文件很大,以后会回缩(对 ...
- Python 报错 MySQLdb._exceptions.OperationalError: (2059, )
Python连接MySQL数据时:报错提示MySQLdb._exceptions.OperationalError: (2059, <NULL>). Python包: mysqlclien ...
- Python从零开始——函数
一:Python函数知识概览 二:函数的定义与调用 三:参数传递 四:函数返回值 五:匿名函数的定义与调用 六:变量作用域与改变变量作用域
- P5008 [yLOI2018] 锦鲤抄(Tarjan+贪心)
洛谷 题意: 给出一个有向图,每次可以删除存在入度的点及其出边,每次删除一个点可以获得其权值. 问最终能够获得的最大权值为多少. 思路: 考虑DAG:我们直接倒着拓扑序来选,即可将所有入度不为\(0\ ...