I would like to understand how to do the following:
How many IP addresses will the user be targeting ? (Receive input ex. 3 )
Put each IP into the same script commands
- ex. (adb -s [IP1] shell [command]), (adb -s [IP2] shell [command]), (adb -s [IP3] shell [command])
Print(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 event value (ex. event6, event7, event8, ect.): ")
os.system("adb -s " (ip_address2) " shell getevent -c 1")
event2 = input("Enter the 2nd event value (ex. event6, event7, event8, ect.): ")
os.system("adb -s " (ip_address3) " shell getevent -c 1")
event3 = input("Enter the 2nd event value (ex. event6, event7, event8, ect.): ")
os.system("adb -s " (ip_address4) " shell getevent -c 1")
event4 = input("Enter the 2nd event value (ex. event6, event7, event8, ect.): ")
os.system("adb -s " (ip_address5) " shell getevent -c 1")
event5 = input("Enter the 2nd event value (ex. event6, event7, event8, ect.): ")
count = 0
count2 = 0
count3 = 0
count4 = 0
count5 = 0
count6 = 0
count7 = 0
count8 = 0
count9 = 0
count10 = 0
count11 = 0
firstDisplay = subprocess.getoutput("adb -s " (ip_address) " shell cat /sys/class/display/mode")
print("{}/// Display Mode/// {}".format(ip_address, firstDisplay))
firstDisplay2 = subprocess.getoutput("adb -s " (ip_address2) " shell cat /sys/class/display/mode")
print("{}/// Display Mode/// {}".format(ip_address2, firstDisplay2))
firstDisplay3 = subprocess.getoutput("adb -s " (ip_address3) " shell cat /sys/class/display/mode")
print("{}/// Display Mode/// {}".format(ip_address3, firstDisplay3))
firstDisplay4 = subprocess.getoutput("adb -s " (ip_address4) " shell cat /sys/class/display/mode")
print("{}/// Display Mode/// {}".format(ip_address4, firstDisplay4))
firstDisplay5 = 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
##Switch HDMI 1
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")
##Switch HDMI 1
subprocess.getoutput("adb -s " (ip_address2) " shell sendevent /dev/input/" (event2) " 4 4 787201;sendevent /dev/input/" (event2) " 1 240 1;sendevent /dev/input/" (event2) " 0 0 0;sendevent /dev/input/" (event2) " 4 4 787201;sendevent /dev/input/" (event2) " 1 240 0;sendevent /dev/input/" (event2) " 0 0 0")
##Switch HDMI 1
subprocess.getoutput("adb -s " (ip_address3) " shell sendevent /dev/input/" (event3) " 4 4 787201;sendevent /dev/input/" (event3) " 1 240 1;sendevent /dev/input/" (event3) " 0 0 0;sendevent /dev/input/" (event3) " 4 4 787201;sendevent /dev/input/" (event3) " 1 240 0;sendevent /dev/input/" (event3) " 0 0 0")
##Switch HDMI 1
subprocess.getoutput("adb -s " (ip_address4) " shell sendevent /dev/input/" (event4) " 4 4 787201;sendevent /dev/input/" (event4) " 1 240 1;sendevent /dev/input/" (event4) " 0 0 0;sendevent /dev/input/" (event4) " 4 4 787201;sendevent /dev/input/" (event4) " 1 240 0;sendevent /dev/input/" (event4) " 0 0 0")
##Switch HDMI 1
subprocess.getoutput("adb -s " (ip_address5) " shell sendevent /dev/input/" (event5) " 4 4 787201;sendevent /dev/input/" (event5) " 1 240 1;sendevent /dev/input/" (event5) " 0 0 0;sendevent /dev/input/" (event5) " 4 4 787201;sendevent /dev/input/" (event5) " 1 240 0;sendevent /dev/input/" (event5) " 0 0 0")
- Currently I'm asking for 5 inputs always and re writing the same commands for every IP.. I feel like there's a better way to have the command listed once and keep switching the variable.
Thank you
CodePudding user response:
Any time that you find yourself numbering variables, you can usually replace that code with a list and a loop. A loop will keep you from repeating the same statement over and over. Also, if you want to access a specific entry from a list, you can always index it. items[1]
is analogous to item1
in every way except that you can now use a variable in place of the index.
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")
CodePudding user response:
Thank you @MarmadileManteater , I appreciate your answer. Does this always work the same with the IF statement :
@MarmadileManteater Here is the bottom portion of my code, it looks for the display mode after switching back home. Then it checks for that the display /mode is not 'null'. If it's null it will throw out that error code and add 1 to that devices NULL count. Can I do the same as you mentioned for IF statements ? How would I increase the count for this specific data point ?
'''
x = subprocess.getoutput("adb -s " (ip_address) " shell cat /sys/class/display/mode")
print("{}/// Display Mode/// {}".format(ip_address, x))
y = subprocess.getoutput("adb -s " (ip_address2) " shell cat /sys/class/display/mode")
print("{}/// Display Mode/// {}".format(ip_address2, y))
x1 = subprocess.getoutput("adb -s " (ip_address3) " shell cat /sys/class/display/mode")
print("{}/// Display Mode/// {}".format(ip_address3, x1))
y1 = subprocess.getoutput("adb -s " (ip_address4) " shell cat /sys/class/display/mode")
print("{}/// Display Mode/// {}".format(ip_address4, y1))
x2 = subprocess.getoutput("adb -s " (ip_address5) " shell cat /sys/class/display/mode")
print("{}/// Display Mode/// {}\n".format(ip_address5, x2))
if x == 'null':
print("Blacksreen on home!")
count2 =1
print ("Switch loop no:" str(count) "\n")
os.system("adb -s " (ip_address) " shell echo " (firstDisplay) " > /sys/class/display/mode" )
if y == 'null':
print("Blacksreen on home!")
count3 =1
print ("Switch loop no:" str(count) "\n")
os.system("adb -s " (ip_address2) " shell echo " (firstDisplay2) " > /sys/class/display/mode" )
if x1 == 'null':
print("Blacksreen on home!")
count4 =1
print ("Switch loop no:" str(count) "\n")
os.system("adb -s " (ip_address3) " shell echo " (firstDisplay3) " > /sys/class/display/mode" )
if y1 == 'null':
print("Blacksreen on home!")
count5 =1
print ("Switch loop no:" str(count) "\n")
os.system("adb -s " (ip_address4) " shell echo " (firstDisplay4) " > /sys/class/display/mode" )
if x2 == 'null':
print("Blacksreen on home!")
count6 =1
print ("Switch loop no:" str(count) "\n")
os.system("adb -s " (ip_address5) " shell echo " (firstDisplay5) " > /sys/class/display/mode" )
print("Succesful switch back home loop no:" str(count) "\n ")
print("NUMBER OF NULLS FOR: " (ip_address) " //// -->" str(count2) "\n "
"NUMBER OF NULLS FOR: " (ip_address2) " //// -->" str(count3) "\n "
"NUMBER OF NULLS FOR: " (ip_address3) " //// -->" str(count4) "\n ")
"NUMBER OF NULLS FOR: " (ip_address4) " //// -->" str(count5) "\n "
"NUMBER OF NULLS FOR: " (ip_address5) " //// -->" str(count6) "\n ")
print("NUMBER OF WRONG cur_port FOR: " (ip_address) " //// -->" str(count7) "\n "
"NUMBER OF WRONG cur_port FOR: " (ip_address2) " //// -->" str(count8) "\n "
"NUMBER OF WRONG cur_port FOR: " (ip_address3) " //// -->" str(count9) "\n ")
"NUMBER OF WRONG cur_port FOR: " (ip_address4) " //// -->" str(count10) "\n "
"NUMBER OF WRONG cur_port FOR: " (ip_address5) " //// -->" str(count11) "\n ")'''