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

2018-03-14  本文已影响1347人  wsf535

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

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',
};
#-*- 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() 
[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
上一篇下一篇

猜你喜欢

热点阅读