Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-20-2013, 08:50 PM   #1
Registered: Mar 2011
Location: Gold Coast, Australia
Distribution: saline, centos, freeBSD, slackware
Posts: 180

Rep: Reputation: 22
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.
Old 02-21-2013, 03:28 AM   #2
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142

Rep: Reputation: 32
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:

you can use something like

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python script to monitor a file for changes and then mail the report with the file attached. LXer Syndicated Linux News 0 12-15-2012 05:20 PM
How to close open ports using a python script or a shell script in python ?? apanimesh061 Programming 3 11-20-2011 12:31 AM
[SOLVED] Converting a HTML file to a PNG file through Python script TheIndependentAquarius Programming 12 02-02-2010 10:10 AM
import c header file into a python script deathalele Programming 1 05-26-2009 09:50 AM
Copy file by script python ! BongHongXanh Programming 2 01-11-2006 08:26 PM

All times are GMT -5. The time now is 07:36 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration