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. Java面向对象-代码块

    Java面向对象-代码块 代码块主要就是通过{}花括号 括起来的代码: 主要分为 普通代码块 构造块 静态代码块三类.后面学到线程还有一个同步代码块,到时候再说: 普通代码块:仅仅是花括号括起来的代码 ...

  2. openGL 变换06

    变换 使用(多个)矩阵(Matrix) 对象可以更好的变换(Transform)一个物体. 向量 向量最基本的定义就是一个方向. 或者说 向量有一个方向(Direction)和大小(Magnitude ...

  3. 视频x264编码浅析

    声明 x264_param_t 结构体变量: x264_param_t params; x264_param_default_preset(&params, "ultrafast&q ...

  4. Path expected for join!的解决办法

    Path expected for join! [SELECT count(*) FROM cn.com.jsoft.entities.TDSysnphoto p left join TDSysnot ...

  5. jdk环境变量一键设置 管理員运行

    退出360等杀毒软件 本机win10 其他环境自测.参考了网上代码修改. @echo off rem dss color 02 mode con cols=70 lines=30 title JDK ...

  6. Xcode迁移工程常见问题

    [Xcode迁移工程常见问题] 1.Header Search Paths (HEADER_SEARCH_PATHS) 是否设置正确.在Search Paths group下. 2.Framework ...

  7. jquery与原生js比较

    以选择符为例,类似于这种 $(".class") 方式,在ie里面,肯定比$("#id") 低很多,而对于chrome和firefox,则因为提供了getEle ...

  8. java Web jsp和servlet的关系

    JSP在本质上就是SERVLET,但是两者的创建方式不一样Servlet完全是JAVA程序代码构成,擅长于流程控制和事务处理,通过Servlet来生成动态网页很不直观JSP由HTML代码和JSP标签构 ...

  9. wdcp挂载数据盘为WWW以及之后出现的各种问题解决方法

    昨天晚上突然有客户反映服务器访问不了,经检查后是因为磁盘满了! 购买阿里云的ECS选择linux系统的时候会赠送20G的系统盘,通常来讲自己用的话基本够了,但是我们作为网络公司,安装了一个WDCP后给 ...

  10. 645. Set Mismatch挑出不匹配的元素和应该真正存在的元素

    [抄题]: he set S originally contains numbers from 1 to n. But unfortunately, due to the data error, on ...