通过python的paramiko模块获取cisco交换机的配置
import paramiko
import time
import os
import threading,Queue class MyExpection(Exception):
pass class ThreadPool(object):
def __init__(self, maxsize):
self.maxsize = maxsize
self._q = Queue.Queue(self.maxsize)
for i in range(self.maxsize):
self._q.put(threading.Thread) def getThread(self):
return self._q.get() def addThread(self):
self._q.put(threading.Thread) def show_conf(remote_session,hostname):
remote_session.send('terminal length 0\n')
time.sleep(0.1)
remote_session.send('show run\n')
time.sleep(1.6)
buff = remote_session.recv(655350)
#print(buff)
file_path = os.path.join(os.path.split(os.path.realpath(__file__))[0],'conf/')
with open(file_path + str(hostname) + '.conf', 'wb') as f:
f.write(buff)
buff = ''
remote_session.send('show vlan\n')
time.sleep(0.2)
buff = remote_session.recv(655350)
file_path = os.path.join(os.path.split(os.path.realpath(__file__))[0],'vlan/')
with open(file_path + str(hostname) + '.vlan', 'wb') as f:
f.write(buff)
os.system('sh sedFile.sh %s'%hostname)
remote_session.send('exit\n')
print('%s is OK'%hostname) def ssh(hostname,username,pw,en_pw,pool):
port = 22
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname, port, username, pw, timeout=5)
remote_session = client.invoke_shell()
time.sleep(0.2)
buff = remote_session.recv(65535).decode()
if buff.endswith('>'):
remote_session.send('en\n')
time.sleep(0.2)
buff = remote_session.recv(65535).decode()
if buff.endswith('Password: '):
remote_session.send('%s\n'%en_pw)
time.sleep(0.2)
buff = remote_session.recv(65535).decode()
if buff.endswith('Password: '):
raise MyExpection('`s enable password is incorrect')
show_conf(remote_session, hostname)
elif buff.endswith('#'):
show_conf(remote_session, hostname)
except MyExpection as e:
print('%s is %s' % (hostname, e))
except Exception as e:
print('%s is %s'%(hostname,e))
pool.addThread() if __name__ == '__main__':
t_list = []
pool = ThreadPool(5)
with open('list.txt','r') as f:
for line in f:
hostname,username,pw,en_pw = line.strip().split('\t')
th = pool.getThread()
t = th(target=ssh, args=(hostname,username,pw,en_pw,pool))
t.start()
t_list.append(t)
for i in t_list:
i.join()
其中在该目录中有conf和vlan的文件夹,并且调用的那个shell如下:主要是为了剪切
#!/bin/bash
#
cd /home/hjc/switch/conf
file1=$1.conf
sed -ni '/Building configuration/,$p' $file1
sed -i '/Building configuration/d' $file1
sed -i '$d' $file1
cd /home/hjc/switch/vlan
file2=$1.vlan
sed -i '/show vlan/d' $file2
sed -i '$d' $file2
而调用的那个list.txt主要是存放ip、user、password、enable_password,
写的很烂,但实现了想要的功能,还算比较开心,后面还会改进可以判断登录的是哪一品牌的交换机,然后做出不同的判断和发送的信息。
通过python的paramiko模块获取cisco交换机的配置的更多相关文章
- (转)python的paramiko模块
python的paramiko模块 原文:http://www.cnblogs.com/breezey/p/6663546.html paramiko是用python语言写的一个模块,遵循S ...
- Python之paramiko模块和SQL连接API
堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...
- python封装configparser模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
- Python之paramiko模块
今天我们来了解一下python的paramiko模块 paramiko是python基于SSH用于远程服务器并执行相应的操作. 我们先在windows下安装paramiko 1.cmd下用pip安装p ...
- 使用python的Paramiko模块登陆SSH
使用python的Paramiko模块登陆SSH paramiko是用Python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. python的paramiko模块 ...
- 利用python 下paramiko模块无密码登录
利用python 下paramiko模块无密码登录 上次我个大家介绍了利用paramiko这个模块,可以模拟ssh登陆远程服务器,并且可以返回执行的命令结果,这次给大家介绍下如何利用已经建立的密钥 ...
- python使用wmi模块获取windows下的系统信息监控系统-乾颐堂
Python用WMI模块获取Windows系统的硬件信息:硬盘分区.使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位置,系统的版本等信息. 本文实例讲述了python使用wmi模块获取w ...
- python的paramiko模块
paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Solaris, BSD, MacOS X, ...
- python的paramiko模块的安装与使用
一:简介 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. 由于使用的是python这样的能够跨平台运行的语言,所以所有python支 ...
随机推荐
- sublime text3 自动编译php 适合用于简单的php文件执行
1.将php路径放入环境变量中 2. 点击 sublime_text的“工具”->"编译系统"->"编译新系统" { "cmd" ...
- Java中静态变量、静态代码块、非静态代码块以及静态方法的加载顺序
在研究单例设计模式的时候,用到了静态变量和静态方法的内容,出于兴趣,这里简单了解一下这四个模块在类初始化的时候的加载顺序. 经过研究发现,它们的加载顺序为: 1.非静态代码块 2.静态变量或者静态代码 ...
- leetcode762
class Solution { public: bool IsPrime(int n) { ) { return false; } || n == ) { return true; } ; i &l ...
- 使用axis2进行WebService的开发
使用axis2进行WebService的开发 Apache Axis2 是 Apache Axis SOAP 项目的后继项目.此项目是 Web 服务核心引擎的重要改进,目标是成为 Web 服务和面向服 ...
- 移植RT2870无线网卡驱动到s3c2416
公司项目要用到usb无线网卡,芯片是ralink的RT2870.以下是将其驱动移植到s3c2416的步骤. 1.下载驱动源码,雷凌官网的下载地址是: http://www.ralinktech.com ...
- PHP数据结构之一:PHP数据结构基本概念—数据结构
学习任何一种技术都应该先清楚它的基本概念,这是学习任何知识的起点!本文是讲述数据结构的基本概念,适合对数据结构已经有一定基础的程序员,更是适合想要学习数据结构的code一族!让我们开始PHP数据结构的 ...
- SpringCloud03 Ribbon知识点、 Feign知识点、利用RestTemplate+Ribbon调用远程服务提供的资源、利用feign调用远程服务提供的资源、熔断
1 远程服务资源的调用 1.1 古老的套路 在微服务出现之前,所有的远程服务资源必须通过RestTemplate或者HttpClient进行:但是这两者仅仅实现了远程服务资源的调用,并未提供负载均衡实 ...
- SpringBoot25 gradle安装、利用gradle创建SrpingBoot项目
1 gradle安装 技巧01:gradle依赖JDK或者JRE,而且版本至少时1.7 1.1 下载安装包 到gradle官网下载安装包[PS: 也可以利用命令的方式安装,本案例是利用安装包的方式] ...
- Hyperledger Fabric1.0 整体结构
整体结构 Hyperledger Fabric 在 1.0 中,架构已经解耦为三部分: fabric-peer:主要起到 peer 作用,包括 endorser.committer 两种角色: fab ...
- 39.FORMAT() 函数
FORMAT() 函数 FORMAT 函数用于对字段的显示进行格式化. SQL FORMAT() 语法 SELECT FORMAT(column_name,format) FROM table_nam ...