How to properly get the results from subprocess.run()?











up vote
-2
down vote

favorite












I am sorry to ask again about this, but I have been unable to find a way to eliminate a false positive that keeps happening.



When I get a reply "Destination unreachable" reply it's showing all packets returned and 0 packets lost... so its showing SERVER UP instead of down.



how on gods earth can I get around this?



# Server up/down Script

# - Module Import section
import socket
import sys
import os
import subprocess

# - IP Address input request
hostname1 = input (" Please Enter IP Address: ")

# - Command to run ping request, but also hides ping info
response = subprocess.run(["ping", "-c", "1", hostname1], stdout=subprocess.PIPE)
response.returncode

#___ ORIGINAL CODE ___
#if (response == "Reply from", hostname1):
if response.returncode == 0:
print ( 50 * "-")
print ("[ **SERVER IS ALIVE** ]")
print ( 50 * "-")
elif response.returncode == 0 and (str("Destination host unreachable.")):
print( 50 * "-")
print(hostname1, "[ **SERVER DOWN** ] ")
print( 50 * "-")
else:
print( 50 * "-")
print(hostname1, "[ **SERVER DOWN** ] ")
print( 50 * "-")









share|improve this question




















  • 2




    You're not comparing "Destination host unreachable" to anything.
    – Barmar
    Nov 12 at 21:49










  • Shouldn't the elif say != 0? The 0 code was already handled by the if block.
    – Barmar
    Nov 12 at 21:53






  • 2




    There's no need to call str(), "Destination host unreachable" is already a string.
    – Barmar
    Nov 12 at 21:54










  • You need to read from the pipe and test whether the output contains that stream.
    – Barmar
    Nov 12 at 21:55















up vote
-2
down vote

favorite












I am sorry to ask again about this, but I have been unable to find a way to eliminate a false positive that keeps happening.



When I get a reply "Destination unreachable" reply it's showing all packets returned and 0 packets lost... so its showing SERVER UP instead of down.



how on gods earth can I get around this?



# Server up/down Script

# - Module Import section
import socket
import sys
import os
import subprocess

# - IP Address input request
hostname1 = input (" Please Enter IP Address: ")

# - Command to run ping request, but also hides ping info
response = subprocess.run(["ping", "-c", "1", hostname1], stdout=subprocess.PIPE)
response.returncode

#___ ORIGINAL CODE ___
#if (response == "Reply from", hostname1):
if response.returncode == 0:
print ( 50 * "-")
print ("[ **SERVER IS ALIVE** ]")
print ( 50 * "-")
elif response.returncode == 0 and (str("Destination host unreachable.")):
print( 50 * "-")
print(hostname1, "[ **SERVER DOWN** ] ")
print( 50 * "-")
else:
print( 50 * "-")
print(hostname1, "[ **SERVER DOWN** ] ")
print( 50 * "-")









share|improve this question




















  • 2




    You're not comparing "Destination host unreachable" to anything.
    – Barmar
    Nov 12 at 21:49










  • Shouldn't the elif say != 0? The 0 code was already handled by the if block.
    – Barmar
    Nov 12 at 21:53






  • 2




    There's no need to call str(), "Destination host unreachable" is already a string.
    – Barmar
    Nov 12 at 21:54










  • You need to read from the pipe and test whether the output contains that stream.
    – Barmar
    Nov 12 at 21:55













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I am sorry to ask again about this, but I have been unable to find a way to eliminate a false positive that keeps happening.



When I get a reply "Destination unreachable" reply it's showing all packets returned and 0 packets lost... so its showing SERVER UP instead of down.



how on gods earth can I get around this?



# Server up/down Script

# - Module Import section
import socket
import sys
import os
import subprocess

# - IP Address input request
hostname1 = input (" Please Enter IP Address: ")

# - Command to run ping request, but also hides ping info
response = subprocess.run(["ping", "-c", "1", hostname1], stdout=subprocess.PIPE)
response.returncode

#___ ORIGINAL CODE ___
#if (response == "Reply from", hostname1):
if response.returncode == 0:
print ( 50 * "-")
print ("[ **SERVER IS ALIVE** ]")
print ( 50 * "-")
elif response.returncode == 0 and (str("Destination host unreachable.")):
print( 50 * "-")
print(hostname1, "[ **SERVER DOWN** ] ")
print( 50 * "-")
else:
print( 50 * "-")
print(hostname1, "[ **SERVER DOWN** ] ")
print( 50 * "-")









share|improve this question















I am sorry to ask again about this, but I have been unable to find a way to eliminate a false positive that keeps happening.



When I get a reply "Destination unreachable" reply it's showing all packets returned and 0 packets lost... so its showing SERVER UP instead of down.



how on gods earth can I get around this?



# Server up/down Script

# - Module Import section
import socket
import sys
import os
import subprocess

# - IP Address input request
hostname1 = input (" Please Enter IP Address: ")

# - Command to run ping request, but also hides ping info
response = subprocess.run(["ping", "-c", "1", hostname1], stdout=subprocess.PIPE)
response.returncode

#___ ORIGINAL CODE ___
#if (response == "Reply from", hostname1):
if response.returncode == 0:
print ( 50 * "-")
print ("[ **SERVER IS ALIVE** ]")
print ( 50 * "-")
elif response.returncode == 0 and (str("Destination host unreachable.")):
print( 50 * "-")
print(hostname1, "[ **SERVER DOWN** ] ")
print( 50 * "-")
else:
print( 50 * "-")
print(hostname1, "[ **SERVER DOWN** ] ")
print( 50 * "-")






python subprocess ping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 22:22









martineau

64.6k887172




64.6k887172










asked Nov 12 at 21:41









Alan Wolfe

31




31








  • 2




    You're not comparing "Destination host unreachable" to anything.
    – Barmar
    Nov 12 at 21:49










  • Shouldn't the elif say != 0? The 0 code was already handled by the if block.
    – Barmar
    Nov 12 at 21:53






  • 2




    There's no need to call str(), "Destination host unreachable" is already a string.
    – Barmar
    Nov 12 at 21:54










  • You need to read from the pipe and test whether the output contains that stream.
    – Barmar
    Nov 12 at 21:55














  • 2




    You're not comparing "Destination host unreachable" to anything.
    – Barmar
    Nov 12 at 21:49










  • Shouldn't the elif say != 0? The 0 code was already handled by the if block.
    – Barmar
    Nov 12 at 21:53






  • 2




    There's no need to call str(), "Destination host unreachable" is already a string.
    – Barmar
    Nov 12 at 21:54










  • You need to read from the pipe and test whether the output contains that stream.
    – Barmar
    Nov 12 at 21:55








2




2




You're not comparing "Destination host unreachable" to anything.
– Barmar
Nov 12 at 21:49




You're not comparing "Destination host unreachable" to anything.
– Barmar
Nov 12 at 21:49












Shouldn't the elif say != 0? The 0 code was already handled by the if block.
– Barmar
Nov 12 at 21:53




Shouldn't the elif say != 0? The 0 code was already handled by the if block.
– Barmar
Nov 12 at 21:53




2




2




There's no need to call str(), "Destination host unreachable" is already a string.
– Barmar
Nov 12 at 21:54




There's no need to call str(), "Destination host unreachable" is already a string.
– Barmar
Nov 12 at 21:54












You need to read from the pipe and test whether the output contains that stream.
– Barmar
Nov 12 at 21:55




You need to read from the pipe and test whether the output contains that stream.
– Barmar
Nov 12 at 21:55












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










The expression (str("Destination host unreachable.") you have in your code will always evaluate to True—the truthiness of any non-empty string.



If you want to see whether a specific string is in the stdout output produced by an invoked subprocess, you would need to use an expression something like this:



("Destination host unreachable." in response.stdout.decode("utf-8"))


after the response = subprocess.run(...) call.



Here's an article describing how to use the subprocess module (note especially the Capturing Output section.






share|improve this answer





















  • Thank you for the advice. Capturing the output was my next plan of attack... but still being a newbie I was trawling through the net to find out how.
    – Alan Wolfe
    Nov 13 at 19:47










  • Alan: The code in your question is already capturing stdout. My answer shows how to convert what it captured into a string via the response.stdout.decode("utf-8") expression and then check that to see if the literal string is in it. Please see What should I do when someone answers my question?
    – martineau
    Nov 13 at 19:59










  • I understood what you had said and that the stdout was already being captured, but I wanted to learn about how it worked first so I understand how it worked. Anyway, I have learned enough to get the code to work and to get the correct response, even when "Destination unreachable" is displayed.
    – Alan Wolfe
    Nov 14 at 12:24












  • Alan: OK. I just want to make sure you understood the code in my answer, and it sounds like you do. If you found my answer helpful, please read What should I do when someone answers my question?.
    – martineau
    Nov 14 at 17:18




















up vote
0
down vote













# - Import section - below are the modules I will need to call for this script
import socket
import sys
import os
import subprocess
import ctypes
import ipaddress

# - User input request for IP or hostanme
hostname1 = input (" Please Enter IP Address: ")

ip_net = ipaddress.ip_network(hostname1)

# Configure subprocess to hide the console window
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE

output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(hostname1)], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]

if "Destination host unreachable" in output.decode('utf-8'):
print ( 50 * "-")
print (ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", " *** SERVER STATUS - ALERT *** ", 0))
print ( 50 * "-")
elif "Request timed out" in output.decode('utf-8'):
print( 50 * "-")
print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", "*** SERVER STATUS - ALERT ***", 0))
print( 50 * "-")
else:
print( 50 * "-")
print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " *** SERVER ALIVE *** ", "** SERVER STATUS - ALERT **", 0))
print( 50 * "-")


#------------------------------------------------------





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',
    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%2f53270507%2fhow-to-properly-get-the-results-from-subprocess-run%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








    up vote
    0
    down vote



    accepted










    The expression (str("Destination host unreachable.") you have in your code will always evaluate to True—the truthiness of any non-empty string.



    If you want to see whether a specific string is in the stdout output produced by an invoked subprocess, you would need to use an expression something like this:



    ("Destination host unreachable." in response.stdout.decode("utf-8"))


    after the response = subprocess.run(...) call.



    Here's an article describing how to use the subprocess module (note especially the Capturing Output section.






    share|improve this answer





















    • Thank you for the advice. Capturing the output was my next plan of attack... but still being a newbie I was trawling through the net to find out how.
      – Alan Wolfe
      Nov 13 at 19:47










    • Alan: The code in your question is already capturing stdout. My answer shows how to convert what it captured into a string via the response.stdout.decode("utf-8") expression and then check that to see if the literal string is in it. Please see What should I do when someone answers my question?
      – martineau
      Nov 13 at 19:59










    • I understood what you had said and that the stdout was already being captured, but I wanted to learn about how it worked first so I understand how it worked. Anyway, I have learned enough to get the code to work and to get the correct response, even when "Destination unreachable" is displayed.
      – Alan Wolfe
      Nov 14 at 12:24












    • Alan: OK. I just want to make sure you understood the code in my answer, and it sounds like you do. If you found my answer helpful, please read What should I do when someone answers my question?.
      – martineau
      Nov 14 at 17:18

















    up vote
    0
    down vote



    accepted










    The expression (str("Destination host unreachable.") you have in your code will always evaluate to True—the truthiness of any non-empty string.



    If you want to see whether a specific string is in the stdout output produced by an invoked subprocess, you would need to use an expression something like this:



    ("Destination host unreachable." in response.stdout.decode("utf-8"))


    after the response = subprocess.run(...) call.



    Here's an article describing how to use the subprocess module (note especially the Capturing Output section.






    share|improve this answer





















    • Thank you for the advice. Capturing the output was my next plan of attack... but still being a newbie I was trawling through the net to find out how.
      – Alan Wolfe
      Nov 13 at 19:47










    • Alan: The code in your question is already capturing stdout. My answer shows how to convert what it captured into a string via the response.stdout.decode("utf-8") expression and then check that to see if the literal string is in it. Please see What should I do when someone answers my question?
      – martineau
      Nov 13 at 19:59










    • I understood what you had said and that the stdout was already being captured, but I wanted to learn about how it worked first so I understand how it worked. Anyway, I have learned enough to get the code to work and to get the correct response, even when "Destination unreachable" is displayed.
      – Alan Wolfe
      Nov 14 at 12:24












    • Alan: OK. I just want to make sure you understood the code in my answer, and it sounds like you do. If you found my answer helpful, please read What should I do when someone answers my question?.
      – martineau
      Nov 14 at 17:18















    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    The expression (str("Destination host unreachable.") you have in your code will always evaluate to True—the truthiness of any non-empty string.



    If you want to see whether a specific string is in the stdout output produced by an invoked subprocess, you would need to use an expression something like this:



    ("Destination host unreachable." in response.stdout.decode("utf-8"))


    after the response = subprocess.run(...) call.



    Here's an article describing how to use the subprocess module (note especially the Capturing Output section.






    share|improve this answer












    The expression (str("Destination host unreachable.") you have in your code will always evaluate to True—the truthiness of any non-empty string.



    If you want to see whether a specific string is in the stdout output produced by an invoked subprocess, you would need to use an expression something like this:



    ("Destination host unreachable." in response.stdout.decode("utf-8"))


    after the response = subprocess.run(...) call.



    Here's an article describing how to use the subprocess module (note especially the Capturing Output section.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 at 23:08









    martineau

    64.6k887172




    64.6k887172












    • Thank you for the advice. Capturing the output was my next plan of attack... but still being a newbie I was trawling through the net to find out how.
      – Alan Wolfe
      Nov 13 at 19:47










    • Alan: The code in your question is already capturing stdout. My answer shows how to convert what it captured into a string via the response.stdout.decode("utf-8") expression and then check that to see if the literal string is in it. Please see What should I do when someone answers my question?
      – martineau
      Nov 13 at 19:59










    • I understood what you had said and that the stdout was already being captured, but I wanted to learn about how it worked first so I understand how it worked. Anyway, I have learned enough to get the code to work and to get the correct response, even when "Destination unreachable" is displayed.
      – Alan Wolfe
      Nov 14 at 12:24












    • Alan: OK. I just want to make sure you understood the code in my answer, and it sounds like you do. If you found my answer helpful, please read What should I do when someone answers my question?.
      – martineau
      Nov 14 at 17:18




















    • Thank you for the advice. Capturing the output was my next plan of attack... but still being a newbie I was trawling through the net to find out how.
      – Alan Wolfe
      Nov 13 at 19:47










    • Alan: The code in your question is already capturing stdout. My answer shows how to convert what it captured into a string via the response.stdout.decode("utf-8") expression and then check that to see if the literal string is in it. Please see What should I do when someone answers my question?
      – martineau
      Nov 13 at 19:59










    • I understood what you had said and that the stdout was already being captured, but I wanted to learn about how it worked first so I understand how it worked. Anyway, I have learned enough to get the code to work and to get the correct response, even when "Destination unreachable" is displayed.
      – Alan Wolfe
      Nov 14 at 12:24












    • Alan: OK. I just want to make sure you understood the code in my answer, and it sounds like you do. If you found my answer helpful, please read What should I do when someone answers my question?.
      – martineau
      Nov 14 at 17:18


















    Thank you for the advice. Capturing the output was my next plan of attack... but still being a newbie I was trawling through the net to find out how.
    – Alan Wolfe
    Nov 13 at 19:47




    Thank you for the advice. Capturing the output was my next plan of attack... but still being a newbie I was trawling through the net to find out how.
    – Alan Wolfe
    Nov 13 at 19:47












    Alan: The code in your question is already capturing stdout. My answer shows how to convert what it captured into a string via the response.stdout.decode("utf-8") expression and then check that to see if the literal string is in it. Please see What should I do when someone answers my question?
    – martineau
    Nov 13 at 19:59




    Alan: The code in your question is already capturing stdout. My answer shows how to convert what it captured into a string via the response.stdout.decode("utf-8") expression and then check that to see if the literal string is in it. Please see What should I do when someone answers my question?
    – martineau
    Nov 13 at 19:59












    I understood what you had said and that the stdout was already being captured, but I wanted to learn about how it worked first so I understand how it worked. Anyway, I have learned enough to get the code to work and to get the correct response, even when "Destination unreachable" is displayed.
    – Alan Wolfe
    Nov 14 at 12:24






    I understood what you had said and that the stdout was already being captured, but I wanted to learn about how it worked first so I understand how it worked. Anyway, I have learned enough to get the code to work and to get the correct response, even when "Destination unreachable" is displayed.
    – Alan Wolfe
    Nov 14 at 12:24














    Alan: OK. I just want to make sure you understood the code in my answer, and it sounds like you do. If you found my answer helpful, please read What should I do when someone answers my question?.
    – martineau
    Nov 14 at 17:18






    Alan: OK. I just want to make sure you understood the code in my answer, and it sounds like you do. If you found my answer helpful, please read What should I do when someone answers my question?.
    – martineau
    Nov 14 at 17:18














    up vote
    0
    down vote













    # - Import section - below are the modules I will need to call for this script
    import socket
    import sys
    import os
    import subprocess
    import ctypes
    import ipaddress

    # - User input request for IP or hostanme
    hostname1 = input (" Please Enter IP Address: ")

    ip_net = ipaddress.ip_network(hostname1)

    # Configure subprocess to hide the console window
    info = subprocess.STARTUPINFO()
    info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    info.wShowWindow = subprocess.SW_HIDE

    output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(hostname1)], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]

    if "Destination host unreachable" in output.decode('utf-8'):
    print ( 50 * "-")
    print (ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", " *** SERVER STATUS - ALERT *** ", 0))
    print ( 50 * "-")
    elif "Request timed out" in output.decode('utf-8'):
    print( 50 * "-")
    print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", "*** SERVER STATUS - ALERT ***", 0))
    print( 50 * "-")
    else:
    print( 50 * "-")
    print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " *** SERVER ALIVE *** ", "** SERVER STATUS - ALERT **", 0))
    print( 50 * "-")


    #------------------------------------------------------





    share|improve this answer

























      up vote
      0
      down vote













      # - Import section - below are the modules I will need to call for this script
      import socket
      import sys
      import os
      import subprocess
      import ctypes
      import ipaddress

      # - User input request for IP or hostanme
      hostname1 = input (" Please Enter IP Address: ")

      ip_net = ipaddress.ip_network(hostname1)

      # Configure subprocess to hide the console window
      info = subprocess.STARTUPINFO()
      info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
      info.wShowWindow = subprocess.SW_HIDE

      output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(hostname1)], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]

      if "Destination host unreachable" in output.decode('utf-8'):
      print ( 50 * "-")
      print (ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", " *** SERVER STATUS - ALERT *** ", 0))
      print ( 50 * "-")
      elif "Request timed out" in output.decode('utf-8'):
      print( 50 * "-")
      print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", "*** SERVER STATUS - ALERT ***", 0))
      print( 50 * "-")
      else:
      print( 50 * "-")
      print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " *** SERVER ALIVE *** ", "** SERVER STATUS - ALERT **", 0))
      print( 50 * "-")


      #------------------------------------------------------





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        # - Import section - below are the modules I will need to call for this script
        import socket
        import sys
        import os
        import subprocess
        import ctypes
        import ipaddress

        # - User input request for IP or hostanme
        hostname1 = input (" Please Enter IP Address: ")

        ip_net = ipaddress.ip_network(hostname1)

        # Configure subprocess to hide the console window
        info = subprocess.STARTUPINFO()
        info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        info.wShowWindow = subprocess.SW_HIDE

        output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(hostname1)], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]

        if "Destination host unreachable" in output.decode('utf-8'):
        print ( 50 * "-")
        print (ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", " *** SERVER STATUS - ALERT *** ", 0))
        print ( 50 * "-")
        elif "Request timed out" in output.decode('utf-8'):
        print( 50 * "-")
        print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", "*** SERVER STATUS - ALERT ***", 0))
        print( 50 * "-")
        else:
        print( 50 * "-")
        print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " *** SERVER ALIVE *** ", "** SERVER STATUS - ALERT **", 0))
        print( 50 * "-")


        #------------------------------------------------------





        share|improve this answer












        # - Import section - below are the modules I will need to call for this script
        import socket
        import sys
        import os
        import subprocess
        import ctypes
        import ipaddress

        # - User input request for IP or hostanme
        hostname1 = input (" Please Enter IP Address: ")

        ip_net = ipaddress.ip_network(hostname1)

        # Configure subprocess to hide the console window
        info = subprocess.STARTUPINFO()
        info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        info.wShowWindow = subprocess.SW_HIDE

        output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(hostname1)], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]

        if "Destination host unreachable" in output.decode('utf-8'):
        print ( 50 * "-")
        print (ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", " *** SERVER STATUS - ALERT *** ", 0))
        print ( 50 * "-")
        elif "Request timed out" in output.decode('utf-8'):
        print( 50 * "-")
        print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " [ SERVER OFFLINE ] ", "*** SERVER STATUS - ALERT ***", 0))
        print( 50 * "-")
        else:
        print( 50 * "-")
        print(ctypes.windll.user32.MessageBoxW(0, hostname1 + " *** SERVER ALIVE *** ", "** SERVER STATUS - ALERT **", 0))
        print( 50 * "-")


        #------------------------------------------------------






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 at 12:39









        Alan Wolfe

        31




        31






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270507%2fhow-to-properly-get-the-results-from-subprocess-run%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 change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents