简体   繁体   中英

Reading from txt file and executing an os command

What i want to achieve would be reading the IP/Domain from the TXT file and executing an OS import command then adding the IP/Domain when pinging.

However the problem is that for some reason it's placing a period at the end of the IP/Domain that it read from the TXT file, resulting in an invalid request when pining (the code works the only problem would be the period at the end)

for example: when the pinging line executes on the compiler it's telling me "bad parameters google.com." however on the txt file it self there is only one period which is the one for the.com it self.

def scanlist():
ipopen = open("IPlist.txt")

#Opens the IPlist.txt file and strips each of the lines so that we can read individually.
with open("IPlist.txt", "r+") as ips_file:
    ips = [ip.strip() for ip in ips_file.readlines()]

#Read each line from the IPlist.txt file
with open("IPlist.txt", "r") as available_ips_file:
    for ip in ips:
        #Pings each line from the IPlist.txt file
        response = os.system('ping -a 1 {}'.format(ip))

        if response == 0:  # 512/DOWN value - 0/UP value
            # Up
            print("- Ip Address:", ip, 'is up!')
        elif response == 512:
            #down
            print("- IP Address:", ip, 'is down!')
        else:
            #other error
            print("- Bad parameters or other error!")

For the entire code visit the github: https://github.com/Hontiris1/IPPing/blob/master/Ping.py

the issue was in the paramter you were passing to the ping the 1 after -a is not a valid parameter

import os

def scanlist():

#Opens the IPlist.txt file and strips each of the lines so that we can read individually.
    with open("IPlist.txt") as ips_file:
        ips  = list(map(str.strip,ips_file.readlines()))

    #Read each line from the IPlist.txt file
    for ip in ips:
        #Pings each line from the IPlist.txt file
        response = os.system('ping {} -a -n 1'.format(ip)) # to send only one request
        if response == 0:  # 512/DOWN value - 0/UP value
            # Up
            print("- Ip Address:", ip, 'is up!')
        elif response == 1: # if it's time out
            #down
            print("- IP Address:", ip, 'is down!')
        else:
            #other error
            print("- Bad parameters or other error!")


scanlist()

output

Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=55ms TTL=56

Ping statistics for 8.8.8.8:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 55ms, Maximum = 55ms, Average = 55ms
- Ip Address: 8.8.8.8 is up!

Pinging stackoverflowll.com [218.93.250.18] with 32 bytes of data:
Request timed out.

Ping statistics for 218.93.250.18:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
- IP Address: stackoverflowll.com is down!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM