Operation and maintenance of the juvenile series - python and cisco (2)

Operation and maintenance of the juvenile series - python and cisco (2)

I. Overview of tasks

Assume that the network can manage more than one layer 2 switch, now we need to create VLAN 10 on each switch, in the absence of CDP, how to use python completed? (Do not forget python is suitable for almost all devices that support remote login)

Second, the implementation steps described

1) a prerequisite

First preparation or article and the like need to turn on the switch and configure SSH IP, user name and password please login configuration for all devices is consistent.

2) the topology of FIG.

3) How to achieve multiple IP

If there is a network device, the IP address of each device is not the same (this is nonsense), how do we go one by one to log more than one device and then execute the command it?

In python, if the address is continuous, then we can use a for loop to achieve individually log onto the device features, such as

[root@yunwei cisco]# cat for.py 
for i in range(1,10):
    print '192.168.108.%s' %i

[root@yunwei cisco]
[root@yunwei cisco]# python for.py 
192.168.108.1
192.168.108.2
192.168.108.3
192.168.108.4
192.168.108.5
192.168.108.6
192.168.108.7
192.168.108.8
192.168.108.9

However, if IP is not contiguous how to do it? So in python we can all IP write a file, then use a for loop to loop the file line by line, to achieve login. Such as

[root@yunwei cisco]# cat IP.txt 
192.168.108.252
192.168.108.253
[root@yunwei cisco]
[root@yunwei cisco]# cat for.py 
ip = file('IP.txt','rb')  # 以只读的形式打开文件
for i in ip.xreadlines(): # 逐行读取文件中的内容
    print i,
[root@yunwei cisco]
[root@yunwei cisco]# python for.py 
192.168.108.252
192.168.108.253
[root@yunwei cisco]#

4) How to achieve multi-command

How to perform multiple files one time? It can be used like the last time we send multiple functions, but it is clear that such efficiency is too low. We can command writes a file, use a for loop to read the file line by line, and then executed. Such as

[root@yunwei cisco]# cat command.txt 
conf t 
vlan 10
name V10
vlan 20
name V20
vlan 30
name V30
vlan 40
name V40
vlan 50
name V50
vlan 60
name V60
vlan 70
name V70
vlan 80
name V80
vlan 90
name V90
vlan 100
name V100
exit
int e0/0
switchport trunk encapsulation dot1q
switchport mode trunk
end
show vlan 

show int trunk

[root@yunwei cisco]# 
[root@yunwei cisco]# cat for.py 
cmd = file('command.txt','rb')
for i in cmd.xreadlines():
    print i,
[root@yunwei cisco]# 

The final script

Here to be a simple annotation, detailed notes refer to the first article.

import paramiko # 导入模块
import time
user = 'yunwsn' # 定义登录用户名
passwd = '123456' # 定义登录密码
s = paramiko.SSHClient() # 实例化
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
IP = file('IP.txt','rb'# 打开IP文件
for ip in IP.xreadlines(): # 循环IP文件
    s.connect(ip,username=user,password=passwd,look_for_keys=False,allow_agent=False)      # 登录读取到的IP文件行
    print '[ \033[0;32m success\033[0m ] login %s ' %ip  # 打印登录成功信息
    cmd = s.invoke_shell()     # 交互式sshell
    command = file('command.txt','rb'# 因为上面已经登录成功,这里打开命令文件
    for command in command.xreadlines(): # 逐行读取命令
        cmd.send(command)  # 执行命令
        time.sleep(0.1)
        output = cmd.recv(65535# 每执行一个命令就打印一次
        print output

    cmd.close() # 一个IP命令执行完毕,关闭交互式shell,循环下一个ip
IP.close() # 全部Ip执行完成,则关闭IP文件

Results are as follows (FIG movable)

Login and see the results

Micro-channel public number: operation and maintenance teenager


Guess you like

Origin blog.51cto.com/xiaowangzai/2406526