subprocess.run() on all elements of a list










0















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()









share|improve this question



















  • 3





    I can't work out what your question is sorry

    – roganjosh
    Oct 11 '18 at 9:50















0















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()









share|improve this question



















  • 3





    I can't work out what your question is sorry

    – roganjosh
    Oct 11 '18 at 9:50













0












0








0


0






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()









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












2 Answers
2






active

oldest

votes


















0














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.






share|improve this answer

























  • 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


















-1














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 :)






share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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.






    share|improve this answer

























    • 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















    0














    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.






    share|improve this answer

























    • 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













    0












    0








    0







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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

















    • 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













    -1














    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 :)






    share|improve this answer



























      -1














      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 :)






      share|improve this answer

























        -1












        -1








        -1







        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 :)






        share|improve this answer













        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 :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 11 '18 at 11:09









        NovakNovak

        1,4481614




        1,4481614



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20