python使用paramiko模块通过ssh2协议对交换机进行配置的方法
作者:一路湘北 时间:2022-05-16 03:03:17
该代码用的是paramiko模块,python版本是python2.7
下面上源码
# -*- coding: utf-8 -*-
import paramiko
import time
import os
port = '22'
username = '****'
password = '****'
ip = '****' # 测试用的交换机ip
msg1flag = 0
mycmd1flag = 0
# 核心方法,该方法连接远程主机并打开一个终端,并将该终端返回
def msg1(ip,mport,musername,mpassword,mflag):
try:
# 设置ssh连接的远程主机地址和端口
t = paramiko.Transport(ip, mport)
# 设置登录名和密码
t.connect(username=musername, password=mpassword)
# 连接成功后打开一个channel
chan = t.open_session()
# 设置会话超时时间
chan.settimeout(timeout=180)
# 打开远程的terminal
chan.get_pty()
# 激活terminal
chan.invoke_shell()
return chan
except Exception,e:
mflag += 1
time.sleep(5)
if mflag < 3:
msg1(ip,mport,musername,mpassword,mflag)
# 黄栋淋交换机开启审计专用
def mycmd(chan,my1flag):
try:
chan.send('system' + '\n') # 输入命令
chan.send('****' + '\n') # 输入命令
chan.send('****' + '\n')
chan.send('****' + '\n')
time.sleep(50)
i = 1
while i < 3:
chan.send('\n')
i += 1
time.sleep(2)
result = chan.recv(65535) # 得到命令返回的结果
print result
strlen = len(result) # 得到结果字符串的长度
print strlen
return result
except Exception,e:
# print e
my1flag += 1
time.sleep(5)
if my1flag < 3:
mycmd(chan,my1flag)
nowtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) # 时间
# 测试项
chan_ip_test = msg1(ip,port,username,password,msg1flag)
resu_ip_test = mycmd(chan_ip_test,mycmd1flag)
来源:https://blog.csdn.net/qq_25134989/article/details/78918188
标签:python,paramiko,ssh2,交换机
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL 数据库语句优化的原则
2010-01-20 10:11:00
Python lxml模块安装教程
2021-08-26 22:23:43
网页设计图标使用指南[译]
2009-03-11 21:13:00
![](https://img.aspxhome.com/file/UploadPic/20093/11/01-19s.jpg)
Django 浅谈根据配置生成SQL语句的问题
2023-05-10 07:48:23
优化次数过多的循环
2009-11-12 12:35:00
MySQL最新漏洞分析
2012-07-11 15:41:10
如何用Python一次性下载抖音上音乐
2022-10-31 04:14:09
![](https://img.aspxhome.com/file/2023/2/67412_0s.jpg)
网页设计的色彩思考
2007-10-19 13:30:00
淘宝CSS框架研究(1):Reset CSS(八卦篇)
2009-03-31 12:58:00
SQL Server管理 这些你懂吗?
2011-07-01 13:40:23
MYSQL教程:数据列类型与查询效率
2009-02-27 15:37:00
PHP基于迭代实现文件夹复制、删除、查看大小等操作的方法
2023-09-03 22:21:42
用 JavaScript 解数学题
2010-07-09 13:38:00
asp实现页面延迟运行的两个简单方法
2007-10-16 13:49:00
SQL Server 数据库操作实用技巧锦集
2009-01-20 13:20:00
用Python编写分析Python程序性能的工具的教程
2022-02-13 01:57:03
![](https://img.aspxhome.com/file/2023/3/63553_0s.jpg)
你凭什么说你的网站用户体验好
2011-03-31 17:08:00
HTML语言将加入硬件操作功能
2009-12-16 19:19:00
![](https://img.aspxhome.com/file/UploadPic/200912/16/01-55s.jpg)
透彻掌握ASP分页技术
2009-03-09 18:26:00
python中列表(list)和元组(tuple)的深入讲解
2021-08-18 01:39:02