Suppress âName or service not knownâ on ping

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have several Raspberry Pi's named numerically (pi0, pi1, etc). They have static IP addresses, but I wanted a simple tool to check on them and make sure they were online even if they got the wrong IP (I've had some trouble in the past. Nothing recently, but it seemed like a good idea to make it foolproof regardless). Instead of using IPs, this tool uses MDNS hostnames that are already in a nice format. The script I wrote is:
#!/bin/bash
report+="Device Name Status Locationn"
report+="----------- ------ --------n"
for i in 0..3
do
report+="Pi$i "
ping -c1 pi$i.local > /dev/null
if [ $? == 0 ]
then
report+="Online "
report+=$(ping -c1 pi$i.local | grep -o "w*192.168.1.w*" | head -1)
else
report+="Unreachable"
fi
report+="n"
done
echo -e "$report"
It works wonderfully when devices are online, returning
Device Name Status Location
----------- ------ --------
Pi0 Online 192.168.1.3
Pi1 Online 192.168.1.4
Pi2 Online 192.168.1.5
Pi3 Online 192.168.1.6
However, when they are offline the output looks like this
ping: pi0.local: Name or service not known
ping: pi1.local: Name or service not known
ping: pi2.local: Name or service not known
ping: pi3.local: Name or service not known
Device Name Status Location
----------- ------ --------
Pi0 Unreachable
Pi1 Unreachable
Pi2 Unreachable
Pi3 Unreachable
Is there a way to suppress the "Name or service not known" for the MDNS lookup on the ping? I already direct output to /dev/null, so I don't see how it is still giving output.
Alternatively, if you have a better/faster/easier/prettier way to do this checking, I'd love to hear it.
io-redirection ping hostname mdns
add a comment |Â
up vote
1
down vote
favorite
I have several Raspberry Pi's named numerically (pi0, pi1, etc). They have static IP addresses, but I wanted a simple tool to check on them and make sure they were online even if they got the wrong IP (I've had some trouble in the past. Nothing recently, but it seemed like a good idea to make it foolproof regardless). Instead of using IPs, this tool uses MDNS hostnames that are already in a nice format. The script I wrote is:
#!/bin/bash
report+="Device Name Status Locationn"
report+="----------- ------ --------n"
for i in 0..3
do
report+="Pi$i "
ping -c1 pi$i.local > /dev/null
if [ $? == 0 ]
then
report+="Online "
report+=$(ping -c1 pi$i.local | grep -o "w*192.168.1.w*" | head -1)
else
report+="Unreachable"
fi
report+="n"
done
echo -e "$report"
It works wonderfully when devices are online, returning
Device Name Status Location
----------- ------ --------
Pi0 Online 192.168.1.3
Pi1 Online 192.168.1.4
Pi2 Online 192.168.1.5
Pi3 Online 192.168.1.6
However, when they are offline the output looks like this
ping: pi0.local: Name or service not known
ping: pi1.local: Name or service not known
ping: pi2.local: Name or service not known
ping: pi3.local: Name or service not known
Device Name Status Location
----------- ------ --------
Pi0 Unreachable
Pi1 Unreachable
Pi2 Unreachable
Pi3 Unreachable
Is there a way to suppress the "Name or service not known" for the MDNS lookup on the ping? I already direct output to /dev/null, so I don't see how it is still giving output.
Alternatively, if you have a better/faster/easier/prettier way to do this checking, I'd love to hear it.
io-redirection ping hostname mdns
1
Add2>&1after your> /dev/null.
â dsstorefile1
May 10 at 0:36
1
Redirectstderrto/dev/null?ping -c1 pi$i.local > /dev/null 2> /dev/null... of course, there are other ways of redirecting (redirect error to out, redirect out to null like you are doing, etc)
â ivanivan
May 10 at 0:37
1
@dsstorefile1 not gonna lie, I'm a little disappointed this was so simple. Guess my google-fu needs a little improvement!
â brndn2k
May 10 at 0:40
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have several Raspberry Pi's named numerically (pi0, pi1, etc). They have static IP addresses, but I wanted a simple tool to check on them and make sure they were online even if they got the wrong IP (I've had some trouble in the past. Nothing recently, but it seemed like a good idea to make it foolproof regardless). Instead of using IPs, this tool uses MDNS hostnames that are already in a nice format. The script I wrote is:
#!/bin/bash
report+="Device Name Status Locationn"
report+="----------- ------ --------n"
for i in 0..3
do
report+="Pi$i "
ping -c1 pi$i.local > /dev/null
if [ $? == 0 ]
then
report+="Online "
report+=$(ping -c1 pi$i.local | grep -o "w*192.168.1.w*" | head -1)
else
report+="Unreachable"
fi
report+="n"
done
echo -e "$report"
It works wonderfully when devices are online, returning
Device Name Status Location
----------- ------ --------
Pi0 Online 192.168.1.3
Pi1 Online 192.168.1.4
Pi2 Online 192.168.1.5
Pi3 Online 192.168.1.6
However, when they are offline the output looks like this
ping: pi0.local: Name or service not known
ping: pi1.local: Name or service not known
ping: pi2.local: Name or service not known
ping: pi3.local: Name or service not known
Device Name Status Location
----------- ------ --------
Pi0 Unreachable
Pi1 Unreachable
Pi2 Unreachable
Pi3 Unreachable
Is there a way to suppress the "Name or service not known" for the MDNS lookup on the ping? I already direct output to /dev/null, so I don't see how it is still giving output.
Alternatively, if you have a better/faster/easier/prettier way to do this checking, I'd love to hear it.
io-redirection ping hostname mdns
I have several Raspberry Pi's named numerically (pi0, pi1, etc). They have static IP addresses, but I wanted a simple tool to check on them and make sure they were online even if they got the wrong IP (I've had some trouble in the past. Nothing recently, but it seemed like a good idea to make it foolproof regardless). Instead of using IPs, this tool uses MDNS hostnames that are already in a nice format. The script I wrote is:
#!/bin/bash
report+="Device Name Status Locationn"
report+="----------- ------ --------n"
for i in 0..3
do
report+="Pi$i "
ping -c1 pi$i.local > /dev/null
if [ $? == 0 ]
then
report+="Online "
report+=$(ping -c1 pi$i.local | grep -o "w*192.168.1.w*" | head -1)
else
report+="Unreachable"
fi
report+="n"
done
echo -e "$report"
It works wonderfully when devices are online, returning
Device Name Status Location
----------- ------ --------
Pi0 Online 192.168.1.3
Pi1 Online 192.168.1.4
Pi2 Online 192.168.1.5
Pi3 Online 192.168.1.6
However, when they are offline the output looks like this
ping: pi0.local: Name or service not known
ping: pi1.local: Name or service not known
ping: pi2.local: Name or service not known
ping: pi3.local: Name or service not known
Device Name Status Location
----------- ------ --------
Pi0 Unreachable
Pi1 Unreachable
Pi2 Unreachable
Pi3 Unreachable
Is there a way to suppress the "Name or service not known" for the MDNS lookup on the ping? I already direct output to /dev/null, so I don't see how it is still giving output.
Alternatively, if you have a better/faster/easier/prettier way to do this checking, I'd love to hear it.
io-redirection ping hostname mdns
edited May 10 at 11:06
Jeff Schaller
31.1k846105
31.1k846105
asked May 10 at 0:32
brndn2k
695
695
1
Add2>&1after your> /dev/null.
â dsstorefile1
May 10 at 0:36
1
Redirectstderrto/dev/null?ping -c1 pi$i.local > /dev/null 2> /dev/null... of course, there are other ways of redirecting (redirect error to out, redirect out to null like you are doing, etc)
â ivanivan
May 10 at 0:37
1
@dsstorefile1 not gonna lie, I'm a little disappointed this was so simple. Guess my google-fu needs a little improvement!
â brndn2k
May 10 at 0:40
add a comment |Â
1
Add2>&1after your> /dev/null.
â dsstorefile1
May 10 at 0:36
1
Redirectstderrto/dev/null?ping -c1 pi$i.local > /dev/null 2> /dev/null... of course, there are other ways of redirecting (redirect error to out, redirect out to null like you are doing, etc)
â ivanivan
May 10 at 0:37
1
@dsstorefile1 not gonna lie, I'm a little disappointed this was so simple. Guess my google-fu needs a little improvement!
â brndn2k
May 10 at 0:40
1
1
Add
2>&1 after your > /dev/null.â dsstorefile1
May 10 at 0:36
Add
2>&1 after your > /dev/null.â dsstorefile1
May 10 at 0:36
1
1
Redirect
stderr to /dev/null ? ping -c1 pi$i.local > /dev/null 2> /dev/null ... of course, there are other ways of redirecting (redirect error to out, redirect out to null like you are doing, etc)â ivanivan
May 10 at 0:37
Redirect
stderr to /dev/null ? ping -c1 pi$i.local > /dev/null 2> /dev/null ... of course, there are other ways of redirecting (redirect error to out, redirect out to null like you are doing, etc)â ivanivan
May 10 at 0:37
1
1
@dsstorefile1 not gonna lie, I'm a little disappointed this was so simple. Guess my google-fu needs a little improvement!
â brndn2k
May 10 at 0:40
@dsstorefile1 not gonna lie, I'm a little disappointed this was so simple. Guess my google-fu needs a little improvement!
â brndn2k
May 10 at 0:40
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
As was briefly pointed out in the comments, ping is displaying that message to stderr, which you had not redirected. Do so with:
...
ping -c1 pi$i.local > /dev/null 2>&1
...
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
As was briefly pointed out in the comments, ping is displaying that message to stderr, which you had not redirected. Do so with:
...
ping -c1 pi$i.local > /dev/null 2>&1
...
add a comment |Â
up vote
1
down vote
accepted
As was briefly pointed out in the comments, ping is displaying that message to stderr, which you had not redirected. Do so with:
...
ping -c1 pi$i.local > /dev/null 2>&1
...
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
As was briefly pointed out in the comments, ping is displaying that message to stderr, which you had not redirected. Do so with:
...
ping -c1 pi$i.local > /dev/null 2>&1
...
As was briefly pointed out in the comments, ping is displaying that message to stderr, which you had not redirected. Do so with:
...
ping -c1 pi$i.local > /dev/null 2>&1
...
answered May 10 at 11:05
Jeff Schaller
31.1k846105
31.1k846105
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442894%2fsuppress-name-or-service-not-known-on-ping%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
Add
2>&1after your> /dev/null.â dsstorefile1
May 10 at 0:36
1
Redirect
stderrto/dev/null?ping -c1 pi$i.local > /dev/null 2> /dev/null... of course, there are other ways of redirecting (redirect error to out, redirect out to null like you are doing, etc)â ivanivan
May 10 at 0:37
1
@dsstorefile1 not gonna lie, I'm a little disappointed this was so simple. Guess my google-fu needs a little improvement!
â brndn2k
May 10 at 0:40