[wplug] BASH question

Wise, Jeremey WISEJ at PIOS.com
Thu Jul 10 08:55:18 EDT 2003


Thanks. This is what I needed. Just a few questions.

1) The line "...-f1 > \ /root/ipaddy.tmp". I believe the "\" is an escape to
ensure that the  "/root/.." will be treated as a path. Is that correct.
2) How did you know that "/usr/sbin/adsl-status" would return zero if link
up? Is this normal or is their a way to check this return code for any
application run?

Thanks,

Jeremey Wise (440)-519-6006
(CNE,MCSE,CSE)
Pioneer-Standard Electronics, Inc
wisej at pios.com

IBM ED PACK -Part # SB033 $4,500 ... SP Discount 11%
IBM ED CARD - Part # SB218 $8,995 ... SP Discount 8.5%

-----Original Message-----
From: Mike Griffin [mailto:mike at dmrnetworks.com] 
Sent: Wednesday, July 09, 2003 6:47 PM
To: wplug at wplug.org
Subject: RE: [wplug] BASH question

Here's a script that I made yesterday and does basically the same thing.
I have this script running every hour, and on boot. You'll want to
create the files ipaddy and ipaddy.tmp with the current IP before
running this script.

#!/bin/bash

/sbin/adsl-status 	# If our link is down, the return error will
				# NOT be zero

if [ $? -ne 0 ]
then
  /usr/sbin/adsl-start	# Bring up our link
  /usr/sbin/ifconfig eth0 | grep inet | cut -d\: -f2 | cut -d' ' -f1 > \
  /root/ipaddy.tmp
fi

# Let's compare IP adress of our ipaddy and ipaddy.tmp files
old="`cat /root/ipaddy`"
new="`cat /root/ipaddy.tmp`"

if [ "$old" != "$new" ]
then
   # This line is in addition to my script. Will mail whoever the new IP
  /usr/bin/mail -s "IP has changed" someonehere at somewhere.com < \
  /root/ipaddy.tmp
  cp -f /root/ipaddy.tmp /root/ipaddy
  # We'll want to rerun our firewall rules for the new IP.
  /etc/iptables
fi

exit 0


-----Original Message-----
From: wplug-admin at wplug.org [mailto:wplug-admin at wplug.org] On Behalf Of
Wise, Jeremey
Sent: Wednesday, July 09, 2003 4:56 PM
Cc: 'WPLUG'
Subject: RE: [wplug] BASH question

Ok so let me see if I understand.

************
# Script to bring up Landmark's Internet
# 07-09-2003
# v0.1
Internetstatus=($(adsl-status |grep up|cut -f 4 -d ' '))
InternetIP=($(ifconfig ppp0 |grep inet |cut -f 2 -d : |cut -f 1 -d ' '))
curDate=`date +"%y-%m-%d"`
curTime=`date +"%H%M"`

if $Internetstatus= up ;then
   printf The Internet link is up!
      if [ ! $InternetIP = ($(cat /root/CurrentIP.txt)) ;then
       echo $InternetIP >/root/CurrentIP.txt
       echo $InternetIP |mail -s "New IP Address" wisej at pios.com
      fi
   ;else
   echo The Internet link is down:$curDate$-$curTime >>/var/log/messages
fi
***********
1) You were saying that to make a variable equal to a string (such as an
IP
address) I need to run the whole command in backets ie. Subshell
ex:(ifconfig ppp0 |grep inet |cut -f 2 -d : |cut -f 1 -d ' ') This does
work
now... thx.
2) My syntax errors; lack of space, lack of closing bracket did not seem
to
fix the next step where I attempt a comparison of $InternetIP to ($(cat
/root/CurrentIP.txt)) and it fails. Is my logic right but syntax wrong?

Thanks,

Jeremey Wise (440)-519-6006
(CNE,MCSE,CSE)
Pioneer-Standard Electronics, Inc
wisej at pios.com

IBM ED PACK -Part # SB033 $4,500 ... SP Discount 11%
IBM ED CARD - Part # SB218 $8,995 ... SP Discount 8.5%

-----Original Message-----
From: Bob Schmertz [mailto:rschmertz at speakeasy.net] 
Sent: Wednesday, July 09, 2003 4:19 PM
To: Wise, Jeremey
Cc: WPLUG
Subject: Re: [wplug] BASH question

Wise, Jeremey incurred the wrath of Bob on Jul 9, by saying

>Another stupid question. I am trying to write a script that detects if
a
DSL
>line is down if it is start it. If the IP rotates email it to me so I
can
>know how to get to it if I need to.
>
>***************
>#!/bin/bash
># Script to bring up Landmark's Internet
># 07-09-2003
># v0.1
>Internetstatus="adsl-status |grep up|cut -f 4 -d ' '"
>InternetIP="ifconfig ppp0 |grep inet |cut -f 2 -d : |cut -f 1 -d ' '"

You probably want to put this whole thing in back quotes (or $(this) 
notation), rather than double quotes.  On the other hand, if you don't 
want this to be evaluated yet, why don't you just put this line later in

the script?

>curDate=`date +"%y-%m-%d"`
>curTime=`date +"%H%M"`
>
>if $Internetstatus=up ;then
>   printf The Internet link is up!
>      if [! $InternetIP=(cat /root/CurrentIP.txt)] ;then

Spaces.  There must be a space after the [.  Apparently there musst also

be spaces on either side of the = comparison operator (while there 
should be no spaces in assignment).  And, looks like you left out a $ 
sign in front of your open parenthesis ( "$(cat /root/CurrentIP.txt)" )

>       echo $InternetIP >/root/CurrentIP.txt
>       echo $InternetIP |mail -s "New IP Address" wisej at pios.com
>      fi
>   ;else
>   echo The Internet link is down:$curDate-$curTime >>/var/log/messages
>fi
>****************
>
>My issues are
>1) I can run the command "ifconfig ppp0 |grep inet |cut -f 2 -d : |cut
-f 1
>-d ' '" at a shell prompt and get an ok response but it does not run. I
>tried to put it in brackets (subshell) but that does not work either
>2) Logic does not hold; " if [! $InternetIP=(cat /root/CurrentIP.txt)]
>;then". How does one do this? How can I check a string output against
an
>existing variable string or integer?

-- 
Cheers,
Bob Schmertz


_______________________________________________
wplug mailing list
wplug at wplug.org
http://www.wplug.org/mailman/listinfo/wplug

_______________________________________________
wplug mailing list
wplug at wplug.org
http://www.wplug.org/mailman/listinfo/wplug



More information about the wplug mailing list