如何使用python脚本远程登录华为交换机?

如何使用python脚本远程登录华为交换机?,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我想用python脚本远程登录华为交换机,阅读一些基本的工作人员,比如显示int简短的结果。我知道华为的基本命令和一些编程。 我可以用python脚本远程登录cisco路由器 这是我迄今为止的尝试 import telnetlib import datetime now = datetime.datetime.now() host = "myhost" # your router ip username = "user" # the username password = "pass

我想用python脚本远程登录华为交换机,阅读一些基本的工作人员,比如显示int简短的结果。我知道华为的基本命令和一些编程。 我可以用python脚本远程登录cisco路由器

这是我迄今为止的尝试

import telnetlib 
import datetime 

now = datetime.datetime.now() 
host = "myhost" # your router ip 
username = "user" # the username 
password = "pass" 

tn = telnetlib.Telnet(host,23,6) 
tn.read_until("Username:") 
tn.write(username+"\n") 
tn.read_until("Password:") 
tn.write(password+"\n") 
tn.write("display int description"+"\n") 
#tn.write("sh run"+"\n") 
tn.write("quit"+"\n") 
output = tn.read_all() 

fp = open("sw_hu.txt","w") 
fp.write(output) 
fp.close()
我会使用库,然后打开与pexpect.spawn'telnet ip_address'模块的连接,这是一个很好的开始。如果您现在使用命令,那么它主要是脱机、expect和before的组合


编辑:在备份配置中添加了部分示例—python 2.x和旧华为路由器

您需要提供更多信息,您的问题缺乏详细信息,因此不清楚,也太宽泛,无法得到答案。你在标准库中有,因此这将是一个很好的开始,当你遇到问题时,你可以发布一些更具体的问题。我添加了我的脚本来描述我的期望。好的,你的代码是如何失败的,以及你对cisco路由器做了哪些不同的操作。有时路由器有旧版本的服务器,相同的代码不适用于它们。相同的脚本在cisco中工作。我只更改了命令display int description以显示int-brief和quit以退出,提示设备可能需要不同的命令。尝试从终端进行telnet会话,并找出需要发出的命令。最终,您的代码可能需要根据设备的不同发出不同的命令。请您编辑您的答案,并给出解释,以使其易于理解?
    if  w['router_type'] == "cisco":
        tn.read_until("Username:")
        tn.write(user.encode('ascii')+"\n"
            #tn.write(user +"\n")
        if password:
            tn.read_until("Password:")
            tn.write(password.encode("ascii") + "\n")(indent)
            tn.write("terminal length 0\n")
            tn.write("show version\n")
            tn.write("show running-config view full\n")
            tn.write("exit\n")
            tn_output = tn.read_all()
            print ("Logging out Router ") + w['host_name'] + ("_") + w['router_type'] + (".....")
            flag = 1


    if  w['router_type'] == "huawei":
        tn.read_until("Username:")
        tn.write(user + "\n")
        if password:
            tn.read_until("Password:")
            tn.write(password + "\n")
            tn.write("screen-length 0 temporary\n")
            tn.write("dis version\n")
            tn.write("dis current-configuration\n")
            tn_output = tn.read_until("return")
            tn.write("quit\n")
            print ("Logging out Router ") + w['host_name'] + ("_") + w['router_type'] + (".....")
            flag = 1


    if  w['router_type'] == "MAIPU":
        tn.read_until("login:")
        tn.write(user + "\n")
        if password:
            tn.read_until("password:")
            tn.write(password + "\n")           
            tn.write("more off\n")
            tn.write("show version\n")
            tn.write("show running\n")
            tn.write("logout\n")
            tn_output = tn.read_all()
            flag = 1
            print ("Logging out Router ") + w['host_name'] + ("_") + w['router_type'] + (".....")
    print ("\n Count: " )+ str(v_count) + ("---\n")
    if flag:
        if v_count==0:
            saveoutput = open ("BACKUP/" + HOST + ".txt" , "w")
            saveoutput.write(tn_output)
            print ("Saving new file ") + HOST + ("......")
        elif v_count == 1:
            previous_file = open("BACKUP/" + HOST + ".txt")
            temp_file = previous_file.read()

            if str(temp_file) != str(tn_output):
            saveoutput = open ("BACKUP/" + HOST + "_v2.txt" , "w")
                saveoutput.write(tn_output)`enter code here`
                print ("Saving new file " )+ HOST + ("......")
        elif v_count > 1:
            previous_file = open("BACKUP/" + HOST + "_v" + str(v_count) + ".txt")
            temp_file = previous_file.read()

                if str(temp_file) != str(tn_output):
    saveoutput = open ("BACKUP/" + HOST + "_v" + str(v_count + 1)  + ".txt" , "w")
        print ("Saving new version of file ") + HOST + ("......")`enter code here`
    if  w['router_type'] == "cisco":
        tn.read_until("Username:")
        tn.write(user.encode('ascii')+"\n"
            #tn.write(user +"\n")
        if password:
            tn.read_until("Password:")
            tn.write(password.encode("ascii") + "\n")(indent)
            tn.write("terminal length 0\n")
            tn.write("show version\n")
            tn.write("show running-config view full\n")
            tn.write("exit\n")
            tn_output = tn.read_all()
            print ("Logging out Router ") + w['host_name'] + ("_") + w['router_type'] + (".....")
            flag = 1


    if  w['router_type'] == "huawei":
        tn.read_until("Username:")
        tn.write(user + "\n")
        if password:
            tn.read_until("Password:")
            tn.write(password + "\n")
            tn.write("screen-length 0 temporary\n")
            tn.write("dis version\n")
            tn.write("dis current-configuration\n")
            tn_output = tn.read_until("return")
            tn.write("quit\n")
            print ("Logging out Router ") + w['host_name'] + ("_") + w['router_type'] + (".....")
            flag = 1


    if  w['router_type'] == "MAIPU":
        tn.read_until("login:")
        tn.write(user + "\n")
        if password:
            tn.read_until("password:")
            tn.write(password + "\n")           
            tn.write("more off\n")
            tn.write("show version\n")
            tn.write("show running\n")
            tn.write("logout\n")
            tn_output = tn.read_all()
            flag = 1
            print ("Logging out Router ") + w['host_name'] + ("_") + w['router_type'] + (".....")
    print ("\n Count: " )+ str(v_count) + ("---\n")
    if flag:
        if v_count==0:
            saveoutput = open ("BACKUP/" + HOST + ".txt" , "w")
            saveoutput.write(tn_output)
            print ("Saving new file ") + HOST + ("......")
        elif v_count == 1:
            previous_file = open("BACKUP/" + HOST + ".txt")
            temp_file = previous_file.read()

            if str(temp_file) != str(tn_output):
            saveoutput = open ("BACKUP/" + HOST + "_v2.txt" , "w")
                saveoutput.write(tn_output)`enter code here`
                print ("Saving new file " )+ HOST + ("......")
        elif v_count > 1:
            previous_file = open("BACKUP/" + HOST + "_v" + str(v_count) + ".txt")
            temp_file = previous_file.read()

                if str(temp_file) != str(tn_output):
    saveoutput = open ("BACKUP/" + HOST + "_v" + str(v_count + 1)  + ".txt" , "w")
        print ("Saving new version of file ") + HOST + ("......")`enter code here`