[opensuse] Script to check interface & restart network service
Dear All, I need a script to check dsl0 availability and restart network daemon if the dsl0 not exist. Then put the script at cron job to run it every 30 minutes However, I never make BASH script before. Hope you will assist me how to make it. Thanks a lot for your kind help. Wong -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 02/10/2010 01:22 AM, Wong wrote:
Dear All,
I need a script to check dsl0 availability and restart network daemon if the dsl0 not exist. Then put the script at cron job to run it every 30 minutes
However, I never make BASH script before. Hope you will assist me how to make it.
Thanks a lot for your kind help.
Wong
Wong, At the very basic level and guessing your dsl0 is your internet, all you really need is a ping check to a reliable server that excepts pings and then just check your connection every 60 seconds or so. If it is just for your use, why use cron, just start the check from your ~/.bashrc. Things can get as complicated as you want to make them, but something as simple as: #!/bin/bash runfile=/tmp/chkdsl0.run cleanup() { rm $runfile } trap cleanup trap cleanup SIGINT SIGTERM EXIT if [[ ! -f $runfile ]]; then touch $runfile while :; do if ! ping -q -c1 yahoo.com &>/dev/null; then rcnetwork restart # (as root, or 'sudo rcnetwork restart' with sudo) fi sleep 60 done fi exit 0 call it say 'chkdsl0.sh' and put it in say ~/Documents. Make it executable with chmod +x chkdsl0.sh. Then just call it and background it in your ~./bashrc with the following statement: ~/Documents/chkdsl10.sh & and your will then execute a ping check against yahoo once every 60 seconds and if yahoo.com doesn't respond then you restart the network to regain dsl0. There are a lot of different ways to do this and this is just a very basic solution with a reasonable check to prevent duplicate check process with each start of a new shell that you can expand on. Good luck. -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Wed, 10 Feb 2010, David C. Rankin wrote:
On 02/10/2010 01:22 AM, Wong wrote: [..] #!/bin/bash runfile=/tmp/chkdsl0.run
Bad. Use /var/run/
cleanup() { rm $runfile }
Should be: rm -f "$runfile"
trap cleanup trap cleanup SIGINT SIGTERM EXIT
if [[ ! -f $runfile ]]; then
A simple: if ! test -f "$runfile"; then suffices and doesn't need bash.
touch $runfile
Should be: echo "$$" > "$runfile"
while :; do if ! ping -q -c1 yahoo.com &>/dev/null; then
You can also check for the interface (with ifconfig or ip link show), or ping the router, if you use one. I don't think yahoo'll like it to be pinged every 60 seconds. ifconfig: dev=dsl0 if LC_ALL=C /sbin/ifconfig "$dev" | grep -q 'UP.*MTU'; then ## interface is up else
rcnetwork restart # (as root, or 'sudo rcnetwork restart' with sudo) fi sleep 60 done fi
I can't look up or remember the 'ip' syntax (different system).
call it say 'chkdsl0.sh' and put it in say ~/Documents. Make it executable with chmod +x chkdsl0.sh. Then just call it and background it in your ~./bashrc with the following statement:
~/Documents/chkdsl10.sh &
That's what ~/bin/ is for! Such a script is definitely not a document.
and your will then execute a ping check against yahoo once every 60 seconds and if yahoo.com doesn't respond then you restart the network to regain dsl0. There are a lot of different ways to do this and this is just a very basic solution with a reasonable check to prevent duplicate check process with each start of a new shell that you can expand on. Good luck.
Start it in ~/.profile. But actually, I'd do the check in/from whatever actually needs the connection. Wong, could you elaborate on why you need to check the connection? HTH, -dnh -- So Linus, what are we doing tonight? The same thing we do every night Tux. Try to take over the world! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday, 2010-02-11 at 04:37 +0100, David Haller wrote:
Hello,
On Wed, 10 Feb 2010, David C. Rankin wrote:
while :; do if ! ping -q -c1 yahoo.com &>/dev/null; then
You can also check for the interface (with ifconfig or ip link show), or ping the router, if you use one. I don't think yahoo'll like it to be pinged every 60 seconds.
However, an external ping can keep the connection up on the ISP side, while a ping to the router does not always do. - -- Cheers, Carlos E. R. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAkt2dbsACgkQtTMYHG2NR9V/kQCfejjefWYhlK94X5j29IaNkaHY EJIAoI/LdyPgWlQf9s5oRY/p/kJ6W7FU =yfol -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hi Dave,
I need a script to check dsl0 availability and restart network daemon if the dsl0 not exist. Then put the script at cron job to run it every 30 minutes
Wong,
At the very basic level and guessing your dsl0 is your internet, all you really need is a ping check to a reliable server that excepts pings and then just check your connection every 60 seconds or so. If it is just for your use, why use cron, just start the check from your ~/.bashrc. Things can get as complicated as you want to make them, but something as simple as:
#!/bin/bash
runfile=/tmp/chkdsl0.run
cleanup() { rm $runfile }
trap cleanup trap cleanup SIGINT SIGTERM EXIT
if [[ ! -f $runfile ]]; then touch $runfile while :; do if ! ping -q -c1 yahoo.com &>/dev/null; then rcnetwork restart # (as root, or 'sudo rcnetwork restart' with sudo) fi sleep 60 done fi
exit 0
Thanks for your advise & useful script. I tried to run it foreground and get this message: /usr/local/sbin/dslcheck: line 9: trap: cleanup: invalid signal specification Is it oK? Please advise Thx & Rgds, Wong -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 11/02/10 04:03, Wong wrote:
Thanks for your advise & useful script.
I tried to run it foreground and get this message:
/usr/local/sbin/dslcheck: line 9: trap: cleanup: invalid signal specification That's because it should be INT TERM EXIT , not SIGINT SIGTERM EXIT. Also only one "trap cleanup" -- it seems to be duplicated for some reason.
Basically it should look like this trap cleanup INT TERM EXIT Regards, Tejas -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 11/02/10 04:03, Wong wrote:
Thanks for your advise & useful script.
I tried to run it foreground and get this message:
/usr/local/sbin/dslcheck: line 9: trap: cleanup: invalid signal specification That's because it should be INT TERM EXIT , not SIGINT SIGTERM EXIT. Also only one "trap cleanup" -- it seems to be duplicated for some reason.
Basically it should look like this trap cleanup INT TERM EXIT
Thanks Tejas, I will try the update Thx & Rgds, Wong -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 02/11/2010 03:35 AM, Wong wrote:
On 11/02/10 04:03, Wong wrote:
Thanks for your advise & useful script.
I tried to run it foreground and get this message:
/usr/local/sbin/dslcheck: line 9: trap: cleanup: invalid signal specification That's because it should be INT TERM EXIT , not SIGINT SIGTERM EXIT. Also only one "trap cleanup" -- it seems to be duplicated for some reason.
Basically it should look like this trap cleanup INT TERM EXIT
Thanks Tejas,
I will try the update
Thx & Rgds,
Wong
Wong, Also make the following change to include a test condition for while list control to make sure the script dies when you kill it. Replace this line: while :; do with while [[ -f $runfile ]]; do In bash, the 'while :; do' just tells the while loop to run forever and since there isn't a condition in the while look that breaks the loop, it will continue to run the test even after the dslcheck script is killed. -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 02/11/2010 03:16 AM, Tejas Guruswamy wrote:
On 11/02/10 04:03, Wong wrote:
Thanks for your advise & useful script.
I tried to run it foreground and get this message:
/usr/local/sbin/dslcheck: line 9: trap: cleanup: invalid signal specification That's because it should be INT TERM EXIT , not SIGINT SIGTERM EXIT. Also only one "trap cleanup" -- it seems to be duplicated for some reason.
Basically it should look like this trap cleanup INT TERM EXIT
Regards, Tejas
Tejas, You and Dave are correct. I must have been tired :-) Dave, picky, picky :p. 'echo "$$" > $runfile' is better, because you do actually store something useful in $runfile, but since I was just testing for existence, touch would work just fine ;-). I agree it should be 'rm -f' we don't want any confirmation questions. Good to with you Dave. One of these days, if just once, I'll actually 'snatch the pebbles from your hand' :p -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Dear All,
On 02/11/2010 03:16 AM, Tejas Guruswamy wrote:
On 11/02/10 04:03, Wong wrote:
Thanks for your advise & useful script.
I tried to run it foreground and get this message:
/usr/local/sbin/dslcheck: line 9: trap: cleanup: invalid signal specification That's because it should be INT TERM EXIT , not SIGINT SIGTERM EXIT. Also only one "trap cleanup" -- it seems to be duplicated for some reason.
Basically it should look like this trap cleanup INT TERM EXIT
Regards, Tejas
Tejas,
You and Dave are correct. I must have been tired :-)
Dave, picky, picky :p. 'echo "$$" > $runfile' is better, because you do actually store something useful in $runfile, but since I was just testing for existence, touch would work just fine ;-). I agree it should be 'rm -f' we don't want any confirmation questions.
Good to with you Dave. One of these days, if just once, I'll actually 'snatch the pebbles from your hand' :p
Thanks to all. This discussion help me to find solution for DSL link disconnection cause by many problems. In my area, most of DSL problem caused by provider and my SuSE cannot wake up automatically the dsl0 interface when service restore. I tried the script, manual load and test to restart the DSL modem. However, the script seems cannot run as expected. Below the script I run: ---------- #!/bin/bash runfile=/tmp/chkdsl0.run cleanup() { rm -rf $runfile } trap cleanup INT TERM EXIT if [[ ! -f $runfile ]]; then touch $runfile while :; do if ! ping -q -c1 w.x.y.z &>/dev/null; then rcnetwork restart # (as root, or 'sudo rcnetwork restart' with sudo) fi sleep 600 done fi exit 0 ---------- *** w.x.y.z is my router at provider site Please advise what wrong is it? Thanks a lot ! Wong -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Saturday, 2010-02-13 at 12:50 +0800, Wong wrote:
I tried the script, manual load and test to restart the DSL modem. However, the script seems cannot run as expected.
Please explain why. What do you see? Error messages, symptoms? - -- Cheers, Carlos E. R. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAkt2di4ACgkQtTMYHG2NR9V7nQCfVhbrhnlH3jGttA4C+EBNL9in FwoAniAIw6DOfVdbhf67ZqU+MX9ZOHvO =lKjy -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
I tried the script, manual load and test to restart the DSL modem. However, the script seems cannot run as expected.
Please explain why. What do you see? Error messages, symptoms?
Hi Carlos, It works now. I run it as foreground process. But I am still stuck how to make it run as background and started every booting. I tried to put the script file into ~/init.d/after.local either at the top or the bottom of after.local. All daemons became unloaded to system including iptables script. Need your advise. Thanks Wong -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Saturday, 2010-02-13 at 19:19 +0800, Wong wrote:
I tried the script, manual load and test to restart the DSL modem. However, the script seems cannot run as expected.
Please explain why. What do you see? Error messages, symptoms?
Hi Carlos,
It works now. I run it as foreground process. But I am still stuck how to make it run as background and started every booting.
I tried to put the script file into ~/init.d/after.local either at the top or the bottom of after.local. All daemons became unloaded to system including iptables script.
That's impossible, one script can not unload the rest. You must be doing something else. - -- Cheers, Carlos E. R. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAkt2j/cACgkQtTMYHG2NR9UiBgCeKPHWdVS76P83uZ+q5LeKhDVK zEIAniWvtDhzeH/m5jblRnpjN6cjNdW5 =Zt/D -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday, 2010-02-13 at 19:19 +0800, Wong wrote:
I tried the script, manual load and test to restart the DSL modem. However, the script seems cannot run as expected.
Please explain why. What do you see? Error messages, symptoms?
Hi Carlos,
It works now. I run it as foreground process. But I am still stuck how to make it run as background and started every booting.
I tried to put the script file into ~/init.d/after.local either at the top or the bottom of after.local. All daemons became unloaded to system including iptables script.
That's impossible, one script can not unload the rest. You must be doing something else.
Hi Carlos, Perhaps my knowledge limitation cannot explain it why, but it is truly happen. It is a mystery for me. To avoid misunderstanding, the others were not loaded then unloaded. But it was not started by after.local if I put the script or loaded from external file. I wish I could have the script and put in to a file and loaded by after.local. It will be more organized. Thanks a lot. Wong -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sunday, 2010-02-14 at 12:59 +0800, Wong wrote:
I tried to put the script file into ~/init.d/after.local either at the top or the bottom of after.local. All daemons became unloaded to system including iptables script.
That's impossible, one script can not unload the rest. You must be doing something else.
Hi Carlos,
Perhaps my knowledge limitation cannot explain it why, but it is truly happen. It is a mystery for me.
It is rather a language barrier, I'm afraid.
To avoid misunderstanding, the others were not loaded then unloaded. But it was not started by after.local if I put the script or loaded from external file.
I wish I could have the script and put in to a file and loaded by after.local. It will be more organized.
Now that I think, that code can not be written into after.local, or the system will "think" that the init scripts failed. All those scripts must exit sucessfully, you can not leave one working in a loop. Yo have to call another script in background from after.local and exit. I would create a new service to load just after "/etc/init.d/network", kind of "networkwatchdog" which would in turn start/kill another script running the loop. - -- Cheers. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAkt3+KMACgkQtTMYHG2NR9XmAACfWVZhBID91K6C2+E9PPjkO5iZ mkQAn2sqLxGV2VwBnZ9eIsO8Ot/E10Dq =dTGh -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
I tried to put the script file into ~/init.d/after.local either at the top or the bottom of after.local. All daemons became unloaded to system including iptables script.
That's impossible, one script can not unload the rest. You must be doing something else.
Hi Carlos,
Perhaps my knowledge limitation cannot explain it why, but it is truly happen. It is a mystery for me.
It is rather a language barrier, I'm afraid.
To avoid misunderstanding, the others were not loaded then unloaded. But it was not started by after.local if I put the script or loaded from external file.
I wish I could have the script and put in to a file and loaded by after.local. It will be more organized.
Now that I think, that code can not be written into after.local, or the system will "think" that the init scripts failed. All those scripts must exit sucessfully, you can not leave one working in a loop. Yo have to call another script in background from after.local and exit.
I would create a new service to load just after "/etc/init.d/network", kind of "networkwatchdog" which would in turn start/kill another script running the loop.
Thousand thanks Carlos, David, and others. It will be a great stuff that a must have for users that using ADSL link. Best Regards, Wong -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 02/14/2010 08:30 PM, Wong wrote:
I tried to put the script file into ~/init.d/after.local either at > the top or the bottom of after.local. All daemons became unloaded to > system including iptables script.
That's impossible, one script can not unload the rest. You must be doing something else.
Hi Carlos,
Perhaps my knowledge limitation cannot explain it why, but it is truly happen. It is a mystery for me.
It is rather a language barrier, I'm afraid.
To avoid misunderstanding, the others were not loaded then unloaded. But it was not started by after.local if I put the script or loaded from external file.
I wish I could have the script and put in to a file and loaded by after.local. It will be more organized.
Now that I think, that code can not be written into after.local, or the system will "think" that the init scripts failed. All those scripts must exit sucessfully, you can not leave one working in a loop. Yo have to call another script in background from after.local and exit.
I would create a new service to load just after "/etc/init.d/network", kind of "networkwatchdog" which would in turn start/kill another script running the loop.
Thousand thanks Carlos, David, and others.
It will be a great stuff that a must have for users that using ADSL link.
Best Regards,
Wong
Wong, I still needed to see your 'ifconfig' output, but here is the script cleaned up a bit. You can use it as discussed, but you should check whether the link is up by some other manner than pinging a public server (it works though ;-) You will notice that in this version of the script, I moved the test of dsl0 to a separate function 'dsl0test()' so all you need to do is edit the function when you decide on the test you want to use. You can also specify the wait period from the command line (default is 60 seconds). So if you wanted to change the interval to 20 seconds, all you need to do is start the script with: nameofscript 20 David and Carlos can fill in my screwups, but it should work well for you. Have fun. #!/bin/bash # check for root and use sudo if not root [[ $UID -eq 0 ]] && prefix="" || prefix="sudo " runfile=/var/run/chkdsl0.run tmpfile=/tmp/chkdsl0 ckinterval=${1:-60} cleanup() { ${prefix}rm -f $runfile } trap cleanup SIGINT SIGTERM EXIT ## Change this test based on your 'ifconfig' output (this is temporary) dsl0test() { ping -q -c1 yahoo.com &>/dev/null return $? } ## if the runfile is still present check the PID to see if it is running [[ -f $runfile ]] && { ps ax | grep -q $(<$runfile) &>/dev/null PIDrunning=$? } ## if not already running, start checking dsl0 connection if [[ ! -f $runfile ]] || [[ $PIDrunning != 0 ]]; then echo "$$" > $tmpfile && ${prefix}cp $tmpfile $runfile && rm $tmpfile ## keep runing until killed or $runfile deleted while [[ -f $runfile ]] && [[ $$ == $( <$runfile ) ]]; do ## if still OK, sleep for $ckinterval seconds, if dls0 down restart network if dsl0test; then sleep $ckinterval else ${prefix}rcnetwork restart fi done fi exit 0 -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 02/14/2010 08:30 PM, Wong wrote:
I tried to put the script file into ~/init.d/after.local either at > the top or the bottom of after.local. All daemons became unloaded to > system including iptables script.
That's impossible, one script can not unload the rest. You must be doing something else.
Hi Carlos,
Perhaps my knowledge limitation cannot explain it why, but it is truly happen. It is a mystery for me.
It is rather a language barrier, I'm afraid.
To avoid misunderstanding, the others were not loaded then unloaded. But it was not started by after.local if I put the script or loaded from external file.
I wish I could have the script and put in to a file and loaded by after.local. It will be more organized.
Now that I think, that code can not be written into after.local, or the system will "think" that the init scripts failed. All those scripts must exit sucessfully, you can not leave one working in a loop. Yo have to call another script in background from after.local and exit.
I would create a new service to load just after "/etc/init.d/network", kind of "networkwatchdog" which would in turn start/kill another script running the loop.
Thousand thanks Carlos, David, and others.
It will be a great stuff that a must have for users that using ADSL link.
Best Regards,
Wong
Wong,
I still needed to see your 'ifconfig' output, but here is the script cleaned up a bit. You can use it as discussed, but you should check whether the link is up by some other manner than pinging a public server (it works though ;-)
You will notice that in this version of the script, I moved the test of dsl0 to a separate function 'dsl0test()' so all you need to do is edit the function when you decide on the test you want to use. You can also specify the wait period from the command line (default is 60 seconds). So if you wanted to change the interval to 20 seconds, all you need to do is start the script with:
nameofscript 20
David and Carlos can fill in my screwups, but it should work well for you. Have fun.
#!/bin/bash
# check for root and use sudo if not root [[ $UID -eq 0 ]] && prefix="" || prefix="sudo "
runfile=/var/run/chkdsl0.run tmpfile=/tmp/chkdsl0 ckinterval=${1:-60}
cleanup() { ${prefix}rm -f $runfile }
trap cleanup SIGINT SIGTERM EXIT
## Change this test based on your 'ifconfig' output (this is temporary) dsl0test() { ping -q -c1 yahoo.com &>/dev/null return $? }
## if the runfile is still present check the PID to see if it is running [[ -f $runfile ]] && { ps ax | grep -q $(<$runfile) &>/dev/null PIDrunning=$? }
## if not already running, start checking dsl0 connection if [[ ! -f $runfile ]] || [[ $PIDrunning != 0 ]]; then echo "$$" > $tmpfile && ${prefix}cp $tmpfile $runfile && rm $tmpfile ## keep runing until killed or $runfile deleted while [[ -f $runfile ]] && [[ $$ == $( <$runfile ) ]]; do ## if still OK, sleep for $ckinterval seconds, if dls0 down restart network if dsl0test; then sleep $ckinterval else ${prefix}rcnetwork restart fi done fi
exit 0
Dave, here it is; dsl0 Link encap:Point-to-Point Protocol inet addr:w.x.y.z P-t-P:a.b.c.d Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1 RX packets:132535 errors:0 dropped:0 overruns:0 frame:0 TX packets:122361 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 RX bytes:126349004 (120.4 Mb) TX bytes:18393814 (17.5 Mb) eth0 Link encap:Ethernet HWaddr 00:60:93:23:E2:49 inet addr:192.168.0.2 Bcast:192.168.0.3 Mask:255.255.255.252 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:134283 errors:0 dropped:0 overruns:0 frame:0 TX packets:124110 errors:0 dropped:0 overruns:0 carrier:0 collisions:232 txqueuelen:1000 RX bytes:129382957 (123.3 Mb) TX bytes:21190789 (20.2 Mb) Interrupt:20 Base address:0xd000 eth1 Link encap:Ethernet HWaddr 00:1D:C0:A3:2A:95 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:97434 errors:0 dropped:0 overruns:0 frame:0 TX packets:129258 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:20350028 (19.4 Mb) TX bytes:130289597 (124.2 Mb) Memory:e0700000-e0720000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:2115 errors:0 dropped:0 overruns:0 frame:0 TX packets:2115 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:223533 (218.2 Kb) TX bytes:223533 (218.2 Kb) I know that I should ping my provider router (a.b.c.d) instead yahoo.com and put the addr into script. Is the script to be run from external file or put into after.local? Please advise me which is better. Thanks a lot! Wong -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Tuesday, 2010-02-16 at 18:53 +0800, Wong wrote: ...
Is the script to be run from external file or put into after.local? Please advise me which is better.
Try calling: /path/script & inside after.local. Do not insert the code, or after.local will never finish and booting sequence will fail. For this, the script must not print any output after initialization is done. - -- Cheers, Carlos E. R. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAkt6iPAACgkQtTMYHG2NR9XDvgCghwpTAsT8Y5p3lHx6z1RCy84o JoQAnR4XS+Lx1e2KCS3j5MmxUKdalK2R =uVX2 -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 02/16/2010 04:53 AM, Wong wrote: <snip>
I know that I should ping my provider router (a.b.c.d) instead yahoo.com and put the addr into script.
If ping works with your provider, then use them, if not -- yahoo is reliable...
Is the script to be run from external file or put into after.local? Please advise me which is better.
Dunno, Carlos will have to help here :p
Thanks a lot!
Wong
-- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 02/16/2010 04:53 AM, Wong wrote:
On 02/14/2010 08:30 PM, Wong wrote: <snip> I know that I should ping my provider router (a.b.c.d) instead yahoo.com and put the addr into script.
Is the script to be run from external file or put into after.local? Please advise me which is better.
Thanks a lot!
Wong
Wong, The following should do what you want based on ifconfig instead of ping. Enjoy: #!/bin/bash # check for root and use sudo if not root [[ $UID -eq 0 ]] && prefix="" || prefix="/usr/bin/sudo " runfile=/var/run/chkdsl0.run tmpfile=/tmp/chkdsl0 ckinterval=${1:-60} iface=dsl0 ## generic cleanup function used by trap cleanup() { sudo rm -f $runfile } trap cleanup SIGINT SIGTERM EXIT ## test uses ifconfig to test for the presence of 'inet addr:' for dsl0 (or $iface) dsl0test() { declare -a ifacestatus IFS=$'\n' ifacestatus=( $(ifconfig) ) for ((i=0;i<${#ifacestatus[@]};i++)); do testline=${ifacestatus[$i]} [[ ${testline:0:4} =~ $iface ]] && break done linkidx=$((i+=1)) if [[ ${ifacestatus[$linkidx]} =~ 'inet addr:' ]]; then return 0 else return 1 fi } ## if the runfile is still present check the PID to see if it is running [[ -f $runfile ]] && { ps ax | grep -q $(<$runfile) &>/dev/null PIDrunning=$? } ## if not already running, start checking dsl0 connection if [[ ! -f $runfile ]] || [[ $PIDrunning != 0 ]]; then echo "$$" > $tmpfile && ${prefix}cp $tmpfile $runfile && rm $tmpfile ## keep runing until killed or $runfile deleted while [[ -f $runfile ]] && [[ $$ == $( <$runfile ) ]]; do ## if still OK, sleep for $ckinterval seconds, if dls0 down restart network if dsl0test; then sleep $ckinterval else ${prefix}rcnetwork restart fi done fi exit 0 -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Tuesday, 2010-02-16 at 17:09 -0600, David C. Rankin wrote:
The following should do what you want based on ifconfig instead of ping. Enjoy:
There is an advantage with using ping to the outside: it keeps the connection up. Some providers use a timeout and if they see no activity, they kill the connection. - -- Cheers, Carlos E. R. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAkt7MxMACgkQtTMYHG2NR9WlqQCfbYqipdwKJ7yndiV4NlhjQhfO QOsAnAr8q1LUBvwHa5ZA4FRyWUGutAVT =fTt5 -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday, 2010-02-16 at 17:09 -0600, David C. Rankin wrote:
The following should do what you want based on ifconfig instead of ping. Enjoy:
There is an advantage with using ping to the outside: it keeps the connection up. Some providers use a timeout and if they see no activity, they kill the connection.
AFAIK, some devices (ADSL Modem) have "nailed-up" connection feature to avoid link disconnection by providers. Does SuSE also have it? Please advise how-to set the nailed-up the link besides pinging the peer. Thanks. Wong -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Tue, 16 Feb 2010, David C. Rankin wrote:
ifacestatus=( $(ifconfig) )
export LANG=de_DE ifacestatus=( $(ifconfig) ) => Kaboom! for the following. Yes, it's understandable for you not to think of that ;) Weirdly enough, I can't get my ifconfig to output the german version, but I've seen it often enough. So: use ifacestatus=( $(LC_ALL=C ifconfig) ) to reliably get the english version of the output. Ah, here's a sample how the german version of ifconfig-output looks like [from http://www.fibel.org/linux/lfo-0.6.0/node476.html ] eth0 Protokoll:Ethernet Hardware Adresse 00:E0:18:1B:21:55 inet Adresse:10.0.1.3 Bcast:10.0.1.255 Maske:255.255.255.0 inet6 Adresse: fe80::2e0:18ff:fe1b:2155/10 G-Aültigkeitsbereich:Verbindung-b UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 Kollisionen:0 Sendewarteschlangenl-Aänge:100-b RX bytes:0 (0.0 b) TX bytes:288 (288.0 b) Interrupt:9 Basisadresse:0x7c00 Ain't that nice? ;) Now add in what Wong might use (Chinese? Encoded as what?) Much fun ensues. NOT! For the rest of the script, much of my other mail seems to apply. -dnh -- If I wanted to point and drool, I'd go to a Chippendales show. -- Leigh Metcalf -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
----- Original Message -----
From: "David Haller"
ifacestatus=( $(ifconfig) )
export LANG=de_DE ifacestatus=( $(ifconfig) ) => Kaboom! for the following. Yes, it's understandable for you not to think of that ;) Weirdly enough, I can't get my ifconfig to output the german version, but I've seen it often enough. So: use ifacestatus=( $(LC_ALL=C ifconfig) ) to reliably get the english version of the output. Ah, here's a sample how the german version of ifconfig-output looks like [from http://www.fibel.org/linux/lfo-0.6.0/node476.html ] eth0 Protokoll:Ethernet Hardware Adresse 00:E0:18:1B:21:55 inet Adresse:10.0.1.3 Bcast:10.0.1.255 Maske:255.255.255.0 inet6 Adresse: fe80::2e0:18ff:fe1b:2155/10 G-Aültigkeitsbereich:Verbindung-b UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 Kollisionen:0 Sendewarteschlangenl-Aänge:100-b RX bytes:0 (0.0 b) TX bytes:288 (288.0 b) Interrupt:9 Basisadresse:0x7c00 Ain't that nice? ;) Now add in what Wong might use (Chinese? Encoded as what?) Much fun ensues. NOT! For the rest of the script, much of my other mail seems to apply. -dnh ------------------------------ Hi, Just tried the script with result: 1. The version interface check, cause my other Ethernet down after reseting modem, but dsl0 and eth0 run. Not tested with after.local 2. The version link check (ping), cause network restart (continuosly) after reseting modem. Not tested with after.local I don't know it effected by language of OS that I am using or by other thing. My SuSE is in EN languange. However, corncerning for language is a good idea. Thanks, Wong -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Tue, 16 Feb 2010, David C. Rankin wrote: [...] We're getting there ;)
#!/bin/bash # check for root and use sudo if not root [[ $UID -eq 0 ]] && prefix="" || prefix="sudo "
How about 'SUDO' instead of prefix? But anyway: using sudo that way is a bad idea. Any /etc/sudoers containing '.* ALL=(root) ALL' or equivalent, even with 'targetpw' set, is doomed to be r00ted or 0wnzrd (or whatever you may call it ;) And checking $UID is unsafe too, I'd guess (the environment is not to be trusted). Use the following instead: test $(uid -u) -eq 0 || sudo /our/script/with/fullpath and then add only /our/script/with/fullpath to sudoers, for those users that shall be able to start the script manually (i.e. from outside an initscript).
runfile=/var/run/chkdsl0.run
Ok, though I see more *.pid than *.run under /var/run ;) But extensions, shmektensions, we don't really care anyway. It's just a hint / convention as to what these files are supposed to contain. "Is dat fing wif der Pid in, no?"[1], so I prefer pidfile and foo.pid ;)
tmpfile=/tmp/chkdsl0
Unneccessary (see below). And dangerous. Generally: Never ever use a tempfile with a static name.
ckinterval=${1:-60}
You can't wait 60s for the connection? Ugh. I'd propose 300 or more ;)
cleanup() { ${prefix}rm -f $runfile }
Should clean out "$tmpfile" too. And quote the "$runfile". Iff you use a tempfile.
trap cleanup SIGINT SIGTERM EXIT
trap cleanup HUP INT TERM EXIT
## Change this test based on your 'ifconfig' output (this is temporary) dsl0test() { ping -q -c1 yahoo.com &>/dev/null return $? }
return is superfluous. A function returns $? of the last command. Experiment with: $ bash -c 'foo() { echo -n "foo: "; false; return $?; }; bar() { echo -n "bar: "; true; false; }; foo; echo $?; bar; echo $?;' foo: 1 bar: 1 (and add/change the last command around ;) Explicitly returning $? doesn't harm though. So: dsl0test() { ping -q -c 1 -s 1 yahoo.com >dev/null 2>&1; } (other domains/hostnames with big pools of machines behind it are alternatives. E.g. www.google.com, www.microsoft.com etc. There are a lot more IPs involved than you can get at with a single nslookup/'dig a' ;)
## if the runfile is still present check the PID to see if it is running [[ -f $runfile ]] && { ps ax | grep -q $(<$runfile) &>/dev/null PIDrunning=$? }
This is not robust. The PID may or may not appear in the output of 'ps' in other places, e.g. as part of a higher PID. And instead of parsing 'ps ax' correctly and robustly: ps ax | awk -vPID="$(<$runfile)" '$1 == PID { exit(0); } END{ exit 1;}' (and you'd then have to check if it's the script running with that pid. You could check that too with awk: ps -eo pid,cmd | awk -vPID="$(<$runfile)" \ '$1 == PID && $2 ~ /^[^[:space:]]scriptname[[:space:]]+/ { exit(0); } END{ exit 1;}' or something like that. Awk's quite nice for such stuff ;), I propose to use another easy but safe way: check_running() { if test -r "$runfile"; then ### whee, we got a pidfile PID="$(<$runfile)" ### what pid are we to check if test -e "/proc/${PID}/cmdline"; then ### ok, something's running ### with that pid if grep -q '[o]ur_scriptname' "/proc/${PID}/cmdline"; then ### yep, this should be already running with PID="$PID" return 0; fi fi fi return 1; }
## if not already running, start checking dsl0 connection if [[ ! -f $runfile ]] || [[ $PIDrunning != 0 ]]; then
if ! check_running; then
echo "$$" > $tmpfile && ${prefix}cp $tmpfile $runfile && rm $tmpfile
Forget about the tempfile. Just write to the pidfile. We're root at this point anyway (see above). echo "$$" > "$pidfile" BTW: using sudo without specifying _explicitly allowed commands in /etc/sudoers, then you could just as well run the whole script as root, login as root, run X as root, shoot yourself in the foot. See above for a possibility.
## keep runing until killed or $runfile deleted
That's a bit unusual ...
while [[ -f $runfile ]] && [[ $$ == $( <$runfile ) ]]; do
... and I don't really see a reason for this ... But I'm a bit tired.
## if still OK, sleep for $ckinterval seconds, if dls0 down restart network if dsl0test; then sleep $ckinterval else ${prefix}rcnetwork restart fi done fi
exit 0
HTH, -dnh, just providing bits and ideas, not a full script, as I may have messed up at this time of night. [1] tell me if I'm channeling Detritus too much -- "What, you don't think "insmod emacs" is a good idea?" -- Joe Moore -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday, 2010-02-12 at 21:09 -0600, David C. Rankin wrote:
You and Dave are correct. I must have been tired :-)
Dave, picky, picky :p. 'echo "$$" > $runfile' is better, because you do actually store something useful in $runfile, but since I was just testing for existence, touch would work just fine ;-). I agree it should be 'rm -f' we don't want any confirmation questions.
"echo" should be faster than calling external program "touch", thus the preference - if I understand correctly. - -- Cheers, Carlos E. R. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAkt2dMoACgkQtTMYHG2NR9VnFACcDqTljD3fhfBbG5ij5RzDG/wO ljkAoJc3afznqkkoZMD8svutW1UUYTUA =Lkkl -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Sat, 13 Feb 2010, Carlos E. R. wrote:
You and Dave are correct. I must have been tired :-)
Dave, picky, picky :p. 'echo "$$" > $runfile' is better, because you
;)
"echo" should be faster than calling external program "touch", thus the preference - if I understand correctly.
No. You could go and get the pid from the runfile and kill the script with that. I.e.: test -f RUNFILE && kill $(< RUNFILE ) && rm -f RUNFILE -dnh -- "Only perl can parse Perl." -- Larry Wall -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sunday, 2010-02-14 at 02:00 +0100, David Haller wrote:
Hello,
On Sat, 13 Feb 2010, Carlos E. R. wrote:
You and Dave are correct. I must have been tired :-)
Dave, picky, picky :p. 'echo "$$" > $runfile' is better, because you
;)
"echo" should be faster than calling external program "touch", thus the preference - if I understand correctly.
No. You could go and get the pid from the runfile and kill the script with that. I.e.: test -f RUNFILE && kill $(< RUNFILE ) && rm -f RUNFILE
Sorry, I don't understand a word. Could you please explain for non bash gurus? - -- Cheers, Carlos E. R. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAkt3Uh4ACgkQtTMYHG2NR9VJkwCeOaKlTIqE3u9KeZnMT//7s1Jq aoIAni4/kB7+E9PEEEUnXMo8iPA2GWEv =GaQL -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Sun, 14 Feb 2010, Carlos E. R. wrote:
On Sunday, 2010-02-14 at 02:00 +0100, David Haller wrote:
On Sat, 13 Feb 2010, Carlos E. R. wrote:
You and Dave are correct. I must have been tired :-)
Dave, picky, picky :p. 'echo "$$" > $runfile' is better, because you
;)
"echo" should be faster than calling external program "touch", thus the preference - if I understand correctly.
No. You could go and get the pid from the runfile and kill the script with that. I.e.: test -f RUNFILE && kill $(< RUNFILE ) && rm -f RUNFILE
Sorry, I don't understand a word. Could you please explain for non bash gurus?
Ok. When the script starts, you save the PID of the bash running the script in the pidfile (called RUNFILE above). Let's use a better name: /var/run/dslwatchdog.pid. You can use that file to get the PID of the (supposedly running) script and then kill that script. Or check if the script still runs, get a bunch of data from /proc/PID/ etc. pp. Above code, written differently: ==== PIDFILE="/var/run/dslwatchdog.pid" if test -f "$PIDFILE" ; then ### is the pidfile a file? watchdog_pid=$( cat "$PIDFILE" ) ### read the PID from the file if kill $watchdog_pid ; then ### kill the watchdog and if ### successful rm -f "$PIDFILE" ### remove the pidfile fi fi ===== The '$(< FILE)' is a bashism to read the content of a file into the commandline without using external commands. I.e. echo $(< FILE) is equivalent to echo $(cat FILE) just without the cat (which takes a lot of time to start in comparison). It may not seem like much, but starting external programs is expensive and _does_ add up. Look at (older) teTeX scripts like mktexnam and alike. Those actually are right to call external programs, as they're /bin/sh scripts and are/were supposed to run under a lot of shells and systems. Ah, and yes, most daemons have a pidfile in /var/run/ for a reason. Such as the ability to check if the daemon already runs (i.e. read the pidfile, look, if there's the daemon already/still running with that pid (look at /proc/${pid}/cmdline ;) or if it ended uncleanly without being able to remove the pidfile). Or whatever ;) Usually, on SUSE, services are started/checked/stopped via startproc/checkproc/killproc, which uses pidfiles "out of the box" and under a configurable name (-f option, so you can run a service more than once, with different pidfiles, say daemon_for_host_foo.pid and daemon_for_host_bar.pid). More questions? Call 0900-..., aaah, ask again! ;) -dnh -- "DOS=HIGH ...I knew it was on something!" (UNIX user, while reading C:\CONFIG.SYS) -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 02/11/2010 03:16 AM, Tejas Guruswamy wrote:
Basically it should look like this trap cleanup INT TERM EXIT
Tejas, That's not correct, see 'man 7 signal': First the signals described in the original POSIX.1-1990 standard. Signal Value Action Comment ---------------------------------------------------------------------- SIGHUP 1 Term Hangup detected on controlling terminal or death of controlling process SIGINT 2 Term Interrupt from keyboard SIGQUIT 3 Core Quit from keyboard SIGILL 4 Core Illegal Instruction SIGABRT 6 Core Abort signal from abort(3) SIGFPE 8 Core Floating point exception SIGKILL 9 Term Kill signal SIGSEGV 11 Core Invalid memory reference SIGPIPE 13 Term Broken pipe: write to pipe with no readers SIGALRM 14 Term Timer signal from alarm(2) SIGTERM 15 Term Termination signal SIGUSR1 30,10,16 Term User-defined signal 1 SIGUSR2 31,12,17 Term User-defined signal 2 SIGCHLD 20,17,18 Ign Child stopped or terminated SIGCONT 19,18,25 Cont Continue if stopped SIGSTOP 17,19,23 Stop Stop process SIGTSTP 18,20,24 Stop Stop typed at tty SIGTTIN 21,21,26 Stop tty input for background process SIGTTOU 22,22,27 Stop tty output for background process The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored. -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 02/11/2010 03:16 AM, Tejas Guruswamy wrote:
Basically it should look like this trap cleanup INT TERM EXIT
Tejas,
That's not correct, see 'man 7 signal' man trap: " The condition can be EXIT, 0 (equivalent to EXIT), or a signal specified using a symbolic name, ***without the SIG prefix***, as
On 13/02/10 05:23, David C. Rankin wrote:
listed in the tables of
signal names in the
participants (5)
-
Carlos E. R.
-
David C. Rankin
-
David Haller
-
Tejas Guruswamy
-
Wong