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交换机的配置的更多相关文章

  1. (转)python的paramiko模块

    python的paramiko模块  原文:http://www.cnblogs.com/breezey/p/6663546.html     paramiko是用python语言写的一个模块,遵循S ...

  2. Python之paramiko模块和SQL连接API

    堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...

  3. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  4. Python之paramiko模块

    今天我们来了解一下python的paramiko模块 paramiko是python基于SSH用于远程服务器并执行相应的操作. 我们先在windows下安装paramiko 1.cmd下用pip安装p ...

  5. 使用python的Paramiko模块登陆SSH

    使用python的Paramiko模块登陆SSH paramiko是用Python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. python的paramiko模块 ...

  6. 利用python 下paramiko模块无密码登录

    利用python 下paramiko模块无密码登录   上次我个大家介绍了利用paramiko这个模块,可以模拟ssh登陆远程服务器,并且可以返回执行的命令结果,这次给大家介绍下如何利用已经建立的密钥 ...

  7. python使用wmi模块获取windows下的系统信息监控系统-乾颐堂

    Python用WMI模块获取Windows系统的硬件信息:硬盘分区.使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位置,系统的版本等信息. 本文实例讲述了python使用wmi模块获取w ...

  8. python的paramiko模块

        paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Solaris, BSD, MacOS X, ...

  9. python的paramiko模块的安装与使用

    一:简介 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. 由于使用的是python这样的能够跨平台运行的语言,所以所有python支 ...

随机推荐

  1. 浏览器访问IPv6地址

    http://[IPv6]:port/index.html http://[3ffe:3201:1200:1::91]:8080/index.html 目前现在的网络运营商基本都不支持基于IPv6地址 ...

  2. List对象中的组合、查询、扩展

    var Pnts = segs.Select( c =>                                             pntTsLst.Where           ...

  3. 转摘:ashx+jquery-autocomplete文本框输入提示功能Asp.net

    引入所需文件 <script type="text/javascript" src="JS/jquery-1.8.2.min.js"></sc ...

  4. JasperMES.cn JasperMES.com.cn 我的MES网站

    好久没有更新博客园.今天写上两面的两个网址JasperMES.cn JasperMES.com.cn是想记录我在做上面两个网站时的一点心得. 老实说,现在建站的软件很多,WordPress,CMS,我 ...

  5. python 中 print 函数用法总结

    Python 思想: “一切都是对象!” 在 Python 3 中接触的第一个很大的差异就是缩进是作为语法的一部分,这和C++等其他语言确实很不一样,所以要小心 ,其中python3和python2中 ...

  6. s3c6410开发板LED驱动程序设计详细…

    2 下面来看看tiny6410关于LED的原理图如图(1)所示: 图1    LED原理图 3 LED实例,代码如下所示:(代码摘自\光盘4\实验代码\3-3-1\src\main.c) main.c ...

  7. Scala中的函数

    Scala中的函数 提纲 1.Scala中的函数简介 2.Scala中的函数的各种写法 1.Scala中的函数简介 =================== Scala是函数式编程和面向对象式编程的混合 ...

  8. ORA-01940:无法删除当前已链接的用户

    (1)查看用户的连接状况 select user name, sid, serial# from v$session; (2)找到要删除用户的sid,和serial,并删除 例如要删除用户nc633t ...

  9. 【光速使用开源框架系列】图片加载框架ImageLoader

    [关于本系列] 最近看了不少开源框架,网上的资料也非常多,但是我认为了解一个框架最好的方法就是实际使用.本系列博文就是带领大家快速的上手一些常用的开源框架,体会到其作用. 由于作者水平有限,本系列只会 ...

  10. 2014蓝桥杯B组初赛试题《奇怪的分式》

    题目描述: 上小学的时候,小明经常自己发明新算法.一次,老师出的题目是:     1/4 乘以 8/5      小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (参见图1.png)   ...