利用secrt终端半自动化管理
利用secrt终端半自动化管理
tonyjia87 发表于3年前
利用secrt终端半自动化管理
  • 发表于 3年前
  • 阅读 25
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 让secrt自动完成命令工作
# $language = "Python"
# $interface = "1.0"

# -*- coding: gbk -*-
cmds = {
    '5800':{'waituser':'Username',
            'user':'admin\r',
            'waitps':'assword',
            'password':'appled\r',
            'waitsigner':'>',
            "cmd1":'display current-configuration\r',
            'cmd2':'dis device manuinfo | include DEVICE\r',
            'cmd3':'display ip inter brief\r',
            'cmd4':'display inter brief\r',
            'cmd5':'display mac-address\r',
            'cmd6':'display clock\r',
            'qu':'qu\r',
            'iplist':{'192.168.240.2':'鹿煤录脪脢戮路露脟酶脪碌脦帽PC-P-C202-3-3-NSW03',
                      


                    },
            },
    '5120':{'waituser':'Username',
            'user':'admin\r',
            'waitps':'assword',
            'password':'appled\r',
            'waitsigner':'>',
            'cmd1':'display current-configuration\r',
            'cmd2':'dis device manuinfo \r',
            'cmd3':'display ip inter brief\r',
            'cmd4':'display brief inter \r',
            'cmd5':'display mac-address\r',
            'cmd6':'display clock\r',
            'qu':'qu\r',
            'iplist':{'192.168.240.3':'鹿煤录脪脢戮路露脟酶鹿脺脌铆PC-P-C202-3-3-NSW01',
                        '192.168.240.4':'鹿煤录脪脢戮路露脟酶ipmiPC-P-C202-3-3-NSW05',
                        '192.168.240.7':'鹿煤录脪脢戮路露脟酶鹿脺脌铆PC-P-C202-C-6-5-NSW01',

                    },
            },

    '55102':{'waituser':'Username',
            'user':'admin\r',
            'waitps':'assword',
            'password':'appled\r',
            'waitsigner':'>',
            'cmd1':'display current-configuration\r',
            'cmd2':'dis device manuinfo | include DEVICE\r',
            'cmd3':'display ip inter brief\r',
            'cmd4':'display inter brief\r',
            'cmd5':'display mac-address\r',
            'cmd6':'display clock\r',
            'qu':'qu\r',
            'iplist':{'192.168.240.5':'鹿煤录脪脢戮路露脟酶脪碌脦帽碌露脝卢YPT-202-6-5-BSW01',
                        '192.168.240.6':'鹿煤录脪脢戮路露脟酶麓忙麓垄碌露脝卢YPT-6-5-202-BSW03',

                    },
            },

    'ciscofc':{'waituser':'login',
            'user':'admin\r',
            'waitps':'assword',
            'password':'apple@123\r',
            'waitsigner':'#',
            'cmd1':'show running-config\r',
            'cmd2':' show module \r',
            'cmd3':'show ip interface \r',
            'cmd4':'show int brief \r',
            'cmd5':'show port-license\r',
            'cmd6':'show clock\r',
            'qu':'exit\r',
            'iplist':{'192.168.240.8':'鹿煤录脪脢戮路露脟酶麓忙麓垄ciscofc PC-P-C202-C-3-3-FSW01',
        
                    },
            },
}


#print cmds['55102']['user']
# Connect to a telnet server and automate the initial login sequence.
# Note that synchronous mode is enabled to prevent server output from
# potentially being missed.
def whoel(command):
    crt.Screen.Send(command)
    while True:
            if crt.Screen.WaitForString("More",5):
                crt.Screen.Send(" ")
            else :
                break 

def main(ios):
    crt.Screen.Synchronous = True
    for i in cmds[ios]['iplist'].keys() :
        crt.Session.LogFileName = ('Telnet %s' %(i))
        
    #crt.Session.LogFileName = ('TELNET whoel 192.168.240.4 23.txt')
        crt.Session.Log(True,True)

    # connect to host on port 23 (the default telnet port)
    #
    #crt.Session.Connect("TELNET 172.16.25.254 23")
        crt.Screen.Send("TELNET %s 23 \r" %(i))
        if crt.Screen.WaitForString('Press')==-1:
            whoel(cmds[ios]['qu'])
            crt.Session.Log(False)
            crt.Screen.Synchronous = False
            
        else:
            crt.Screen.WaitForString(cmds[ios]['waituser'])
            crt.Screen.Send(cmds[ios]['user'])

            crt.Screen.WaitForString(cmds[ios]['waitps'])
            crt.Screen.Send(cmds[ios]['password'])

            crt.Screen.Synchronous = False
            crt.Screen.WaitForString(cmds[ios]['waitsigner'])
            whoel(cmds[ios]['cmd1'])
            whoel(cmds[ios]['cmd2'])
            whoel(cmds[ios]['cmd3'])
            whoel(cmds[ios]['cmd4'])
            whoel(cmds[ios]['cmd5'])
            whoel(cmds[ios]['cmd6'])
            whoel(cmds[ios]['qu'])
            crt.Session.Log(False)
            crt.Screen.Synchronous = False
#main('ciscofc')
main('5800')
main('5120')
main('55102')
main('ciscofc')


共有 人打赏支持
粉丝 0
博文 1
码字总数 1523
×
tonyjia87
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: