Linux bash script to check internet connection not workingGet the source directory of a Bash script from within the script itselfHow do I prompt for Yes/No/Cancel input in a Linux shell script?How to check if a string contains a substring in BashHow to iterate over arguments in a Bash scriptHow to check if a program exists from a Bash script?Pipe to/from the clipboard in Bash scriptHow to detect the physical connected state of a network cable/connector?How to check if a variable is set in Bash?Check existence of input argument in a Bash shell scriptCheck number of arguments passed to a Bash script
Can you board the plane when your passport is valid less than 3 months?
Gambler coin problem: fair coin and two-headed coin
Cooking Scrambled Eggs
Can Orcus use Multiattack with any melee weapon?
How does the OS tell whether an "Address is already in use"?
1mth baby boy keeps peeing through diapers, sometimes diper seems practically unused
Given current technology, could TV display screens double as video camera sensors?
Is it legal for source code containing undefined behavior to crash the compiler?
What are the occurences of total war in the Native Americans?
What is the loud noise of a helicopter when the rotors are not yet moving?
Number of academics in various EU countries
Hangman game in Python - need feedback on the quality of code
Why can't you reverse the order of the input redirection operator for while loops?
Discussing work with supervisor in an invited dinner with his family
Why error propagation in CBC mode encryption affect two blocks?
How do I feed my black hole?
Adoption records in 1871 based on census info
Under what circumstances does intermodulation become an issue in a filter after LNA setup?
Is one hour layover sufficient at Singapore Changi Airport (Indian citizen travelling from US to India)?
How do you capitalize agile costs with less mature teams?
What does it take for witness testimony to be believed?
50-move rule: only the last 50 or any consecutive 50?
Why does a sticker slowly peel off, but if it is pulled quickly it tears?
Why should a self-financing strategy be previsible?
Linux bash script to check internet connection not working
Get the source directory of a Bash script from within the script itselfHow do I prompt for Yes/No/Cancel input in a Linux shell script?How to check if a string contains a substring in BashHow to iterate over arguments in a Bash scriptHow to check if a program exists from a Bash script?Pipe to/from the clipboard in Bash scriptHow to detect the physical connected state of a network cable/connector?How to check if a variable is set in Bash?Check existence of input argument in a Bash shell scriptCheck number of arguments passed to a Bash script
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to use a bash script on my raspberry pi to check whether the internet connection is still present.
I found this script which I extended:
#!/bin/bash
# I do not work properly
if [[ "$(ping -c 1 8.8.8.8 | grep 'Network is unreachable' )" != "" ]]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
fi
# I work properly
if [[ "$(ping -c 1 8.8.8.8 | grep '100% packet loss' )" != "" ]]; then
echo "Internet isn't present"
sudo shutdown -r 0
exit 1
else
echo "Internet is present"
exit 0
fi
The second part works nice, the first one, however, does not.
I unplugged my Ethernet cable and get an Error message like connect: Network is unreachable
. So as expected. But I am not understanding why my script is not picking it up? I think it has something to do with it being an error message and not an output. But I do not know how I would have to adjust my script to grep from error messages as well, assuming this is correct.
I found a way how to get the output to be registered by grep by using this line ping -c 1 8.8.8.8 2> >(grep 'unreachable';)
. However, this still makes my script fail because it is no longer recognized by the rest of that line then. And I also do not entirely understand what 2> >()
does.
linux bash grep connection ping
add a comment |
I want to use a bash script on my raspberry pi to check whether the internet connection is still present.
I found this script which I extended:
#!/bin/bash
# I do not work properly
if [[ "$(ping -c 1 8.8.8.8 | grep 'Network is unreachable' )" != "" ]]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
fi
# I work properly
if [[ "$(ping -c 1 8.8.8.8 | grep '100% packet loss' )" != "" ]]; then
echo "Internet isn't present"
sudo shutdown -r 0
exit 1
else
echo "Internet is present"
exit 0
fi
The second part works nice, the first one, however, does not.
I unplugged my Ethernet cable and get an Error message like connect: Network is unreachable
. So as expected. But I am not understanding why my script is not picking it up? I think it has something to do with it being an error message and not an output. But I do not know how I would have to adjust my script to grep from error messages as well, assuming this is correct.
I found a way how to get the output to be registered by grep by using this line ping -c 1 8.8.8.8 2> >(grep 'unreachable';)
. However, this still makes my script fail because it is no longer recognized by the rest of that line then. And I also do not entirely understand what 2> >()
does.
linux bash grep connection ping
add a comment |
I want to use a bash script on my raspberry pi to check whether the internet connection is still present.
I found this script which I extended:
#!/bin/bash
# I do not work properly
if [[ "$(ping -c 1 8.8.8.8 | grep 'Network is unreachable' )" != "" ]]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
fi
# I work properly
if [[ "$(ping -c 1 8.8.8.8 | grep '100% packet loss' )" != "" ]]; then
echo "Internet isn't present"
sudo shutdown -r 0
exit 1
else
echo "Internet is present"
exit 0
fi
The second part works nice, the first one, however, does not.
I unplugged my Ethernet cable and get an Error message like connect: Network is unreachable
. So as expected. But I am not understanding why my script is not picking it up? I think it has something to do with it being an error message and not an output. But I do not know how I would have to adjust my script to grep from error messages as well, assuming this is correct.
I found a way how to get the output to be registered by grep by using this line ping -c 1 8.8.8.8 2> >(grep 'unreachable';)
. However, this still makes my script fail because it is no longer recognized by the rest of that line then. And I also do not entirely understand what 2> >()
does.
linux bash grep connection ping
I want to use a bash script on my raspberry pi to check whether the internet connection is still present.
I found this script which I extended:
#!/bin/bash
# I do not work properly
if [[ "$(ping -c 1 8.8.8.8 | grep 'Network is unreachable' )" != "" ]]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
fi
# I work properly
if [[ "$(ping -c 1 8.8.8.8 | grep '100% packet loss' )" != "" ]]; then
echo "Internet isn't present"
sudo shutdown -r 0
exit 1
else
echo "Internet is present"
exit 0
fi
The second part works nice, the first one, however, does not.
I unplugged my Ethernet cable and get an Error message like connect: Network is unreachable
. So as expected. But I am not understanding why my script is not picking it up? I think it has something to do with it being an error message and not an output. But I do not know how I would have to adjust my script to grep from error messages as well, assuming this is correct.
I found a way how to get the output to be registered by grep by using this line ping -c 1 8.8.8.8 2> >(grep 'unreachable';)
. However, this still makes my script fail because it is no longer recognized by the rest of that line then. And I also do not entirely understand what 2> >()
does.
linux bash grep connection ping
linux bash grep connection ping
asked Mar 27 at 20:00
JRszJRsz
1,9863 gold badges19 silver badges39 bronze badges
1,9863 gold badges19 silver badges39 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You don't really need a string comparison here:
ping
command gives an appropriate return code after it completes execution.
So, you could use something like:
function check_connectivity()
local test_ip
local test_count
test_ip="8.8.8.8"
test_count=1
if ping -c $test_count $test_ip > /dev/null; then
echo "Have internet connectivity"
else
echo "Do not have connectivity"
fi
check_connectivity
Works like a charm and I understand it, nice and simple!
– JRsz
Mar 27 at 20:36
1
Glad it helped :)
– akskap
Mar 27 at 20:38
add a comment |
Try this:
x=`ping -c1 google.com 2>&1 | grep failure`
if [ ! "$x" = "" ]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
fi
Or for your script I would do:
if [[ "$(ping -c 1 8.8.8.8 | grep '100% packet loss' )" != "" ]]; then
echo "Internet isn't present"
sudo shutdown -r 0
exit 1
elif [[ "$(ping -c 1 8.8.8.8 | grep 'packet loss' | grep -o "1[0-9][0-9]+%")" != "" ]]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
else
echo "Internet is present"
exit 0
fi
But i recommending using /sys/class/net instead
# Test for network conection
for interface in $(ls /sys/class/net/ | grep -v lo);do
if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then OnLine=1;fi;done
if ! [ $OnLine ]; then echo "Internet isn't present" > /dev/stderr;sudo shutdown -r 0; exit; fi
Thank you for your answer. I need it to be as simple as possible and I do not really understand your recommended way. But I already got it solved :)
– JRsz
Mar 27 at 20:37
1
Good that you found a solution.. For sys/class/net: What: /sys/class/net/<iface>/carrier Description: Indicates the current physical link state of the interface. Posssible values are: 0: physical link is down 1: physical link is up Note: some special devices, e.g: bonding and team drivers will allow this attribute to be written to force a link state for operating correctly and designating another fallback interfac
– wuseman
Mar 27 at 20:40
1
You can find more info here kernel.org/doc/Documentation/ABI/testing/sysfs-class-net if you are interested.
– wuseman
Mar 27 at 20:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f55385534%2flinux-bash-script-to-check-internet-connection-not-working%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
You don't really need a string comparison here:
ping
command gives an appropriate return code after it completes execution.
So, you could use something like:
function check_connectivity()
local test_ip
local test_count
test_ip="8.8.8.8"
test_count=1
if ping -c $test_count $test_ip > /dev/null; then
echo "Have internet connectivity"
else
echo "Do not have connectivity"
fi
check_connectivity
Works like a charm and I understand it, nice and simple!
– JRsz
Mar 27 at 20:36
1
Glad it helped :)
– akskap
Mar 27 at 20:38
add a comment |
You don't really need a string comparison here:
ping
command gives an appropriate return code after it completes execution.
So, you could use something like:
function check_connectivity()
local test_ip
local test_count
test_ip="8.8.8.8"
test_count=1
if ping -c $test_count $test_ip > /dev/null; then
echo "Have internet connectivity"
else
echo "Do not have connectivity"
fi
check_connectivity
Works like a charm and I understand it, nice and simple!
– JRsz
Mar 27 at 20:36
1
Glad it helped :)
– akskap
Mar 27 at 20:38
add a comment |
You don't really need a string comparison here:
ping
command gives an appropriate return code after it completes execution.
So, you could use something like:
function check_connectivity()
local test_ip
local test_count
test_ip="8.8.8.8"
test_count=1
if ping -c $test_count $test_ip > /dev/null; then
echo "Have internet connectivity"
else
echo "Do not have connectivity"
fi
check_connectivity
You don't really need a string comparison here:
ping
command gives an appropriate return code after it completes execution.
So, you could use something like:
function check_connectivity()
local test_ip
local test_count
test_ip="8.8.8.8"
test_count=1
if ping -c $test_count $test_ip > /dev/null; then
echo "Have internet connectivity"
else
echo "Do not have connectivity"
fi
check_connectivity
edited Mar 27 at 20:24
answered Mar 27 at 20:11
akskapakskap
5534 silver badges11 bronze badges
5534 silver badges11 bronze badges
Works like a charm and I understand it, nice and simple!
– JRsz
Mar 27 at 20:36
1
Glad it helped :)
– akskap
Mar 27 at 20:38
add a comment |
Works like a charm and I understand it, nice and simple!
– JRsz
Mar 27 at 20:36
1
Glad it helped :)
– akskap
Mar 27 at 20:38
Works like a charm and I understand it, nice and simple!
– JRsz
Mar 27 at 20:36
Works like a charm and I understand it, nice and simple!
– JRsz
Mar 27 at 20:36
1
1
Glad it helped :)
– akskap
Mar 27 at 20:38
Glad it helped :)
– akskap
Mar 27 at 20:38
add a comment |
Try this:
x=`ping -c1 google.com 2>&1 | grep failure`
if [ ! "$x" = "" ]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
fi
Or for your script I would do:
if [[ "$(ping -c 1 8.8.8.8 | grep '100% packet loss' )" != "" ]]; then
echo "Internet isn't present"
sudo shutdown -r 0
exit 1
elif [[ "$(ping -c 1 8.8.8.8 | grep 'packet loss' | grep -o "1[0-9][0-9]+%")" != "" ]]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
else
echo "Internet is present"
exit 0
fi
But i recommending using /sys/class/net instead
# Test for network conection
for interface in $(ls /sys/class/net/ | grep -v lo);do
if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then OnLine=1;fi;done
if ! [ $OnLine ]; then echo "Internet isn't present" > /dev/stderr;sudo shutdown -r 0; exit; fi
Thank you for your answer. I need it to be as simple as possible and I do not really understand your recommended way. But I already got it solved :)
– JRsz
Mar 27 at 20:37
1
Good that you found a solution.. For sys/class/net: What: /sys/class/net/<iface>/carrier Description: Indicates the current physical link state of the interface. Posssible values are: 0: physical link is down 1: physical link is up Note: some special devices, e.g: bonding and team drivers will allow this attribute to be written to force a link state for operating correctly and designating another fallback interfac
– wuseman
Mar 27 at 20:40
1
You can find more info here kernel.org/doc/Documentation/ABI/testing/sysfs-class-net if you are interested.
– wuseman
Mar 27 at 20:41
add a comment |
Try this:
x=`ping -c1 google.com 2>&1 | grep failure`
if [ ! "$x" = "" ]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
fi
Or for your script I would do:
if [[ "$(ping -c 1 8.8.8.8 | grep '100% packet loss' )" != "" ]]; then
echo "Internet isn't present"
sudo shutdown -r 0
exit 1
elif [[ "$(ping -c 1 8.8.8.8 | grep 'packet loss' | grep -o "1[0-9][0-9]+%")" != "" ]]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
else
echo "Internet is present"
exit 0
fi
But i recommending using /sys/class/net instead
# Test for network conection
for interface in $(ls /sys/class/net/ | grep -v lo);do
if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then OnLine=1;fi;done
if ! [ $OnLine ]; then echo "Internet isn't present" > /dev/stderr;sudo shutdown -r 0; exit; fi
Thank you for your answer. I need it to be as simple as possible and I do not really understand your recommended way. But I already got it solved :)
– JRsz
Mar 27 at 20:37
1
Good that you found a solution.. For sys/class/net: What: /sys/class/net/<iface>/carrier Description: Indicates the current physical link state of the interface. Posssible values are: 0: physical link is down 1: physical link is up Note: some special devices, e.g: bonding and team drivers will allow this attribute to be written to force a link state for operating correctly and designating another fallback interfac
– wuseman
Mar 27 at 20:40
1
You can find more info here kernel.org/doc/Documentation/ABI/testing/sysfs-class-net if you are interested.
– wuseman
Mar 27 at 20:41
add a comment |
Try this:
x=`ping -c1 google.com 2>&1 | grep failure`
if [ ! "$x" = "" ]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
fi
Or for your script I would do:
if [[ "$(ping -c 1 8.8.8.8 | grep '100% packet loss' )" != "" ]]; then
echo "Internet isn't present"
sudo shutdown -r 0
exit 1
elif [[ "$(ping -c 1 8.8.8.8 | grep 'packet loss' | grep -o "1[0-9][0-9]+%")" != "" ]]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
else
echo "Internet is present"
exit 0
fi
But i recommending using /sys/class/net instead
# Test for network conection
for interface in $(ls /sys/class/net/ | grep -v lo);do
if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then OnLine=1;fi;done
if ! [ $OnLine ]; then echo "Internet isn't present" > /dev/stderr;sudo shutdown -r 0; exit; fi
Try this:
x=`ping -c1 google.com 2>&1 | grep failure`
if [ ! "$x" = "" ]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
fi
Or for your script I would do:
if [[ "$(ping -c 1 8.8.8.8 | grep '100% packet loss' )" != "" ]]; then
echo "Internet isn't present"
sudo shutdown -r 0
exit 1
elif [[ "$(ping -c 1 8.8.8.8 | grep 'packet loss' | grep -o "1[0-9][0-9]+%")" != "" ]]; then
echo "Network isn't present"
sudo shutdown -r 0
exit 1
else
echo "Internet is present"
exit 0
fi
But i recommending using /sys/class/net instead
# Test for network conection
for interface in $(ls /sys/class/net/ | grep -v lo);do
if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then OnLine=1;fi;done
if ! [ $OnLine ]; then echo "Internet isn't present" > /dev/stderr;sudo shutdown -r 0; exit; fi
edited Mar 27 at 20:35
answered Mar 27 at 20:08
wusemanwuseman
3431 silver badge9 bronze badges
3431 silver badge9 bronze badges
Thank you for your answer. I need it to be as simple as possible and I do not really understand your recommended way. But I already got it solved :)
– JRsz
Mar 27 at 20:37
1
Good that you found a solution.. For sys/class/net: What: /sys/class/net/<iface>/carrier Description: Indicates the current physical link state of the interface. Posssible values are: 0: physical link is down 1: physical link is up Note: some special devices, e.g: bonding and team drivers will allow this attribute to be written to force a link state for operating correctly and designating another fallback interfac
– wuseman
Mar 27 at 20:40
1
You can find more info here kernel.org/doc/Documentation/ABI/testing/sysfs-class-net if you are interested.
– wuseman
Mar 27 at 20:41
add a comment |
Thank you for your answer. I need it to be as simple as possible and I do not really understand your recommended way. But I already got it solved :)
– JRsz
Mar 27 at 20:37
1
Good that you found a solution.. For sys/class/net: What: /sys/class/net/<iface>/carrier Description: Indicates the current physical link state of the interface. Posssible values are: 0: physical link is down 1: physical link is up Note: some special devices, e.g: bonding and team drivers will allow this attribute to be written to force a link state for operating correctly and designating another fallback interfac
– wuseman
Mar 27 at 20:40
1
You can find more info here kernel.org/doc/Documentation/ABI/testing/sysfs-class-net if you are interested.
– wuseman
Mar 27 at 20:41
Thank you for your answer. I need it to be as simple as possible and I do not really understand your recommended way. But I already got it solved :)
– JRsz
Mar 27 at 20:37
Thank you for your answer. I need it to be as simple as possible and I do not really understand your recommended way. But I already got it solved :)
– JRsz
Mar 27 at 20:37
1
1
Good that you found a solution.. For sys/class/net: What: /sys/class/net/<iface>/carrier Description: Indicates the current physical link state of the interface. Posssible values are: 0: physical link is down 1: physical link is up Note: some special devices, e.g: bonding and team drivers will allow this attribute to be written to force a link state for operating correctly and designating another fallback interfac
– wuseman
Mar 27 at 20:40
Good that you found a solution.. For sys/class/net: What: /sys/class/net/<iface>/carrier Description: Indicates the current physical link state of the interface. Posssible values are: 0: physical link is down 1: physical link is up Note: some special devices, e.g: bonding and team drivers will allow this attribute to be written to force a link state for operating correctly and designating another fallback interfac
– wuseman
Mar 27 at 20:40
1
1
You can find more info here kernel.org/doc/Documentation/ABI/testing/sysfs-class-net if you are interested.
– wuseman
Mar 27 at 20:41
You can find more info here kernel.org/doc/Documentation/ABI/testing/sysfs-class-net if you are interested.
– wuseman
Mar 27 at 20:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55385534%2flinux-bash-script-to-check-internet-connection-not-working%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