Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UnboundLocalError:局部变量';ip地址a';分配前参考_Python_Variables_Telnet - Fatal编程技术网

Python UnboundLocalError:局部变量';ip地址a';分配前参考

Python UnboundLocalError:局部变量';ip地址a';分配前参考,python,variables,telnet,Python,Variables,Telnet,我正在使用telnetlib.Telnet函数连接到Telnet设备,但我不知道如何修复错误UnboundLocalError:分配前引用的局部变量“ip_addressa” 没有,我看到了,但这对我没有帮助,所以这是我的代码 import getpass import sys import telnetlib import traceback def h4ck_th3_w0rld(HOST): user = 'admin' password = 'password'

我正在使用telnetlib.Telnet函数连接到Telnet设备,但我不知道如何修复错误UnboundLocalError:分配前引用的局部变量“ip_addressa” 没有,我看到了,但这对我没有帮助,所以这是我的代码

import getpass
import sys
import telnetlib
import traceback
 
def h4ck_th3_w0rld(HOST):
    user = 'admin'
    password = 'password'
    tn = None
    try:
        tn = telnetlib.Telnet(HOST, 23, 11)
 
        tn.read_until("Username: ", 7)
        tn.write(user + "\n")
        if password:
            tn.read_until("Password: ", 7)
            tn.write(password + "\n")

    for a in range(200, 256):
        for b in range(7, 7):
            ip_addressa = '192.168.%d.%d' % (b, a)
          
            print(ip_addressa)
 

    tn.write("cd /var/tmp\n")
    tn.write("./busybox telnet" + ip_addressa + "\n")
        tn.write("exit\n")
 
        print(tn.read_all())
    except Exception as e:
        #print traceback.print_exc()
        print 'Error occurred'
    finally:
        if tn:
            tn.close()
 
 
for i in range(205, 206):
    for j in range(7, 8):
        ip_address = '192.168.%d.%d' % (j, i)
        h4ck_th3_w0rld(ip_address)
        print(ip_address)

因此,我收到一个错误
UnboundLocalError:assignment前引用的局部变量'ip\U addressa'无

请发布回溯,以便我们可以看到失败的行。并考虑把这个煮沸到一个我们可以运行的最小例子来测试。就目前情况而言,我只能猜测。但是

    for b in range(7, 7):
        ip_addressa = '192.168.%d.%d' % (b, a)
        print(ip_addressa)
range(7,7)
不会迭代任何内容,因此循环不会运行,
ip\u addressa
不会分配,也不会打印任何内容。该范围应该是多少取决于要迭代的地址数

您在发布的代码中还有其他问题,例如的
在“try/except”中没有正确缩进,这是一个语法错误。和
tn.write(“cd/var/tmp\n”)
应该在
内。也许有一些标签空间的事情正在进行,所以我真的不能说