您的位置:首页 > 编程语言 > Python开发

python 自动化运维脚本,可以巡检思科,华三,等网络设备

2019-10-16 17:38 3943 查看


python 自动化运脚本

按照oxidized搭建的CMDB 为数据源。自动向数据库读取账号密码,设备名称,等 并输出状态信息 如图所示



数据库字段如下 enable 字段存放思科的enable密码,login 是登陆方式 存放http登陆,ssh 和telnet等登陆方式。device 是设备类型。


第一段代码 批量抓取思科,华三的配置,命令部分我已经做了注释(具体模块使用可以百度)


paramiko
threading
time
os
pymysql

(ipusernamepasswdcmdname):
    :

        log = (+%(nameip)) ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ipusernamepasswd=)
        ssh_shell = ssh.invoke_shell()  ssh_shell.recv()
        m cmd:
            = ssh_shell.sendall(m)  time.sleep()
        log.write( ssh_shell.recv())
        ssh.close()
    :
        %(ip)

__name__==:
    h3c_sw = [] h3c_wifi = []cisco_2911 = []thread=[]
    db = pymysql.connect(
        ======)
    cursor = db.cursor()
    cursor.execute() h3c_sql = cursor.fetchall()
    i h3c_sql:
        i[] == :
            a=threading.Thread(=ssh2=(i[]i[]i[]h3c_wifii[]))   a.start()
        :
            a = threading.Thread(=ssh2=(i[]i[]i[]h3c_swi[]))
            a.start()
    cursor.execute()cisco_sql = cursor.fetchall()
    i cisco_sql :
        i[] == :
            cisco_2911[] = (i[]+ )   b = threading.Thread(=ssh2=(i[]i[]i[]cisco_2911i[]))
            b.start()
            b.join()   db.close()


此python文件执行完成后 生成的log会保存到本地。



用正则表达式 读取 参数进行输出 只输出了华三设备

re
os
sys
pymysql
(ipname) :
        :
                (%ip) file :  message = file.read()
                pattern = match = re.findall(patternmessage)
                (ip++(match))
                routes = re.findall(message)
                (ip++(routes))

                cpu = re.findall(message)

                (ip++cpu[])

                memory = re.findall(message)
                memory1= re.findall(memory[])

                (ip++memory1[])

                power = re.findall(message)
                power = re.findall(power[])
                (ip++power[])

                fan = re.findall(message)
                fan = re.findall(fan[])
                (ip++fan[])
                ()
        :
                __name__==:
    db = pymysql.connect(
        ======)
    cursor = db.cursor()
    cursor.execute()
    data = cursor.fetchall()
    i data:
        xunjian((i[]))
    db.close()










内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: