Telnet登陆网络设备执行命令脚本
# !/usr//bin/python3
# -*- coding:UTF-8 -*- from telnetlib import Telnet
import time, os, datetime, json
import logging class TelnetClient(): def __init__(self):
self.tn = Telnet() def __login_host(self, ip, username, password, enable=None, verbose=True):
try:
self.tn.open(ip, port=23)
except:
logging.warning('%s网络连接失败' % ip)
return False
self.tn.read_until(b'Username:', timeout=1)
self.tn.write(b'\n')
self.tn.write(username.encode() + b'\n')
rely = self.tn.expect([], timeout=1)[2].decode().strip() # 读显
if verbose:
print(rely)
self.tn.read_until(b'Password:', timeout=1)
self.tn.write(password.encode() + b'\n')
rely = self.tn.expect([], timeout=1)[2].decode().strip()
if verbose:
print(rely)
if enable is not None:
self.tn.write(b'enable\n')
self.tn.write(enable.encode() + b'\n')
if verbose:
rely = self.tn.expect([], timeout=1)[2].decode().strip()
print(rely)
time.sleep(1)
time.sleep(2)
rely = self.tn.read_very_eager().decode()
if 'Login invalid' not in rely:
logging.warning('%s登陆成功' % ip)
return True
else:
logging.warning('%s登陆失败,用户名或密码错误' % ip)
return False def __do_cmd(self, cmd, location=None):
if location:
os.system('mkdir backup\\{}'.format(location))
with open('backup/{}/configuration_{}.txt'.format(location, datetime.date.today()), 'w') as f:
self.tn.write(cmd.encode().strip() + b'\n')
time.sleep(3)
rescmd = self.tn.read_very_eager().decode()
f.write(rescmd)
logging.warning('命令执行结果:\n %s' % rescmd)
return True
else:
os.system('mkdir backup')
with open('backup/configuration_{}.txt'.format(location, datetime.date.today()), 'w') as f:
self.tn.write(cmd.encode().strip() + b'\n')
time.sleep(3)
rescmd = self.tn.read_very_eager().decode()
f.write(rescmd)
logging.warning('命令执行结果:\n %s' % rescmd)
return True def telnet_login(self, ip, user, pwd, enable=None, cmd=None, location=None):
res1 = self.__login_host(ip, user, pwd, enable)
if res1:
for i in cmd.split(';'):
self.__do_cmd(i, location)
self.__logout_host()
return True
else:
self.__logout_host()
return False def __logout_host(self):
self.tn.close() if __name__ == '__main__':
username = 'cisco' # 用户名
password = 'cisco' # 密码
# 存放设备登陆信息和要执行的命令(多条命令用;隔开),示例:["ip", "username", "password", "enable_password", "terminal length 0;show run", "backup_path"]
lists = 'list.text'
with open('test.txt', 'rt', encoding='utf-8') as list_obj:
# textline = list_obj.readline()
# while textline != '':
# print(json.loads(textline))
# textline = list_obj.readline()
telnet_client = TelnetClient()
for line in list_obj:
hostmsg = json.loads(line)
telnet_client.telnet_login(hostmsg[0], hostmsg[1], hostmsg[2], hostmsg[3], hostmsg[4])
常见网络设备命令结果全部显示
<H3C> screen-length disable # no screen-length disable (华三)
Cisco# terminal length 0 # terminal no length (思科)
MaiPu# more off # more on (迈普)
<HuaWei> screen-length 0 temporary # undo screen-length temporary (华为)
RuiJie# terminal length 0 # terminal no length (锐捷)
ZTE# terminal length 0 # no terminal length (中兴)
常见网络设备巡检命令
https://blog.csdn.net/qq_46417230/article/details/122336116
Telnet登陆网络设备执行命令脚本的更多相关文章
- expect实现远程主机自动执行命令脚本
2014年第一个脚本,哈哈!!! expect实现远程主机自动执行命令脚本: #!/usr/bin/expect -- if { [llength $argv] < 4 } { puts &qu ...
- Python-SSH批量登陆并执行命令
Python-SSH批量登陆并执行命令 #!/usr/bin/env python #-*- coding:utf-8 -*- import paramiko from time import cti ...
- Shell下通过echo+telnet在远端执行命令
创建脚本cmd.sh,用于输入telnet的用户与密码,以及生成远端需要执行的命令 执行命令 MY_SIGN=/tmp/sign; (sh cmd.sh ) | (telnet localhost ...
- webshell下执行命令脚本汇集
cmd1.asp <object runat=server id=shell scope=page classid="clsid:72C24DD5-D70A-438B-8A42-984 ...
- shell中使用expect命令进行远程执行命令脚本
expect是用来实现自动交互功能的工具之一,使用expect-send来实现交互过程. 注意: 1.脚本的执行方法与bash shell不一样,比如:expect example.sh 2.向一个脚 ...
- ssh登陆并执行命令不退出
如果希望SSH登陆后先执行shell命令,可以这样: ssh user@ip -t "cd /data ; /bin/bash"
- Mysq登陆后执行命令提示You must SET PASSWORD before executing this statement
mysql 安装完成后,在输入命令行时,提示:You must SET PASSWORD before executing this statement 提示必须设置密码,我想不是已经设置了密码吗? ...
- ssh 免交互登录 ,远程执行命令脚本。
##免交互SSH登录auto_login_ssh () { expect -c "set timeout -1; spawn -noecho ssh -o ...
- linux关机时候执行命令脚本或程序
Write a service file and place it in /etc/systemd/system/beforeshuttingdown.service code: [Unit] Des ...
- 使用paramiko远程登录并执行命令脚本
#!/usr/bin/env python #coding=utf-8 import paramiko, getpass,sys,traceback class ssh_utils(): def lo ...
随机推荐
- 腾讯云对象存储 COS搭建个人网站
腾讯云对象存储 COS搭建个人网站,简单易操作,方便快捷. 只需要将你的网站资源上传即可,然后设置上你的自定义 CDN 加速域名,一个个人网站就上线啦!当然,你也可以不用设置自定义 CDN 加速域 ...
- 线程join detach 僵尸线程
进程死亡后,由父进程负责回收PCB资源,不回收则会出现僵尸进程 对于线程来说,任何一个线程都可以回收另一个线程的资源 在子线程终止后,通常在主线程中通过pthread_join来回收子线程的资源,获取 ...
- WGCMS 奇迹网站系统 介绍[V2023.2.2]
智鹏网站系统,请勿用作非法用途 权利和义务: 程序仅限学习技术使用,未经官方许可不得用于商业! 程序售价500元一套,绑定域名,不限制端口.如绑定:xx.com,则www.xx.com.mu.xx.c ...
- [转载]python跨文件使用全局变量的实现
python跨文件使用全局变量的实现 更新时间:2022-10-25 14:46:38发布时间:602天前 朗读 Python 定义了全局变量的特性,使用global 关键字修饰 1 global k ...
- 4. Lighting 窗口
Lighting 实现烘焙或者实时渲染都在这里设置,其他灯光或者反射探头的作用相当于允许 Lighting (窗口)烘焙或者实时渲染. 0bject: Lightmap Static: 把烘焙的对象设 ...
- axios 进行同步请求(async+await+promise)
axios 进行同步请求(async+await+promise) 遇到的问题介绍 将axios的异步请求改为同步请求想到了async 和await.Promise axios介绍 Axios 是一个 ...
- java学习日记20230301-API文档
JAVA API java application programming interinterface 应用程序编程接口,是java提供的基本编程接口 在线文档:https://www.matoo ...
- 时间戳转换为yyyy-MM-dd格式
原文链接https://blog.csdn.net/hu104160112/article/details/111167033
- Ubuntu磁盘查看分区和挂载
1.查看硬盘 sudo lshw -c disk 查看有多少个硬盘,一般会显示 disk:0(设备名为 /dev/vda) disk:1 (设备名为 /dev/vdb) 2.查看分区 sudo fdi ...
- CF1268B题解
CF1268B 题解 题目翻译 给你一个杨表,用一个有 \(n\) 个元素的数组 \(a\) 表示杨表每一列的高度.你需要用 \(1 \times 2\) 或 \(2 \times 1\) 的骨牌填充 ...