LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Python ping script from file. (http://www.linuxquestions.org/questions/programming-9/python-ping-script-from-file-4175451032/)

0men 02-20-2013 08:50 PM

Python ping script from file.
 
I'm trying to create a small python ping script which reads ip addresses' from a .txt file and pings them each twice. I can do it no problems with subprocess and just 1 ip address(no .txt file)

But Im just struggling with how to pass the ip addresses to the ping function. Im guessing im meant to readlines() but im stuck on how i get python to use the ip address and actually perform the ping for each ip address.

...confused as hell. Hopefully someone gets what i mean and can help.

thanks in advance.

pgpython 02-21-2013 03:28 AM

Yes you probaly can use readlines. readlines returns a list of each line in the file. You can also use the with statement when you open the file, so assuming your file is something like:

Code:

XXX.XXX.XXX.XXX
XXX.XXX.XX.XXX
...

you can use something like

Code:

with ("ips.txt", "r") as f:
    for ip in f.readlines():
        #Do something with each ip


I hope that helps and don't forget you can always use dir(obj) and help(obj) from the python console to get help on how to use a module or object


All times are GMT -5. The time now is 01:31 AM.