用paramiko多线程SSH登陆华为交换机处理简单的命令。

前面提到过可以用netconf来管理交换机,可是我们的交换机没有提前开启netconf。难道要一台台登陆上开,NO NO...下面介绍用paramiko登陆交换批量修改。

  • 首先建一个交换机信息文件hz_sw_ip.py,主要保存交换机名与管理地址,重点是后面的管理地址要能SSH登陆到交换机,内容如下
hz_ip={
'1F-E03-ES' : '192.168.45.62',
'1F-E04-kvm' : '192.168.45.67',
'1F-E10-kvm   ' : '192.168.45.68',
'4F-A32-kvm   ' : '192.168.45.13',
'4F-A34-kvm   ' : '192.168.45.14',
'4F-A36-kvm   ' : '192.168.45.57',
};
  • 再建一个远程登陆执行命令文件:enablenetconf.py
#-*- coding: utf-8 -*-
#!/usr/bin/python 
import paramiko
import threading
import time
from hz_sw_ip import hz_ip  #导入交换机信息
def ssh2(ip,username,passwd,cmd):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip,22,username,passwd,timeout=5)
        ssh_shell = ssh.invoke_shell()  #使用invoke是为了可以执行多条命令
        print ssh_shell.recv(1024)
        for m in cmd:
            res = ssh_shell.sendall(m+'\n')
            time.sleep(float(1))
        print ssh_shell.recv(1024)
        ssh.close()
    except :
        print '%s\tError\n'%(ip)


if __name__=='__main__':
    cmd = ['sys','snetconf server enable','commit'] #你要执行的命令列表
    username = "wsf535"  #用户名
    passwd = "*****"    #密码
    threads = [10]   #多线程
    print "Begin......"
    for key in hz_ip:
        ip = hz_ip[key]
        a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
        a.start() 
  • 执行:python enablenetconf.py 查看运行过程。
[root@192-168-56-48 enablenetconf]# python enablenetconf.py
Begin......

Info: The max number of VTY users is 5, the number of current VTY users online is 1, and total number of terminal users online is 1.
      The current login time is 2018-03-14 13:36:48+08:00.
      The last login time is 2017-12-18 15:25:07+08:00 from 192.168.48.8 through SSH.

Info: The max number of VTY users is 5, the number of current VTY users online is 1, and total number of terminal users online is 1.
      The current login time is 2018-03-14 13:36:49+08:00.







Info: The max number of VTY users is 5, the number of current VTY users online is 1, and total number of terminal users online is 1.
      The current login time is 2018-03-14 21:54:51+08:00.

<1F-E04-kvm>sys
Enter system view, return user view with return command.
[1F-E04-kvm]

      The last login time is 2018-02-15 18:27:37+08:00 from 192.168.48.8 through SSH.
<1F-E10-kvm>sys
Enter system view, return user view with return command.
[~1F-E10-kvm]
Info: The max number of VTY users is 5, the number of current VTY users online is 1, and total number of terminal users online is 1.
      The current login time is 2018-03-14 13:36:49+08:00.
      The last login time is 2018-03-13 14:42:09+08:00 from 192.168.48.8 through SSH.
<1F-E03-ES>sys
Enter system view, return user view with return command.
[~1F-E03-ES]
Info: The max number of VTY users is 5, the number of current VTY users online is 1, and total number of terminal users online is 1.
      The current login time is 2018-03-14 21:47:26+08:00.
      The last login time is 2017-12-27 22:18:05+08:00 from 192.168.48.8 through SSH.
<4F-A34-kvm>sys
Enter system view, return user view with return command.
[4F-A34-kvm]
Info: The max number of VTY users is 5, the number of current VTY users online is 1, and total number of terminal users online is 1.
      The current login time is 2018-03-14 13:36:49+08:00.
      The last login time is 2017-11-16 17:57:08+08:00 from 192.168.48.8 through SSH.
<4F-A36-kvm>sys
Enter system view, return user view with return command.
[4F-A36-kvm]

      The last login time is 2017-10-31 08:41:39+08:00 from 192.168.48.8 through SSH.
<4F-A32-kvm>sys
Enter system view, return user view with return command.
[~4F-A32-kvm]
snetconf server enable
Info: Succeeded in starting the SNETCONF server on SSH port 22.
[*1F-E04-kvm]
snetconf server enable
Info: Succeeded in starting the SNETCONF server on SSH port 22.
[*F-E10-kvm]
snetconf server enable
Info: Succeeded in starting the SNETCONF server on SSH port 22.
[*1F-E03-ES]
snetconf server enable
Info: Succeeded in starting the SNETCONF server on SSH port 22.
[*4F-A34-kvm]
snetconf server enable
Info: Succeeded in starting the SNETCONF server on SSH port 22.
[*4F-A36-kvm]
snetconf server enable
Info: Succeeded in starting the SNETCONF server on SSH port 22.
[*4F-A32-kvm]
commit
Committing.
commit

commit
Committing.
commit

commit
Committing.
commit
  • 验证:查看命令执行成功
<XS-4F-A36-kvm>dis current-configuration | in netcon
snetconf server enable
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 0×1.帧中继概述Frame Relay,简称FR,可以将它看做X.25协议的简化版本,帧中继网络中不考虑传输差错...
    Zero___阅读 2,407评论 0 2
  • Python 面向对象Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对...
    顺毛阅读 4,239评论 4 16
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,239评论 2 33
  • 从昨天的预约到今天的成行,从南街出发跨过锦江来到北街,从高安二中老校区来到高安中学老校区,见到罗老师的时间从按小时...
    长江秋水阅读 585评论 1 2
  • 灯泡 卫生间的灯泡烧坏了,我跑出去买了一个回来准备换上,一进门却见老婆面有怒色,原来坏的是浴室的镜前灯——那是一种...
    众山小阅读 339评论 0 1