-2

I am not sure what is wrong with the script. The error I get is this:

filename = sys.argv[1]
IndexError: list index out of range

Code:

import subprocess, sys, re
filename = sys.argv[1]
f = open('pingfile.txt', 'r')
result = {}
def main():
    print('IP, TimeToPing (ms)')
    for host in f.readlines():
        ping = subprocess.getoutput(f"ping -c 1 -t 2 {host}")
        dns = re.findall('\d+.\d+.\d+.\d+',ping)
        if 'time' in ping:
            time = re.findall('time=\w+',ping)[0]
            time = time.replace('time=', '')
        else:
            time = 'NotFound'
        print(dns[0], ',', time)

if __name__ == "__main__":
        main()

im not sure what im doing wrong.

SuperStormer
  • 4,531
  • 5
  • 20
  • 32
  • If you're using a python IDLE like me click Run > Run..customized, then fill in your filename in the window. – TheHappyBee May 12 '22 at 21:55
  • yes it helped. now when i run it on command line i get another error. it doesnt like the quotes. at the end File "ping2.py", line 10 ping = subprocess.getoutput(f"ping -c 1 -t 2 {host}") – user19099651 May 12 '22 at 22:13
  • @user19099651, please don't ask new questions in comments. If you have a new question, ask a new question. – Chris May 12 '22 at 22:26

0 Answers0