You are not logged in.
I have made bash script which checks ip address, and if ip address had change since last time it sends email, restartrs mpd...
It all works perfect when if started from terminal if it is called with root account.
When I try to use script with cron in 90% of cases it does not recognize ip address, mail could not be sent...
This is the script:
/usr/sbin/checkIp
#!/bin/sh
echo loading>/home/lordofinsomnia/ipTest.txt
curIPFile=/home/lordofinsomnia/ip.txt
IPFunc=$(ip addr | grep inet | grep eth0)
SubStr=${IPFunc:9:15}
SubStrIndex2=$(expr index "$SubStr" /)
curIP=${IPFunc:9:SubStrIndex2-1}
oldIP=$(cat $curIPFile)
echo curIP:$curIP>>/home/lordofinsomnia/ipTest.txt
echo before if>>/home/lordofinsomnia/ipTest.txt
if [ curIP == oldIP ]; then
#echo ip is unchanged
mailSubject="ip address test"
mail -s "$mailSubject" "mail@mail.com" < /dev/null
else
#echo ip is changed
confReplace=ipaddress
sed "s/$confReplace/$curIP/g" /home/lordofinsomnia/mpd1.conf > /etc/mpd.conf
rc.d restart mpd
mailSubject="ip address change"
echo $curIP>/home/lordofinsomnia/ip.txt
mail -s "$mailSubject" "mail@mail.com" < /home/lordofinsomnia/ip.txt
echo sent mail>>/home/lordofinsomnia/ipTest.txt
fi
Cron setting is:
*/5 * * * * root /usr/sbin/checkIp
I have tried,
Cron setting is:
*/5 * * * * /usr/sbin/checkIp
but it results are the same...
my "log" file has output like this. IP is not detected...
loading
curIP:
before if
sent mail
What am I doing wrong?
Last edited by LordOfInsomnia (2012-04-09 21:28:08)
Offline
Debug it more. Log the output of "ip addr".
Maybe /usr/sbin/ (location of "ip") isn't specified in PATH in /etc/crontab.
Offline
<disclaimer>
Ignore if I don't get something correctly...
</disclaimer>
To get only the current local IP, I'd do:
ip addr | awk '/wlan0$/ {print $5}'
Of course, change the network interface so it refers to your setup.
:: Registered Linux User No. 223384
:: github
:: infinality-bundle+fonts: good looking fonts made easy
Offline
@brebs
I have tried it and loged it to a file it's loged in file, but rest of code is not working.
@bohoomil
I don't have wireless
ip addr | awk '/eth0$/ {print $2}'
this works for my lan, but still needs parsing...
Offline
It should work, but you may have to change it accordingly to isolate the IP. Can you paste the exact output of your 'ip addr'?
:: Registered Linux User No. 223384
:: github
:: infinality-bundle+fonts: good looking fonts made easy
Offline
My script works, but this is output.
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether xx:yy:zz:aa:bb:cc brd ff:ff:ff:ff:ff:ff
inet 188.x.y.z/22 brd 188.x.y.255 scope global eth0
inet6 xx::yy:zz:aa:bb36/64 scope link
valid_lft forever preferred_lft forever
this part od code works perfect, from terminal...
IPFunc=$(ip addr | grep inet | grep eth0)
SubStr=${IPFunc:9:15}
SubStrIndex2=$(expr index "$SubStr" /)
curIP=${IPFunc:9:SubStrIndex2-1}
Offline
Why did you lazily ask us, rather than just continue debugging?
$ IPFunc=$(echo $(ip addr | grep inet | grep eth0) | cut -d" " -f2)
$ echo $IPFunc
192.168.1.2/24
Your "9" should have made you suspicious. That ip command is using a weird delimiter, which "echo" converts into a manageable space.
Offline
I have added another cut to work
cut -d "/" -f
curIP=$(echo $(ip addr | grep inet | grep eth0) | cut -d" " -f2 | cut -d "/" -f1)
I did not know for cut command
Thank You!
Offline
Or, if you would prefer something simpler:
awk '/eth0$/ {print $2}' <(ip addr)
if you don't want the netmask
awk '/eth0$/ {sub(/\/24/,""); print $2}' <(ip addr)
Offline