公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

提出需求

现在有这样的需求:全网的监控骨干设备及VPN骨干设备都要新增SSH配置(开通SSH登录),涉及到的网络设备上百台,若一台台登录到设备上去配置,想想工作量都巨大。那么Python的应用来啦。只需要简单的100来行代码,就可以自动下发相应配置到设备上。

会点Python编程的网络工程师,工作效率嗖嗖嗖的提升!

需要应用到的Python模块:

  • 1、telnetlib 远程telnet到网络设备(socket连上后想干嘛干嘛)
  • 2、time、datetime模块(需要表述时间),time模块的sleep()常常被用到,休眠、停顿的意思。
  • 3、multiproccessing 多进程,使用多进程可以节省时间(多进程占用更多的CPU,区别于多线程-threading模块)
  • 4、pyMySQL模块,用它来连接数据库,操作数据库。

直接上脚本:(Python2编写,linux环境下实测运行正常)

# -*- coding: UTF-8 -*-

#! /usr/bin/python2.7

import telnetlib

import datetime

import time

from time import sleep

from multiprocessing import Process

import pymysql.cursors

def GO(H, ip,device_type_id):

    username = 'username'

    password = 'password'

    if device_type_id == 20:

        tn = telnetlib.Telnet(ip)

        tn.read_until('login:', timeout=3)

        tn.write(username + 'n')

        tn.read_until('Password:', timeout=3)

        tn.write(password + 'n')

        time.sleep(4)

        tn.write('su' + 'n')

        tn.read_until('Password:', timeout=3)

        tn.write('hzcnc_enable' + 'n')

        time.sleep(3)

        tn.read_until('>', timeout=3)

        tn.write('sys n ssh server enable n ssh server acl 2001n quitn  ')

        time.sleep(2)

        tn.close()

        print "H3C-%s is OK! Now time is %s" % (H, datetime.date.today())

    #interface_config.close()

    if device_type_id == 21 or device_type_id == 22 or device_type_id == 24 or device_type_id == 19:

        tn = telnetlib.Telnet(ip)

        tn.read_until('Username:', timeout=3)

        tn.write(username + 'n')

        tn.read_until('Password:', timeout=3)

        tn.write(password + 'n')

        time.sleep(4)

        tn.write('su' + 'n')

        tn.read_until('Password:', timeout=3)

        tn.write('hzcnc_enable' + 'n')

        time.sleep(3)

        tn.read_until('>', timeout=3)

        tn.write('sys n ssh server enable n ssh server acl 2001n quitn  ')

        time.sleep(2)

        tn.close()

        print "H3C-%s is OK! Now time is %s" % (H, datetime.date.today())

    if device_type_id == 23 or device_type_id == 16:

        tn = telnetlib.Telnet(ip)

        tn.read_until('login:', timeout=3)

        tn.write(username + 'n')

        tn.read_until('Password:', timeout=3)

        tn.write(password + 'n')

        time.sleep(3)

        tn.write('su' + 'n')

        tn.read_until('Password:', timeout=3)

        tn.write('hzcnc_enable' + 'n')

        time.sleep(3)

        tn.read_until('>', timeout=3)

        tn.write('sys n ssh server enable n ssh server acl 2001n quitn  ')

        time.sleep(2)

        tn.close()

        print "H3C-%s is OK! Now time is %s" % (H, datetime.date.today())

if __name__ == '__main__':

    connection = pymysql.connect(host='localhost',

                                 user='username',

                                 password='passwd',

                                 db='db_name',

                                 charset='utf8mb4',

                                 cursorclass=pymysql.cursors.DictCursor)

    try:

        with connection.cursor() as cursor:

            sql = "SELECT name, manage_ip, device_type_id FROM devices WHERE device_role_id=6"

            # sql = "SELECT name, manage_ip, device_type_id FROM dcim_device WHERE manage_ip ='device_ip'"

            cursor.execute(sql)

            result = cursor.fetchall()

    finally:

        connection.close()

    for x in result:

        p = Process(target=GO, args=(str(x['name']),str(x['manage_ip']),int(x['device_type_id']),))

        p.start()

        sleep(2)

配置结果:

看我怎么通过Python脚本给100台网络设备一键化下发相同的配置

完美配置完毕

【笔者为网络工程师,懂点Python编程基础,习惯用自动化编程的思维去编程相关运维脚本,工作多年,希望把自己的经验分享给大家,觉得有用的,可以关注、点赞、转发,如有相同或者不同观点,欢迎评论,谢谢!】

分享到:
标签:脚本 Python
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定