Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
Wow, thats Kewl! Thanks Robert, yes, that makes the code a little more portable! In a similar vein, to make it truly run, and for being so good(!), how do I automate getting the WAN interface name (em0 dc0 etc) on different machines!? Kind regards David Hingston - Original Message - From: "Robert Huff" <[EMAIL PROTECTED]> To: "Hinkie" <[EMAIL PROTECTED]> Sent: Saturday, September 01, 2007 2:45 PM Subject: Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up) Hello: > from edit.php create / write into new file /usr/bin/pinger.sh #!/bin/sh ping -c1 Insert_1st_Gateway_Hop_Here_commonly_Static_IP_a.b.c.1 if [ $? -eq 2 ]; then ifconfig em0 down ifconfig em0 up echo 'Gateway Down' else echo 'Gateway Up' fi And you might consider replacing: ping -c1 Insert_1st_Gateway_Hop_Here_commonly_Static_IP_a.b.c.1 with: ping -c1 `netstat -rn | head -n 5 | tail -n 1 | awk '{ print $2 }'` Robert Huff ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
Dear People As a courtesy to anyone interested I have finally sovled this (I hope), this is what I did, this is on a FreeBSD pfSense firewall router. Essentially the fix is to ping the static IP's first hop, if this is down then flick the WAN NIC state down and up, this restores the lost connection where the Motorola 5101 has stopped sending packets (presumably for some incompatibility reason) The Motorola 5101 has today been replaced with a 5100, the ISP tell me most commercial lines are running the 5100 as they say it is more router compatible than the newer 5101. I'll advise if the 5100 exhibits the same behaviour(!) however if it does the following should address it within a minute. If you are copying it be sure to copy it exactly as spaces in the wrong place stuff it upetc!! For both the lists and my record it is done by: => in /etc/crontab add */1 * * * * root /usr/bin/pinger.sh => from edit.php create / write into new file /usr/bin/pinger.sh #!/bin/sh ping -c1 Insert_1st_Gateway_Hop_Here_commonly_Static_IP_a.b.c.1 if [ $? -eq 2 ]; then ifconfig em0 down ifconfig em0 up echo 'Gateway Down' else echo 'Gateway Up' fi => from exec.php run chmod u+x /usr/bin/pinger.sh => from exec.php run ls -l /usr/bin/pinger.sh and check there is an x in the file permissions (for executable) It will have run when you see a log series of commands starting with Sep 1 11:32:13 kernel: em0: link state changed to UP Sep 1 11:32:11 kernel: em0: link state changed to DOWN The only problem I see with this approach is that whenever the Internet is down for whatever reason the WAN interface is going to be disconnected and reconnected every minute, as well as filling the logs with this info, but that seems only of concern from the perspective of filling the log with rubbish. I might tinker with it to send me an email to advise me when the code has also run . Thank you again to the people who worked with me on this. Kind regards David Hingston ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
David, you can also run it on a single line by piping it into sh: /bin/sh | command here However, feel free to put it in a file. Somethings as simple as this I like to make one-liners so they're easy to read in the crontab entry. Glad I could help! Eric On Aug 29, 2007, at 3:16 PMAug 29, 2007, Hinkie wrote: Eric You are of course correct, it runs under "sh" I think that means I have to run it in a separate script file I write it as (First_Hop subs with a.b.c.1) #!/bin/sh if ! ping -c1 First_Hop >/dev/null 2>/dev/null then echo First_Hop_OK else ifconfig em0 down && ifconfig em0 up echo Restored fi Save as /etc/pinger.sh Try run: pfsense-p266ii:~# /etc/pinger.sh /etc/pinger.sh: Permission denied. Try fix: pfsense-p266ii:~# chmod 777 /etc/pinger.sh chmod: /etc/pinger.sh: Read-only file system H I assume if it won't run here then crontab won't run it either? Kind regards David Hingston - Original Message - From: "Eric Crist" <[EMAIL PROTECTED]> To: "Hinkie" <[EMAIL PROTECTED]> Cc: Sent: Thursday, August 30, 2007 7:30 AM Subject: Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up) David, The script I provided assumes the sh shell. Most shell scripts have been historically written for the bourne shell. Before running the script I sent you, try typing sh at the command line. Then, run the line I sent your way. Let me know how it works for you! Eric On Aug 29, 2007, at 2:16 PMAug 29, 2007, Hinkie wrote: Hi Eric Thank you for that, I think I see one thing I was doing wrong (putting the ping into the if!) Running your code in the command window I get (Following the successful ping) TESTV=0: Command not found. TESTV: Undefined variable. As you can tell I am learning this syntax! Can you help me further? With thanks David Hingston - Original Message - From: "Eric Crist" <[EMAIL PROTECTED]> To: "Hinkie" <[EMAIL PROTECTED]> Cc: Sent: Thursday, August 30, 2007 2:17 AM Subject: Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up) On Aug 29, 2007, at 8:03 AMAug 29, 2007, Hinkie wrote: Hi I want to run a cron job in /etc/crontab that runs (ifconfig em0 down; ifconfig em0 up) if my cables static ip gateway can't be pinged but I can't figure it out. I can't get the syntax that runs in the command window, to then put intot the crontab Can anyone help me? Here's what I'd use: ping -c 1 a.b.c.d; TESTV=$?; if [ $TESTV != 0 ]; then `ifconfig em0 down`; fi HTH - Eric F Crist Secure Computing Networks ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions- [EMAIL PROTECTED]" - Eric F Crist Secure Computing Networks - Eric F Crist Secure Computing Networks ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
On 2007-08-29 14:18, Eric Crist <[EMAIL PROTECTED]> wrote: >On Aug 29, 2007, at 1:54 PMAug 29, 2007, Giorgos Keramidas wrote: >> On 2007-08-29 18:51, Wojciech Puchar <[EMAIL PROTECTED]> >> wrote: >>> fragment from my test program (used for other thing but doesn't matter) >>> >>> /sbin/ping -i 0.5 -s 1450 -c 3 tested_host >/dev/null 2>/dev/null >>> if [ $? != 0 ];then >>> perform_action_if_doesnt_ping >>> fi >> >> I'm not sure if '!=' is a 'portable' way to write sh(1) tests, >> but you have a point there :-) > > AFAIK, the != is evaluated by test, not sh. You're right. I did check with SUSv2 and SUSv3 after I posted the previous message. The != operator is defined by both standards[1,2], so I was wrong about its portability. [1] http://www.opengroup.org/onlinepubs/007908799/xcu/test.html [2] http://www.opengroup.org/onlinepubs/009695399/utilities/test.html Thanks for the correction, Giorgos ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
Eric You are of course correct, it runs under "sh" I think that means I have to run it in a separate script file I write it as (First_Hop subs with a.b.c.1) #!/bin/sh if ! ping -c1 First_Hop >/dev/null 2>/dev/null then echo First_Hop_OK else ifconfig em0 down && ifconfig em0 up echo Restored fi Save as /etc/pinger.sh Try run: pfsense-p266ii:~# /etc/pinger.sh /etc/pinger.sh: Permission denied. Try fix: pfsense-p266ii:~# chmod 777 /etc/pinger.sh chmod: /etc/pinger.sh: Read-only file system H I assume if it won't run here then crontab won't run it either? Kind regards David Hingston - Original Message - From: "Eric Crist" <[EMAIL PROTECTED]> To: "Hinkie" <[EMAIL PROTECTED]> Cc: Sent: Thursday, August 30, 2007 7:30 AM Subject: Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up) David, The script I provided assumes the sh shell. Most shell scripts have been historically written for the bourne shell. Before running the script I sent you, try typing sh at the command line. Then, run the line I sent your way. Let me know how it works for you! Eric On Aug 29, 2007, at 2:16 PMAug 29, 2007, Hinkie wrote: > Hi Eric > > Thank you for that, I think I see one thing I was doing wrong > (putting the ping into the if!) > > Running your code in the command window I get (Following the > successful ping) > > TESTV=0: Command not found. > TESTV: Undefined variable. > > As you can tell I am learning this syntax! > > Can you help me further? > > With thanks > > David Hingston > > - Original Message - > From: "Eric Crist" <[EMAIL PROTECTED]> > To: "Hinkie" <[EMAIL PROTECTED]> > Cc: > Sent: Thursday, August 30, 2007 2:17 AM > Subject: Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig > em0 up) > > > On Aug 29, 2007, at 8:03 AMAug 29, 2007, Hinkie wrote: > >> Hi >> >> I want to run a cron job in /etc/crontab that runs (ifconfig em0 >> down; ifconfig em0 up) if my cables static ip gateway can't be >> pinged but I can't figure it out. I can't get the syntax that runs >> in the command window, to then put intot the crontab >> >> Can anyone help me? > > Here's what I'd use: > > ping -c 1 a.b.c.d; TESTV=$?; if [ $TESTV != 0 ]; then `ifconfig em0 > down`; fi > > HTH > - > Eric F Crist > Secure Computing Networks > > ___ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions- > [EMAIL PROTECTED]" - Eric F Crist Secure Computing Networks ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
David, The script I provided assumes the sh shell. Most shell scripts have been historically written for the bourne shell. Before running the script I sent you, try typing sh at the command line. Then, run the line I sent your way. Let me know how it works for you! Eric On Aug 29, 2007, at 2:16 PMAug 29, 2007, Hinkie wrote: Hi Eric Thank you for that, I think I see one thing I was doing wrong (putting the ping into the if!) Running your code in the command window I get (Following the successful ping) TESTV=0: Command not found. TESTV: Undefined variable. As you can tell I am learning this syntax! Can you help me further? With thanks David Hingston - Original Message - From: "Eric Crist" <[EMAIL PROTECTED]> To: "Hinkie" <[EMAIL PROTECTED]> Cc: Sent: Thursday, August 30, 2007 2:17 AM Subject: Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up) On Aug 29, 2007, at 8:03 AMAug 29, 2007, Hinkie wrote: Hi I want to run a cron job in /etc/crontab that runs (ifconfig em0 down; ifconfig em0 up) if my cables static ip gateway can't be pinged but I can't figure it out. I can't get the syntax that runs in the command window, to then put intot the crontab Can anyone help me? Here's what I'd use: ping -c 1 a.b.c.d; TESTV=$?; if [ $TESTV != 0 ]; then `ifconfig em0 down`; fi HTH - Eric F Crist Secure Computing Networks ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions- [EMAIL PROTECTED]" - Eric F Crist Secure Computing Networks ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
On Aug 29, 2007, at 1:54 PMAug 29, 2007, Giorgos Keramidas wrote: On 2007-08-29 18:51, Wojciech Puchar <[EMAIL PROTECTED]> wrote: fragment from my test program (used for other thing but doesn't matter) /sbin/ping -i 0.5 -s 1450 -c 3 tested_host >/dev/null 2>/dev/null if [ $? != 0 ];then perform_action_if_doesnt_ping fi I'm not sure if '!=' is a 'portable' way to write sh(1) tests, but you have a point there :-) Rewriting this as: #!/bin/sh ping -i 0.5 -s 1450 -c 3 tested_host >/dev/null 2>&1 if test $? -eq 0 ; then exit 0 fi perform_action_if_doesnt_ping may save you an extra indentation level too. AFAIK, the != is evaluated by test, not sh. - Eric F Crist Secure Computing Networks ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
Hi Eric Thank you for that, I think I see one thing I was doing wrong (putting the ping into the if!) Running your code in the command window I get (Following the successful ping) TESTV=0: Command not found. TESTV: Undefined variable. As you can tell I am learning this syntax! Can you help me further? With thanks David Hingston - Original Message - From: "Eric Crist" <[EMAIL PROTECTED]> To: "Hinkie" <[EMAIL PROTECTED]> Cc: Sent: Thursday, August 30, 2007 2:17 AM Subject: Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up) On Aug 29, 2007, at 8:03 AMAug 29, 2007, Hinkie wrote: > Hi > > I want to run a cron job in /etc/crontab that runs (ifconfig em0 > down; ifconfig em0 up) if my cables static ip gateway can't be > pinged but I can't figure it out. I can't get the syntax that runs > in the command window, to then put intot the crontab > > Can anyone help me? Here's what I'd use: ping -c 1 a.b.c.d; TESTV=$?; if [ $TESTV != 0 ]; then `ifconfig em0 down`; fi HTH - Eric F Crist Secure Computing Networks ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
On 2007-08-29 18:51, Wojciech Puchar <[EMAIL PROTECTED]> wrote: > fragment from my test program (used for other thing but doesn't matter) > > /sbin/ping -i 0.5 -s 1450 -c 3 tested_host >/dev/null 2>/dev/null > if [ $? != 0 ];then > perform_action_if_doesnt_ping > fi I'm not sure if '!=' is a 'portable' way to write sh(1) tests, but you have a point there :-) Rewriting this as: #!/bin/sh ping -i 0.5 -s 1450 -c 3 tested_host >/dev/null 2>&1 if test $? -eq 0 ; then exit 0 fi perform_action_if_doesnt_ping may save you an extra indentation level too. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
fragment from my test program (used for other thing but doesn't matter) /sbin/ping -i 0.5 -s 1450 -c 3 tested_host >/dev/null 2>/dev/null if [ $? != 0 ];then perform_action_if_doesnt_ping fi ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
On 2007-08-30 01:03, Hinkie <[EMAIL PROTECTED]> wrote: > Hi > > I want to run a cron job in /etc/crontab that runs (ifconfig em0 down; > ifconfig em0 up) if my cables static ip gateway can't be pinged but I > can't figure it out. I can't get the syntax that runs in the command > window, to then put intot the crontab > > Can anyone help me? > > I've tried all sorts such as: > > if (ping -c1 a.b.c.1 != 1) then ifconfig em0 down; ifconfig em0 up endif > if ping -c1 a.b.c.1 != 1 then ping -c1 203.97.234.1 endif > if ping -c1 a.b.c.1 (ifconfig em0 down; ifconfig em0 up) > if ( ping -c1 a.b.c.1 != 1) ( ping -c1 203.97.234.182 ) > if $command("=ping -c1 a.b.c.1 =0") echo sucess > if ping -c 1 -w 1 a.b.c.1 >/dev/null echo sucess > if [[ (ping -q -c 3 a.b.c.1) == @(*100% packet loss*) ]]; echo sucess > if ! ping -c a.b.c.1 > /dev/null ; then echo sucess ; echo fail; fi I see you are using csh(1) shell syntax. The crontab entries are passed as commands to /bin/sh, so this sort of syntax won't really work. In general, I find the idea of long, complex crontab entries _very_ error-prone, and I prefer writing scripts which are _callable_ from cron. For example, I have several local clones of repositories which can be synchronized using Mercurial, but properly synchronizing them would require running: #!/bin/sh repo="mercurial/crew" repodir="${HOME}/hg/mercurial/crew" cd "${repodir}" if test $? -ne 0 ; then echo "${repodir}: directory not accessible." >&2 exit 1 fi hg pull Instead of cluttering my crontab with long-winded, complex stuff like this, I wrote a short shell script -- like the one above -- and saved it in my `~/cron.d' directory. Now my crontab contains: */2 * * * * ${HOME}/cron.d/hg-pull-crew.sh Try something similar too. It's easier to maintain potentially complex actions, if you `abstract away' all the complexity behind a script. Then you can also use whatever you prefer to write the scripts, like csh(1), Perl, or Python. Just make the script file executable and make sure it contains the proper '#!' line. - Giorgos ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
> Here's what I'd use: > ping -c 1 a.b.c.d; TESTV=$?; if [ $TESTV != 0 ]; then `ifconfig em0 down`; I'd suggest something more like: ping -c 1 $host [ $? -eq 0 ] && ifconfig em0 down && ifconfig em0 up or ping -c 1 $host if [ $? -eq 0 ]; then ifconfig em0 down && ifconfig em0 up fi -- Matthew Anthony Kolybabi (Mak) () ASCII Ribbon Campaign | Against HTML e-mail /\ www.asciiribbon.org | Against proprietary extensions ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
On Aug 29, 2007, at 8:03 AMAug 29, 2007, Hinkie wrote: Hi I want to run a cron job in /etc/crontab that runs (ifconfig em0 down; ifconfig em0 up) if my cables static ip gateway can't be pinged but I can't figure it out. I can't get the syntax that runs in the command window, to then put intot the crontab Can anyone help me? Here's what I'd use: ping -c 1 a.b.c.d; TESTV=$?; if [ $TESTV != 0 ]; then `ifconfig em0 down`; fi HTH - Eric F Crist Secure Computing Networks ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
Written by Hinkie on 08/29/07 08:03>> Hi I want to run a cron job in /etc/crontab that runs (ifconfig em0 down; ifconfig em0 up) if my cables static ip gateway can't be pinged but I can't figure it out. I can't get the syntax that runs in the command window, to then put intot the crontab Can anyone help me? I've tried all sorts such as: if (ping -c1 a.b.c.1 != 1) then ifconfig em0 down; ifconfig em0 up endif if ping -c1 a.b.c.1 != 1 then ping -c1 203.97.234.1 endif if ping -c1 a.b.c.1 (ifconfig em0 down; ifconfig em0 up) if ( ping -c1 a.b.c.1 != 1) ( ping -c1 203.97.234.182 ) if $command("=ping -c1 a.b.c.1 =0") echo sucess if ping -c 1 -w 1 a.b.c.1 >/dev/null echo sucess if [[ (ping -q -c 3 a.b.c.1) == @(*100% packet loss*) ]]; echo sucess if ! ping -c a.b.c.1 > /dev/null ; then echo sucess ; echo fail; fi Any help would be appreciated. Kind regards David Hingston I would recommend you make a shell script, place it in /root/bin or something else sensible, and have cron call that. I think it's generally frowned upon to have excessive scripting directly in the crontab. The following script should do what you want: #!/bin/sh if ! ping -c1 203.97.234.1821>/dev/null 2>/dev/null then ifconfig em0 down && ifconfig em0 up fi ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)
Hi I want to run a cron job in /etc/crontab that runs (ifconfig em0 down; ifconfig em0 up) if my cables static ip gateway can't be pinged but I can't figure it out. I can't get the syntax that runs in the command window, to then put intot the crontab Can anyone help me? I've tried all sorts such as: if (ping -c1 a.b.c.1 != 1) then ifconfig em0 down; ifconfig em0 up endif if ping -c1 a.b.c.1 != 1 then ping -c1 203.97.234.1 endif if ping -c1 a.b.c.1 (ifconfig em0 down; ifconfig em0 up) if ( ping -c1 a.b.c.1 != 1) ( ping -c1 203.97.234.182 ) if $command("=ping -c1 a.b.c.1 =0") echo sucess if ping -c 1 -w 1 a.b.c.1 >/dev/null echo sucess if [[ (ping -q -c 3 a.b.c.1) == @(*100% packet loss*) ]]; echo sucess if ! ping -c a.b.c.1 > /dev/null ; then echo sucess ; echo fail; fi Any help would be appreciated. Kind regards David Hingston ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"