Ping hosts based on filter and return dead or alive

Tangent point coordinates when the curve is a set of points

How do I prevent employees from either switching to competitors or opening their own business?

How come the nude protesters were not arrested?

Is it expected that a reader will skip parts of what you write?

How is John Wick 3 a 15 certificate?

Union with anonymous struct with flexible array member

A IP can traceroute to it, but can not ping

You have (3^2 + 2^3 + 2^2) Guesses Left. Figure out the Last one

Active low-pass filters --- good to what frequencies?

How to tell your grandparent to not come to fetch you with their car?

Longest bridge/tunnel that can be cycled over/through?

Overlapping String-Blocks

How does an ordinary object become radioactive?

Does a scale have more than seven chords?

is it possible for a vehicle to be manufactured witout a catalitic converter

SQL counting distinct over partition

How can this tool find out registered domains from an IP?

Rebus with 20 song titles

Arriving at the same result with the opposite hypotheses

Meaning of 'lose their grip on the groins of their followers'

Colloquialism for “see you later”

How to hide an urban landmark?

Which languages would be most useful in Europe at the end of the 19th century?

Why can my keyboard only digest 6 keypresses at a time?



Ping hosts based on filter and return dead or alive







.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I am trying to create a script that is capable of pinging multiple hosts from a text file in my directory.The hosts are with specific naming conventions and are grouped.E.g in the text fie



10.10.10.10 XX-YY_ZZ name of the host in form of URL | serial number
The file is not CSV but txt.
What I am hoping to make is something simple which when you run say " pingme YY" ( where YY is the in common element of the devices belonging to a certain location ) it would return me a result of e.g:



XX-YY_ZZ = ALIVE



Probably is very simple but I have no idea how to even start as this is Linux REDHAT we are running.



Any pointers and ideas would be appreciated.










share|improve this question














bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • So you will have multiple hosts with YY in the name and you want the script to ping all of them and return a single status or a status for each one? Also can you post a redacted sample line from your input file in your question so I can see how many columns/separators are in it exactly?

    – Jesse_b
    Aug 11 '17 at 11:16













  • a simple tool for this is Ansible. Use it and get your stuff done very easily.

    – Luv33preet
    Aug 11 '17 at 11:20


















1















I am trying to create a script that is capable of pinging multiple hosts from a text file in my directory.The hosts are with specific naming conventions and are grouped.E.g in the text fie



10.10.10.10 XX-YY_ZZ name of the host in form of URL | serial number
The file is not CSV but txt.
What I am hoping to make is something simple which when you run say " pingme YY" ( where YY is the in common element of the devices belonging to a certain location ) it would return me a result of e.g:



XX-YY_ZZ = ALIVE



Probably is very simple but I have no idea how to even start as this is Linux REDHAT we are running.



Any pointers and ideas would be appreciated.










share|improve this question














bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • So you will have multiple hosts with YY in the name and you want the script to ping all of them and return a single status or a status for each one? Also can you post a redacted sample line from your input file in your question so I can see how many columns/separators are in it exactly?

    – Jesse_b
    Aug 11 '17 at 11:16













  • a simple tool for this is Ansible. Use it and get your stuff done very easily.

    – Luv33preet
    Aug 11 '17 at 11:20














1












1








1








I am trying to create a script that is capable of pinging multiple hosts from a text file in my directory.The hosts are with specific naming conventions and are grouped.E.g in the text fie



10.10.10.10 XX-YY_ZZ name of the host in form of URL | serial number
The file is not CSV but txt.
What I am hoping to make is something simple which when you run say " pingme YY" ( where YY is the in common element of the devices belonging to a certain location ) it would return me a result of e.g:



XX-YY_ZZ = ALIVE



Probably is very simple but I have no idea how to even start as this is Linux REDHAT we are running.



Any pointers and ideas would be appreciated.










share|improve this question














I am trying to create a script that is capable of pinging multiple hosts from a text file in my directory.The hosts are with specific naming conventions and are grouped.E.g in the text fie



10.10.10.10 XX-YY_ZZ name of the host in form of URL | serial number
The file is not CSV but txt.
What I am hoping to make is something simple which when you run say " pingme YY" ( where YY is the in common element of the devices belonging to a certain location ) it would return me a result of e.g:



XX-YY_ZZ = ALIVE



Probably is very simple but I have no idea how to even start as this is Linux REDHAT we are running.



Any pointers and ideas would be appreciated.







bash shell rhel ping






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 11 '17 at 11:07









PseudonymityPseudonymity

62




62





bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • So you will have multiple hosts with YY in the name and you want the script to ping all of them and return a single status or a status for each one? Also can you post a redacted sample line from your input file in your question so I can see how many columns/separators are in it exactly?

    – Jesse_b
    Aug 11 '17 at 11:16













  • a simple tool for this is Ansible. Use it and get your stuff done very easily.

    – Luv33preet
    Aug 11 '17 at 11:20



















  • So you will have multiple hosts with YY in the name and you want the script to ping all of them and return a single status or a status for each one? Also can you post a redacted sample line from your input file in your question so I can see how many columns/separators are in it exactly?

    – Jesse_b
    Aug 11 '17 at 11:16













  • a simple tool for this is Ansible. Use it and get your stuff done very easily.

    – Luv33preet
    Aug 11 '17 at 11:20

















So you will have multiple hosts with YY in the name and you want the script to ping all of them and return a single status or a status for each one? Also can you post a redacted sample line from your input file in your question so I can see how many columns/separators are in it exactly?

– Jesse_b
Aug 11 '17 at 11:16







So you will have multiple hosts with YY in the name and you want the script to ping all of them and return a single status or a status for each one? Also can you post a redacted sample line from your input file in your question so I can see how many columns/separators are in it exactly?

– Jesse_b
Aug 11 '17 at 11:16















a simple tool for this is Ansible. Use it and get your stuff done very easily.

– Luv33preet
Aug 11 '17 at 11:20





a simple tool for this is Ansible. Use it and get your stuff done very easily.

– Luv33preet
Aug 11 '17 at 11:20










2 Answers
2






active

oldest

votes


















0














I believe this will do what you want:



pingme () {
hostfile="/home/jbutryn/test/hostfile.txt"
IFS= mapfile -t hosts < <(cat $hostfile)
for host in "${hosts[@]}"; do
match=$(echo "$host" | grep -o "-$1_" | sed 's/-//' | sed 's/_//')
if [[ "$match" = "$1" ]]; then
hostname=$(echo "$host" | awk '{print $2}')
ping -c1 -W1 $(echo "$host" | awk '{print $1}') > /dev/null
if [[ $? = 0 ]]; then
echo "$hostname is alive"
elif [[ $? = 1 ]]; then
echo "$hostname is dead"
fi
fi
done
}


You can run this function like: pingme UR01 and it should return something like:



[root@JBCLAMP001 test]# pingme CC
BB-CC_DD is dead
AA-CC_EE is alive
[root@JBCLAMP001 test]# pingme DD
CC-DD_EE is dead
CC-DD_JJ is alive





share|improve this answer


























  • I suspect this is exactly what is needed. Is that shell though ....cause I got it on shell and produced me an error ( line 1: syntax error near unexpected token `{ )

    – Pseudonymity
    Aug 11 '17 at 12:40











  • Uhm. Much of it is in bash syntax. In order to use it you can add it to a file and source file or . file. Or if you want it to be a more permanent solution you can source that file from your ~/.bashrc.

    – Jesse_b
    Aug 11 '17 at 12:41













  • Additionally ensure the file doesn't have any windows special characters in it. I wrote that with LF line breaks but it was pasted to the site on a windows machine so it may have added some CR characters too. You can check this with a text editor like notepadd++ or similar.

    – Jesse_b
    Aug 11 '17 at 13:03











  • I have tried and tried and its not working . Tried modifying it - noting . i have 2.6.32-754.2.1.el6.x86_64 Linux , I have the file in .sh , run it via ./ , the txt file I tried to change the order , small letters , big letters- nothing, no errors no nothing as it isnt reading the file.

    – Pseudonymity
    Sep 20 '18 at 9:03





















0














You can use the exit code from ping to determine if ping succeeded or failed.



ping HOST -c1 > /dev/null && echo "ALIVE" || echo "DEAD"


When host is alive exit code is 0, and for dead 1. To ping every host you can loop each line and use awk to get the first column which contains address/hostname.



exec 3<input.txt

while read -u3 line
do
if [ "$line" == "" ]; then
# skip empty lines
continue
fi

ping -c1 $(echo "$line"| awk '{print $1}') > /dev/null

if [ $? = 0 ]; then
echo "$line=ALIVE"
else
echo "$line=DEAD"
fi
done


EDIT:



If you want only to ping host by matching a single line in the file, you can grep it:



# find line for $host, only 1 line
line = grep "$HOST" input.txt |tail -n 1

# $($line |awk '{print $1}') outputs the first column of line (address)
ping -c1 $($line |awk '{print $1}') >/dev/null && echo "$line=ALIVE" || echo "$line=DEAD"





share|improve this answer


























  • Yes but I have multiple hosts per location. So HOST = what is in common in the name of the hosts, correct ? Where exactly in script is the host referance though....I mean if I specify it in the command under HOST ?

    – Pseudonymity
    Aug 11 '17 at 11:39











  • So you have more than one host per line? $(echo "$line"| awk '{print $1}') outputs the first column in the row (assumed hostname/ip address).

    – sebasth
    Aug 11 '17 at 11:41











  • I don't think the -q switch is available in redhat ping. It's not on centos 7 anyway. Also the -W1 switch helped a lot in my answer. By default it waits 20 seconds for a timeout which could make this task take a really long time if a lot of nodes are down.

    – Jesse_b
    Aug 11 '17 at 12:30













  • I'll remove it since should work without -q just as well.

    – sebasth
    Aug 11 '17 at 12:32












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f385453%2fping-hosts-based-on-filter-and-return-dead-or-alive%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














I believe this will do what you want:



pingme () {
hostfile="/home/jbutryn/test/hostfile.txt"
IFS= mapfile -t hosts < <(cat $hostfile)
for host in "${hosts[@]}"; do
match=$(echo "$host" | grep -o "-$1_" | sed 's/-//' | sed 's/_//')
if [[ "$match" = "$1" ]]; then
hostname=$(echo "$host" | awk '{print $2}')
ping -c1 -W1 $(echo "$host" | awk '{print $1}') > /dev/null
if [[ $? = 0 ]]; then
echo "$hostname is alive"
elif [[ $? = 1 ]]; then
echo "$hostname is dead"
fi
fi
done
}


You can run this function like: pingme UR01 and it should return something like:



[root@JBCLAMP001 test]# pingme CC
BB-CC_DD is dead
AA-CC_EE is alive
[root@JBCLAMP001 test]# pingme DD
CC-DD_EE is dead
CC-DD_JJ is alive





share|improve this answer


























  • I suspect this is exactly what is needed. Is that shell though ....cause I got it on shell and produced me an error ( line 1: syntax error near unexpected token `{ )

    – Pseudonymity
    Aug 11 '17 at 12:40











  • Uhm. Much of it is in bash syntax. In order to use it you can add it to a file and source file or . file. Or if you want it to be a more permanent solution you can source that file from your ~/.bashrc.

    – Jesse_b
    Aug 11 '17 at 12:41













  • Additionally ensure the file doesn't have any windows special characters in it. I wrote that with LF line breaks but it was pasted to the site on a windows machine so it may have added some CR characters too. You can check this with a text editor like notepadd++ or similar.

    – Jesse_b
    Aug 11 '17 at 13:03











  • I have tried and tried and its not working . Tried modifying it - noting . i have 2.6.32-754.2.1.el6.x86_64 Linux , I have the file in .sh , run it via ./ , the txt file I tried to change the order , small letters , big letters- nothing, no errors no nothing as it isnt reading the file.

    – Pseudonymity
    Sep 20 '18 at 9:03


















0














I believe this will do what you want:



pingme () {
hostfile="/home/jbutryn/test/hostfile.txt"
IFS= mapfile -t hosts < <(cat $hostfile)
for host in "${hosts[@]}"; do
match=$(echo "$host" | grep -o "-$1_" | sed 's/-//' | sed 's/_//')
if [[ "$match" = "$1" ]]; then
hostname=$(echo "$host" | awk '{print $2}')
ping -c1 -W1 $(echo "$host" | awk '{print $1}') > /dev/null
if [[ $? = 0 ]]; then
echo "$hostname is alive"
elif [[ $? = 1 ]]; then
echo "$hostname is dead"
fi
fi
done
}


You can run this function like: pingme UR01 and it should return something like:



[root@JBCLAMP001 test]# pingme CC
BB-CC_DD is dead
AA-CC_EE is alive
[root@JBCLAMP001 test]# pingme DD
CC-DD_EE is dead
CC-DD_JJ is alive





share|improve this answer


























  • I suspect this is exactly what is needed. Is that shell though ....cause I got it on shell and produced me an error ( line 1: syntax error near unexpected token `{ )

    – Pseudonymity
    Aug 11 '17 at 12:40











  • Uhm. Much of it is in bash syntax. In order to use it you can add it to a file and source file or . file. Or if you want it to be a more permanent solution you can source that file from your ~/.bashrc.

    – Jesse_b
    Aug 11 '17 at 12:41













  • Additionally ensure the file doesn't have any windows special characters in it. I wrote that with LF line breaks but it was pasted to the site on a windows machine so it may have added some CR characters too. You can check this with a text editor like notepadd++ or similar.

    – Jesse_b
    Aug 11 '17 at 13:03











  • I have tried and tried and its not working . Tried modifying it - noting . i have 2.6.32-754.2.1.el6.x86_64 Linux , I have the file in .sh , run it via ./ , the txt file I tried to change the order , small letters , big letters- nothing, no errors no nothing as it isnt reading the file.

    – Pseudonymity
    Sep 20 '18 at 9:03
















0












0








0







I believe this will do what you want:



pingme () {
hostfile="/home/jbutryn/test/hostfile.txt"
IFS= mapfile -t hosts < <(cat $hostfile)
for host in "${hosts[@]}"; do
match=$(echo "$host" | grep -o "-$1_" | sed 's/-//' | sed 's/_//')
if [[ "$match" = "$1" ]]; then
hostname=$(echo "$host" | awk '{print $2}')
ping -c1 -W1 $(echo "$host" | awk '{print $1}') > /dev/null
if [[ $? = 0 ]]; then
echo "$hostname is alive"
elif [[ $? = 1 ]]; then
echo "$hostname is dead"
fi
fi
done
}


You can run this function like: pingme UR01 and it should return something like:



[root@JBCLAMP001 test]# pingme CC
BB-CC_DD is dead
AA-CC_EE is alive
[root@JBCLAMP001 test]# pingme DD
CC-DD_EE is dead
CC-DD_JJ is alive





share|improve this answer















I believe this will do what you want:



pingme () {
hostfile="/home/jbutryn/test/hostfile.txt"
IFS= mapfile -t hosts < <(cat $hostfile)
for host in "${hosts[@]}"; do
match=$(echo "$host" | grep -o "-$1_" | sed 's/-//' | sed 's/_//')
if [[ "$match" = "$1" ]]; then
hostname=$(echo "$host" | awk '{print $2}')
ping -c1 -W1 $(echo "$host" | awk '{print $1}') > /dev/null
if [[ $? = 0 ]]; then
echo "$hostname is alive"
elif [[ $? = 1 ]]; then
echo "$hostname is dead"
fi
fi
done
}


You can run this function like: pingme UR01 and it should return something like:



[root@JBCLAMP001 test]# pingme CC
BB-CC_DD is dead
AA-CC_EE is alive
[root@JBCLAMP001 test]# pingme DD
CC-DD_EE is dead
CC-DD_JJ is alive






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 11 '17 at 12:27

























answered Aug 11 '17 at 12:20









Jesse_bJesse_b

16.1k34078




16.1k34078













  • I suspect this is exactly what is needed. Is that shell though ....cause I got it on shell and produced me an error ( line 1: syntax error near unexpected token `{ )

    – Pseudonymity
    Aug 11 '17 at 12:40











  • Uhm. Much of it is in bash syntax. In order to use it you can add it to a file and source file or . file. Or if you want it to be a more permanent solution you can source that file from your ~/.bashrc.

    – Jesse_b
    Aug 11 '17 at 12:41













  • Additionally ensure the file doesn't have any windows special characters in it. I wrote that with LF line breaks but it was pasted to the site on a windows machine so it may have added some CR characters too. You can check this with a text editor like notepadd++ or similar.

    – Jesse_b
    Aug 11 '17 at 13:03











  • I have tried and tried and its not working . Tried modifying it - noting . i have 2.6.32-754.2.1.el6.x86_64 Linux , I have the file in .sh , run it via ./ , the txt file I tried to change the order , small letters , big letters- nothing, no errors no nothing as it isnt reading the file.

    – Pseudonymity
    Sep 20 '18 at 9:03





















  • I suspect this is exactly what is needed. Is that shell though ....cause I got it on shell and produced me an error ( line 1: syntax error near unexpected token `{ )

    – Pseudonymity
    Aug 11 '17 at 12:40











  • Uhm. Much of it is in bash syntax. In order to use it you can add it to a file and source file or . file. Or if you want it to be a more permanent solution you can source that file from your ~/.bashrc.

    – Jesse_b
    Aug 11 '17 at 12:41













  • Additionally ensure the file doesn't have any windows special characters in it. I wrote that with LF line breaks but it was pasted to the site on a windows machine so it may have added some CR characters too. You can check this with a text editor like notepadd++ or similar.

    – Jesse_b
    Aug 11 '17 at 13:03











  • I have tried and tried and its not working . Tried modifying it - noting . i have 2.6.32-754.2.1.el6.x86_64 Linux , I have the file in .sh , run it via ./ , the txt file I tried to change the order , small letters , big letters- nothing, no errors no nothing as it isnt reading the file.

    – Pseudonymity
    Sep 20 '18 at 9:03



















I suspect this is exactly what is needed. Is that shell though ....cause I got it on shell and produced me an error ( line 1: syntax error near unexpected token `{ )

– Pseudonymity
Aug 11 '17 at 12:40





I suspect this is exactly what is needed. Is that shell though ....cause I got it on shell and produced me an error ( line 1: syntax error near unexpected token `{ )

– Pseudonymity
Aug 11 '17 at 12:40













Uhm. Much of it is in bash syntax. In order to use it you can add it to a file and source file or . file. Or if you want it to be a more permanent solution you can source that file from your ~/.bashrc.

– Jesse_b
Aug 11 '17 at 12:41







Uhm. Much of it is in bash syntax. In order to use it you can add it to a file and source file or . file. Or if you want it to be a more permanent solution you can source that file from your ~/.bashrc.

– Jesse_b
Aug 11 '17 at 12:41















Additionally ensure the file doesn't have any windows special characters in it. I wrote that with LF line breaks but it was pasted to the site on a windows machine so it may have added some CR characters too. You can check this with a text editor like notepadd++ or similar.

– Jesse_b
Aug 11 '17 at 13:03





Additionally ensure the file doesn't have any windows special characters in it. I wrote that with LF line breaks but it was pasted to the site on a windows machine so it may have added some CR characters too. You can check this with a text editor like notepadd++ or similar.

– Jesse_b
Aug 11 '17 at 13:03













I have tried and tried and its not working . Tried modifying it - noting . i have 2.6.32-754.2.1.el6.x86_64 Linux , I have the file in .sh , run it via ./ , the txt file I tried to change the order , small letters , big letters- nothing, no errors no nothing as it isnt reading the file.

– Pseudonymity
Sep 20 '18 at 9:03







I have tried and tried and its not working . Tried modifying it - noting . i have 2.6.32-754.2.1.el6.x86_64 Linux , I have the file in .sh , run it via ./ , the txt file I tried to change the order , small letters , big letters- nothing, no errors no nothing as it isnt reading the file.

– Pseudonymity
Sep 20 '18 at 9:03















0














You can use the exit code from ping to determine if ping succeeded or failed.



ping HOST -c1 > /dev/null && echo "ALIVE" || echo "DEAD"


When host is alive exit code is 0, and for dead 1. To ping every host you can loop each line and use awk to get the first column which contains address/hostname.



exec 3<input.txt

while read -u3 line
do
if [ "$line" == "" ]; then
# skip empty lines
continue
fi

ping -c1 $(echo "$line"| awk '{print $1}') > /dev/null

if [ $? = 0 ]; then
echo "$line=ALIVE"
else
echo "$line=DEAD"
fi
done


EDIT:



If you want only to ping host by matching a single line in the file, you can grep it:



# find line for $host, only 1 line
line = grep "$HOST" input.txt |tail -n 1

# $($line |awk '{print $1}') outputs the first column of line (address)
ping -c1 $($line |awk '{print $1}') >/dev/null && echo "$line=ALIVE" || echo "$line=DEAD"





share|improve this answer


























  • Yes but I have multiple hosts per location. So HOST = what is in common in the name of the hosts, correct ? Where exactly in script is the host referance though....I mean if I specify it in the command under HOST ?

    – Pseudonymity
    Aug 11 '17 at 11:39











  • So you have more than one host per line? $(echo "$line"| awk '{print $1}') outputs the first column in the row (assumed hostname/ip address).

    – sebasth
    Aug 11 '17 at 11:41











  • I don't think the -q switch is available in redhat ping. It's not on centos 7 anyway. Also the -W1 switch helped a lot in my answer. By default it waits 20 seconds for a timeout which could make this task take a really long time if a lot of nodes are down.

    – Jesse_b
    Aug 11 '17 at 12:30













  • I'll remove it since should work without -q just as well.

    – sebasth
    Aug 11 '17 at 12:32
















0














You can use the exit code from ping to determine if ping succeeded or failed.



ping HOST -c1 > /dev/null && echo "ALIVE" || echo "DEAD"


When host is alive exit code is 0, and for dead 1. To ping every host you can loop each line and use awk to get the first column which contains address/hostname.



exec 3<input.txt

while read -u3 line
do
if [ "$line" == "" ]; then
# skip empty lines
continue
fi

ping -c1 $(echo "$line"| awk '{print $1}') > /dev/null

if [ $? = 0 ]; then
echo "$line=ALIVE"
else
echo "$line=DEAD"
fi
done


EDIT:



If you want only to ping host by matching a single line in the file, you can grep it:



# find line for $host, only 1 line
line = grep "$HOST" input.txt |tail -n 1

# $($line |awk '{print $1}') outputs the first column of line (address)
ping -c1 $($line |awk '{print $1}') >/dev/null && echo "$line=ALIVE" || echo "$line=DEAD"





share|improve this answer


























  • Yes but I have multiple hosts per location. So HOST = what is in common in the name of the hosts, correct ? Where exactly in script is the host referance though....I mean if I specify it in the command under HOST ?

    – Pseudonymity
    Aug 11 '17 at 11:39











  • So you have more than one host per line? $(echo "$line"| awk '{print $1}') outputs the first column in the row (assumed hostname/ip address).

    – sebasth
    Aug 11 '17 at 11:41











  • I don't think the -q switch is available in redhat ping. It's not on centos 7 anyway. Also the -W1 switch helped a lot in my answer. By default it waits 20 seconds for a timeout which could make this task take a really long time if a lot of nodes are down.

    – Jesse_b
    Aug 11 '17 at 12:30













  • I'll remove it since should work without -q just as well.

    – sebasth
    Aug 11 '17 at 12:32














0












0








0







You can use the exit code from ping to determine if ping succeeded or failed.



ping HOST -c1 > /dev/null && echo "ALIVE" || echo "DEAD"


When host is alive exit code is 0, and for dead 1. To ping every host you can loop each line and use awk to get the first column which contains address/hostname.



exec 3<input.txt

while read -u3 line
do
if [ "$line" == "" ]; then
# skip empty lines
continue
fi

ping -c1 $(echo "$line"| awk '{print $1}') > /dev/null

if [ $? = 0 ]; then
echo "$line=ALIVE"
else
echo "$line=DEAD"
fi
done


EDIT:



If you want only to ping host by matching a single line in the file, you can grep it:



# find line for $host, only 1 line
line = grep "$HOST" input.txt |tail -n 1

# $($line |awk '{print $1}') outputs the first column of line (address)
ping -c1 $($line |awk '{print $1}') >/dev/null && echo "$line=ALIVE" || echo "$line=DEAD"





share|improve this answer















You can use the exit code from ping to determine if ping succeeded or failed.



ping HOST -c1 > /dev/null && echo "ALIVE" || echo "DEAD"


When host is alive exit code is 0, and for dead 1. To ping every host you can loop each line and use awk to get the first column which contains address/hostname.



exec 3<input.txt

while read -u3 line
do
if [ "$line" == "" ]; then
# skip empty lines
continue
fi

ping -c1 $(echo "$line"| awk '{print $1}') > /dev/null

if [ $? = 0 ]; then
echo "$line=ALIVE"
else
echo "$line=DEAD"
fi
done


EDIT:



If you want only to ping host by matching a single line in the file, you can grep it:



# find line for $host, only 1 line
line = grep "$HOST" input.txt |tail -n 1

# $($line |awk '{print $1}') outputs the first column of line (address)
ping -c1 $($line |awk '{print $1}') >/dev/null && echo "$line=ALIVE" || echo "$line=DEAD"






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 11 '17 at 12:53

























answered Aug 11 '17 at 11:32









sebasthsebasth

9,02732550




9,02732550













  • Yes but I have multiple hosts per location. So HOST = what is in common in the name of the hosts, correct ? Where exactly in script is the host referance though....I mean if I specify it in the command under HOST ?

    – Pseudonymity
    Aug 11 '17 at 11:39











  • So you have more than one host per line? $(echo "$line"| awk '{print $1}') outputs the first column in the row (assumed hostname/ip address).

    – sebasth
    Aug 11 '17 at 11:41











  • I don't think the -q switch is available in redhat ping. It's not on centos 7 anyway. Also the -W1 switch helped a lot in my answer. By default it waits 20 seconds for a timeout which could make this task take a really long time if a lot of nodes are down.

    – Jesse_b
    Aug 11 '17 at 12:30













  • I'll remove it since should work without -q just as well.

    – sebasth
    Aug 11 '17 at 12:32



















  • Yes but I have multiple hosts per location. So HOST = what is in common in the name of the hosts, correct ? Where exactly in script is the host referance though....I mean if I specify it in the command under HOST ?

    – Pseudonymity
    Aug 11 '17 at 11:39











  • So you have more than one host per line? $(echo "$line"| awk '{print $1}') outputs the first column in the row (assumed hostname/ip address).

    – sebasth
    Aug 11 '17 at 11:41











  • I don't think the -q switch is available in redhat ping. It's not on centos 7 anyway. Also the -W1 switch helped a lot in my answer. By default it waits 20 seconds for a timeout which could make this task take a really long time if a lot of nodes are down.

    – Jesse_b
    Aug 11 '17 at 12:30













  • I'll remove it since should work without -q just as well.

    – sebasth
    Aug 11 '17 at 12:32

















Yes but I have multiple hosts per location. So HOST = what is in common in the name of the hosts, correct ? Where exactly in script is the host referance though....I mean if I specify it in the command under HOST ?

– Pseudonymity
Aug 11 '17 at 11:39





Yes but I have multiple hosts per location. So HOST = what is in common in the name of the hosts, correct ? Where exactly in script is the host referance though....I mean if I specify it in the command under HOST ?

– Pseudonymity
Aug 11 '17 at 11:39













So you have more than one host per line? $(echo "$line"| awk '{print $1}') outputs the first column in the row (assumed hostname/ip address).

– sebasth
Aug 11 '17 at 11:41





So you have more than one host per line? $(echo "$line"| awk '{print $1}') outputs the first column in the row (assumed hostname/ip address).

– sebasth
Aug 11 '17 at 11:41













I don't think the -q switch is available in redhat ping. It's not on centos 7 anyway. Also the -W1 switch helped a lot in my answer. By default it waits 20 seconds for a timeout which could make this task take a really long time if a lot of nodes are down.

– Jesse_b
Aug 11 '17 at 12:30







I don't think the -q switch is available in redhat ping. It's not on centos 7 anyway. Also the -W1 switch helped a lot in my answer. By default it waits 20 seconds for a timeout which could make this task take a really long time if a lot of nodes are down.

– Jesse_b
Aug 11 '17 at 12:30















I'll remove it since should work without -q just as well.

– sebasth
Aug 11 '17 at 12:32





I'll remove it since should work without -q just as well.

– sebasth
Aug 11 '17 at 12:32


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • 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%2funix.stackexchange.com%2fquestions%2f385453%2fping-hosts-based-on-filter-and-return-dead-or-alive%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

Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...