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 * "-")
python subprocess ping
add a comment |
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 * "-")
python subprocess ping
2
You're not comparing"Destination host unreachable"
to anything.
– Barmar
Nov 12 at 21:49
Shouldn't theelif
say!= 0
? The0
code was already handled by theif
block.
– Barmar
Nov 12 at 21:53
2
There's no need to callstr()
,"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
add a comment |
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 * "-")
python subprocess ping
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
python subprocess ping
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 theelif
say!= 0
? The0
code was already handled by theif
block.
– Barmar
Nov 12 at 21:53
2
There's no need to callstr()
,"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
add a comment |
2
You're not comparing"Destination host unreachable"
to anything.
– Barmar
Nov 12 at 21:49
Shouldn't theelif
say!= 0
? The0
code was already handled by theif
block.
– Barmar
Nov 12 at 21:53
2
There's no need to callstr()
,"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
add a comment |
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.
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 capturingstdout
. My answer shows how to convert what it captured into a string via theresponse.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
add a comment |
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 * "-")
#------------------------------------------------------
add a comment |
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.
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 capturingstdout
. My answer shows how to convert what it captured into a string via theresponse.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
add a comment |
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.
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 capturingstdout
. My answer shows how to convert what it captured into a string via theresponse.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
add a comment |
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.
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.
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 capturingstdout
. My answer shows how to convert what it captured into a string via theresponse.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
add a comment |
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 capturingstdout
. My answer shows how to convert what it captured into a string via theresponse.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
add a comment |
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 * "-")
#------------------------------------------------------
add a comment |
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 * "-")
#------------------------------------------------------
add a comment |
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 * "-")
#------------------------------------------------------
# - 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 * "-")
#------------------------------------------------------
answered Nov 14 at 12:39
Alan Wolfe
31
31
add a comment |
add a comment |
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%2f53270507%2fhow-to-properly-get-the-results-from-subprocess-run%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
2
You're not comparing
"Destination host unreachable"
to anything.– Barmar
Nov 12 at 21:49
Shouldn't the
elif
say!= 0
? The0
code was already handled by theif
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