【问题标题】:Python Script - Ask user how many IP addresses they want to use, then use that input for a loop script?Python 脚本 - 询问用户他们想要使用多少个 IP 地址,然后将该输入用于循环脚本?
【发布时间】:2022-08-23 01:27:12
【问题描述】:

我想了解如何执行以下操作:

  • 用户将定位多少个 IP 地址? (接收输入 ex. 3 )

  • 将每个 IP 放入相同的脚本命令中

    • 例如。 (adb -s [IP1] shell [命令]), (adb -s [IP2] shell [命令]), (adb -s [IP3] shell [命令])
  • 打印(IP1、IP2、IP3)

import subprocess
import os

##Get IP of devices connected
ip_address = input(\"Enter the IP address you wish to test in the form xxx.xxx.xxx.xxx: \")
ip_address2 = input(\"Enter Next IP address you wish to test in the form xxx.xxx.xxx.xxx: or ENTER  \")
ip_address3 =  input(\"Enter Next IP address you wish to test in the form xxx.xxx.xxx.xxx: or ENTER  \")
ip_address4 = input(\"Enter Next IP address you wish to test in the form xxx.xxx.xxx.xxx: or ENTER  \")
ip_address5 = input(\"Enter Next IP address you wish to test in the form xxx.xxx.xxx.xxx: or ENTER  \")

##GET THE EVENT TYPE OF EACH RCU
os.system(\"adb -s \" +(ip_address)+ \" shell getevent -c 1\")
event = input(\"Enter the happy meal Value\")

os.system(\"adb -s \" +(ip_address2)+ \" shell getevent -c 1\")
event2 = input(\"Enter the happy meal Value  \")

os.system(\"adb -s \" +(ip_address3)+ \" shell getevent -c 1\")
event3 = input(\"Enter the happy meal Value  \")

os.system(\"adb -s \" +(ip_address4)+ \" shell getevent -c 1\")
event4 = input(\"Enter the happy meal Value  \")

os.system(\"adb -s \" +(ip_address5)+ \" shell getevent -c 1\")
event5 = input(\"Enter the happy meal Value  \")

happy = 0
happy2= 0
happy3 = 0
happy4 = 0
happy5 = 0
happy6 = 0

happy7 = 0
happy8 = 0
happy9 = 0
happy10 = 0
happy11 = 0


happy= subprocess.getoutput(\"adb -s \"+(ip_address)+\" shell cat /sys/class/display/mode\")
print(\"{}/// Display Mode/// {}\".format(ip_address, firstDisplay))

happy2= subprocess.getoutput(\"adb -s \"+(ip_address2)+\" shell cat /sys/class/display/mode\")
print(\"{}/// Display Mode/// {}\".format(ip_address2, firstDisplay2))

happy3= subprocess.getoutput(\"adb -s \"+(ip_address3)+\" shell cat /sys/class/display/mode\")
print(\"{}/// Display Mode/// {}\".format(ip_address3, firstDisplay3))

happy4= subprocess.getoutput(\"adb -s \"+(ip_address4)+\" shell cat /sys/class/display/mode\")
print(\"{}/// Display Mode/// {}\".format(ip_address4, firstDisplay4))

happy5 = subprocess.getoutput(\"adb -s \"+(ip_address5)+\" shell cat /sys/class/display/mode\")
print(\"{}/// Display Mode/// {}\\n\".format(ip_address5, firstDisplay5))



while count<100: #[ $n -le 5 ]

  count += 1


  • 目前我总是要求 5 个输入并为每个 IP 重新编写相同的命令。我觉得有一个更好的方法可以让命令列出一次并不断切换变量。

谢谢

    标签: python variables automation script


    【解决方案1】:

    每当您发现自己为变量编号时,通常可以用列表和循环替换该代码。循环将防止您一遍又一遍地重复相同的语句。此外,如果您想访问列表中的特定条目,您可以随时为其编制索引。 items[1] 在各个方面都类似于 item1,只是您现在可以使用变量代替索引。

    import subprocess
    import os
    
    total_length = 5
    ip_addresses = []
    events = []
    ##Get IP of devices connected
    while len(ip_addresses) < total_length:
        ip_addresses.append(input("Enter the IP address you wish to test in the form xxx.xxx.xxx.xxx: "))
    
    while len(events) < total_length:
        # Set i to the length of events before we push our new entry, so that it references the last entry in the list
        i = len(events)
        ##GET THE EVENT TYPE OF EACH RCU
        os.system("adb -s " +(ip_addresses[i])+ " shell getevent -c 1")
        events.append(input("Enter the event value (ex. event6, event7, event8, ect.):  "))
    
    count = 0
    counts = []
    
    firstDisplays = []
    while len(firstDisplays) < total_length:
        # Set i to the length of first displays before we push our new entry, so that it references the last entry in the list
        i = len(firstDisplays)
        firstDisplays.append(subprocess.getoutput("adb -s "+(ip_addresses[i])+" shell cat /sys/class/display/mode"))
        print("{}/// Display Mode/// {}".format(ip_addresses[i], firstDisplays[i]))
    
    while count<100: #[ $n -le 5 ]
      count += 1
    
    for i in range(0, total_length):
      # Set i to the length of our lists before we push our new entries, so that it references the last entries in the lists
      ip_address = ip_addresses[i]
      event = events[i]
      print('SWITCHING TO HDMI-1 INPUT\n')
      subprocess.getoutput("adb -s " +(ip_address)+ " shell sendevent /dev/input/"+(event)+" 4 4 787201;sendevent /dev/input/"+(event)+" 1 240 1;sendevent /dev/input/"+(event)+" 0 0 0;sendevent /dev/input/"+(event)+" 4 4 787201;sendevent /dev/input/"+(event)+" 1 240 0;sendevent /dev/input/"+(event)+" 0 0 0")
    

    【讨论】:

    • 我发布了另一个 cmets 作为答案,您介意看看我在设置索引时可能遇到的问题。 @marmadilemanteater
    猜你喜欢
    • 2012-08-08
    • 1970-01-01
    • 2019-04-30
    • 2013-01-26
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    相关资源
    最近更新 更多