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. zabbix snmp 协议监控 dell iRDAC

    转摘:http://blog.csdn.net/wanglei_storage/article/details/52789921 http://blog.csdn.net/wanglei_storag ...

  2. php创建token

    token是唯一的标识,随机字符串+时间戳结合,一般不会出现重复的字符串了. 这是一种方式 function create_token() { $randLength = 6; $chars = 'a ...

  3. PL/SQL 训练13--plsql 优化

    --数据缓存技术 --PGA和SGA---SGA:系统全局区域--PGA:Process Global Area是为每个连接到Oracle的用户进程保留的内存. ---PLSQL从PGA获取信息的速度 ...

  4. Lambda中的常用sql方法

    1.Groupby 对集合进行分组,如: var dllList = _menuMan.Load(c => c.TXT_ASSEMBLYNAME != null).GroupBy(c=>c ...

  5. oracle 11g R2 标准版 64位linux安装

    安装环境:Redhat es 5.5 64位 ,系统内存8G,swap 10G ,oracle 11G R2 标准版 一,Oracle 安装前的准备检查一下包,必须全部安装:binutils-2.17 ...

  6. STM32用有源蜂鸣器实现闹钟的声响

    有源蜂鸣器的声音是固定的,工作电压恒定,改变通断电的时间获得不同时长的音响,譬如连续音.快速短音.慢速长音(类似莫尔斯电报)来区分不同的报警信息. 简单的说,有源蜂鸣器只能发出一种声音,因为它的频率是 ...

  7. SqlConnection 无法设置连接超时

    1.最有效的方法:对表格建立索引 2 在连接字符串中设置 Connection Timeout (默认15秒)3 设置 SqlCommand.CommandTimeout(默认是 30 秒)

  8. Elasticsearch集群如何扩容机器?

    前提, Elasticsearch-2.4.3的3节点安装(多种方式图文详解)   比如,你已经成功搭建了3台机器的es集群,如我这里分别是192.168.80.10.192.168.80.11.19 ...

  9. Navigator - BOM对象

    Navigator 对象 Navigator 对象包含有关浏览器的信息. 注释:没有应用于 navigator 对象的公开标准,不过所有浏览器都支持该对象. Navigator 对象集合 集合 描述 ...

  10. springMVC+spring+mybatis整合(包括文件上传和下载)

    driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncod ...