subprocess.run() on all elements of a list
I have a list of hostnames / IP addresses, and my script takes the each item from the text file and stores them in the nodes
variable as a list.
I want to ping each host and output the results to a text file. I can do it with a single host, but am having trouble understanding how to iterate through the list.
I have looked at other posts on Stackoverflow, but most of the posts are using the OS module, which has been deprecated.
My code:
#!/usr/local/bin/python3.6
import argparse
import subprocess
parser = argparse.ArgumentParser(description="Reads a file and pings hosts by line.")
parser.add_argument("filename")
args = parser.parse_args()
# Opens a text file that has the list of IP addresses or hostnames and puts
#them into a list.
with open(args.filename) as f:
lines = f.readlines()
nodes = [x.strip() for x in lines]
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", nodes[0]],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
with open('output.txt', 'w') as f:
print(ping.stdout.decode(), file=f)
f.close()
python
add a comment |
I have a list of hostnames / IP addresses, and my script takes the each item from the text file and stores them in the nodes
variable as a list.
I want to ping each host and output the results to a text file. I can do it with a single host, but am having trouble understanding how to iterate through the list.
I have looked at other posts on Stackoverflow, but most of the posts are using the OS module, which has been deprecated.
My code:
#!/usr/local/bin/python3.6
import argparse
import subprocess
parser = argparse.ArgumentParser(description="Reads a file and pings hosts by line.")
parser.add_argument("filename")
args = parser.parse_args()
# Opens a text file that has the list of IP addresses or hostnames and puts
#them into a list.
with open(args.filename) as f:
lines = f.readlines()
nodes = [x.strip() for x in lines]
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", nodes[0]],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
with open('output.txt', 'w') as f:
print(ping.stdout.decode(), file=f)
f.close()
python
3
I can't work out what your question is sorry
– roganjosh
Oct 11 '18 at 9:50
add a comment |
I have a list of hostnames / IP addresses, and my script takes the each item from the text file and stores them in the nodes
variable as a list.
I want to ping each host and output the results to a text file. I can do it with a single host, but am having trouble understanding how to iterate through the list.
I have looked at other posts on Stackoverflow, but most of the posts are using the OS module, which has been deprecated.
My code:
#!/usr/local/bin/python3.6
import argparse
import subprocess
parser = argparse.ArgumentParser(description="Reads a file and pings hosts by line.")
parser.add_argument("filename")
args = parser.parse_args()
# Opens a text file that has the list of IP addresses or hostnames and puts
#them into a list.
with open(args.filename) as f:
lines = f.readlines()
nodes = [x.strip() for x in lines]
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", nodes[0]],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
with open('output.txt', 'w') as f:
print(ping.stdout.decode(), file=f)
f.close()
python
I have a list of hostnames / IP addresses, and my script takes the each item from the text file and stores them in the nodes
variable as a list.
I want to ping each host and output the results to a text file. I can do it with a single host, but am having trouble understanding how to iterate through the list.
I have looked at other posts on Stackoverflow, but most of the posts are using the OS module, which has been deprecated.
My code:
#!/usr/local/bin/python3.6
import argparse
import subprocess
parser = argparse.ArgumentParser(description="Reads a file and pings hosts by line.")
parser.add_argument("filename")
args = parser.parse_args()
# Opens a text file that has the list of IP addresses or hostnames and puts
#them into a list.
with open(args.filename) as f:
lines = f.readlines()
nodes = [x.strip() for x in lines]
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", nodes[0]],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
with open('output.txt', 'w') as f:
print(ping.stdout.decode(), file=f)
f.close()
python
python
edited Oct 12 '18 at 8:08
SiHa
3,36061633
3,36061633
asked Oct 11 '18 at 9:49
scottsescottse
32
32
3
I can't work out what your question is sorry
– roganjosh
Oct 11 '18 at 9:50
add a comment |
3
I can't work out what your question is sorry
– roganjosh
Oct 11 '18 at 9:50
3
3
I can't work out what your question is sorry
– roganjosh
Oct 11 '18 at 9:50
I can't work out what your question is sorry
– roganjosh
Oct 11 '18 at 9:50
add a comment |
2 Answers
2
active
oldest
votes
You can iterate directly through your list of nodes like so:
with open(args.filename) as f:
lines = f.readlines()
nodes = [x.strip() for x in lines]
with open('output.txt', 'w') as f:
for node in nodes:
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", node],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
print(ping.stdout.decode(), file=f)
Note that you can also directly iterate over your input file, which is said to be more 'Pythonic' than using readlines()
:
with open(args.filename,'r') as infile, open('output.txt', 'w') as outfile:
for line in infile:
node = line.strip()
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", node],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
print(ping.stdout.decode(), file=outfile)
Note that this is untested, but I can't see any obvious errors.
Thank you for help SiHa, I appreciate your help on this! My python script works like I want to with your help.
– scottse
Oct 11 '18 at 18:05
add a comment |
Just iterate through the nodes list like this:
for i in nodes:
ping = subprocess.run(
["ping", "-c 1", i],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
Hope it helps :)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52757097%2fsubprocess-run-on-all-elements-of-a-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can iterate directly through your list of nodes like so:
with open(args.filename) as f:
lines = f.readlines()
nodes = [x.strip() for x in lines]
with open('output.txt', 'w') as f:
for node in nodes:
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", node],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
print(ping.stdout.decode(), file=f)
Note that you can also directly iterate over your input file, which is said to be more 'Pythonic' than using readlines()
:
with open(args.filename,'r') as infile, open('output.txt', 'w') as outfile:
for line in infile:
node = line.strip()
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", node],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
print(ping.stdout.decode(), file=outfile)
Note that this is untested, but I can't see any obvious errors.
Thank you for help SiHa, I appreciate your help on this! My python script works like I want to with your help.
– scottse
Oct 11 '18 at 18:05
add a comment |
You can iterate directly through your list of nodes like so:
with open(args.filename) as f:
lines = f.readlines()
nodes = [x.strip() for x in lines]
with open('output.txt', 'w') as f:
for node in nodes:
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", node],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
print(ping.stdout.decode(), file=f)
Note that you can also directly iterate over your input file, which is said to be more 'Pythonic' than using readlines()
:
with open(args.filename,'r') as infile, open('output.txt', 'w') as outfile:
for line in infile:
node = line.strip()
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", node],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
print(ping.stdout.decode(), file=outfile)
Note that this is untested, but I can't see any obvious errors.
Thank you for help SiHa, I appreciate your help on this! My python script works like I want to with your help.
– scottse
Oct 11 '18 at 18:05
add a comment |
You can iterate directly through your list of nodes like so:
with open(args.filename) as f:
lines = f.readlines()
nodes = [x.strip() for x in lines]
with open('output.txt', 'w') as f:
for node in nodes:
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", node],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
print(ping.stdout.decode(), file=f)
Note that you can also directly iterate over your input file, which is said to be more 'Pythonic' than using readlines()
:
with open(args.filename,'r') as infile, open('output.txt', 'w') as outfile:
for line in infile:
node = line.strip()
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", node],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
print(ping.stdout.decode(), file=outfile)
Note that this is untested, but I can't see any obvious errors.
You can iterate directly through your list of nodes like so:
with open(args.filename) as f:
lines = f.readlines()
nodes = [x.strip() for x in lines]
with open('output.txt', 'w') as f:
for node in nodes:
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", node],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
print(ping.stdout.decode(), file=f)
Note that you can also directly iterate over your input file, which is said to be more 'Pythonic' than using readlines()
:
with open(args.filename,'r') as infile, open('output.txt', 'w') as outfile:
for line in infile:
node = line.strip()
# Opens the ping program
ping = subprocess.run(
["ping", "-c 1", node],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Captures stdout and puts into a text file.
print(ping.stdout.decode(), file=outfile)
Note that this is untested, but I can't see any obvious errors.
edited Nov 14 '18 at 9:53
answered Oct 11 '18 at 11:24
SiHaSiHa
3,36061633
3,36061633
Thank you for help SiHa, I appreciate your help on this! My python script works like I want to with your help.
– scottse
Oct 11 '18 at 18:05
add a comment |
Thank you for help SiHa, I appreciate your help on this! My python script works like I want to with your help.
– scottse
Oct 11 '18 at 18:05
Thank you for help SiHa, I appreciate your help on this! My python script works like I want to with your help.
– scottse
Oct 11 '18 at 18:05
Thank you for help SiHa, I appreciate your help on this! My python script works like I want to with your help.
– scottse
Oct 11 '18 at 18:05
add a comment |
Just iterate through the nodes list like this:
for i in nodes:
ping = subprocess.run(
["ping", "-c 1", i],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
Hope it helps :)
add a comment |
Just iterate through the nodes list like this:
for i in nodes:
ping = subprocess.run(
["ping", "-c 1", i],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
Hope it helps :)
add a comment |
Just iterate through the nodes list like this:
for i in nodes:
ping = subprocess.run(
["ping", "-c 1", i],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
Hope it helps :)
Just iterate through the nodes list like this:
for i in nodes:
ping = subprocess.run(
["ping", "-c 1", i],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
Hope it helps :)
answered Oct 11 '18 at 11:09
NovakNovak
1,4481614
1,4481614
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52757097%2fsubprocess-run-on-all-elements-of-a-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
I can't work out what your question is sorry
– roganjosh
Oct 11 '18 at 9:50