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. js操作一般文件和csv文件

    js操作一般文件和csv文件 将文本文件读成字符串 <input type="file" id="upload"> document.getElem ...

  2. 【技术调研】最强Node-RED初探总结

    在某个项目中需要调研下node-red的功能,我大概花了三天时间研究了相关的官方文档,写了几个Demo总结了下node-red相关的功能.如需转载,请注明出处 https://www.cnblogs. ...

  3. vue中使用markdown富文本,并在html页面中展示

    想给自己的后台增加一个markdown编辑器,下面记录下引用的步骤 引入组件mavon-editor 官网地址:https://github.com/hinesboy/mavonEditor // 插 ...

  4. LINUX的SSH下FTP到远程服务器Entering Passive Mode失败解决

    LINUX 系统FTP连接远程服务器经常出现在传输文件或者发出 ls命令时候出现 "Entering Passive Mode "然后就再也无法运作了.该工作主要是因为LINUX的 ...

  5. windows 使用命令打开防火墙的端口

    Open TCP Port 80 in Windows Firewall Using Netsh [McNeel Wiki]https://wiki.mcneel.com/zoo/zoo5netsh ...

  6. 使用composer安装laravel5.4

    composer create-project --prefer-dist laravel/laravel blog 后面的是文件目录

  7. jsp Ajax请求(返回xml数据类型)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  8. opennebula 发送序列化ID,构造json格式错误

  9. boost库中sleep方法详解

    博客转载自:https://blog.csdn.net/huang_xw/article/details/8453506 boost库中sleep有两个方法: 1. 这个方法只能在线程中用, 在主线程 ...

  10. Makefile 编写规则 - 1

    Makefilen内容 1. 显示规则:显示规则说明了,如何生成一个或多个目标.这是由Makefile指出要生成的文件和文件依赖的文件.2. 隐晦规则:基于Makefile的自动推导功能3. 变量的定 ...