PDA

View Full Version : Bash help needed


turtle
2013-09-29, 20:42
I have the concept of what I want but am failing to get the execution down the way I need it. The script will be executed every 30 seconds via crontab.
* * * * * /foo/bar/your_script
* * * * * sleep 30; /foo/bar/your_script

Here's what I'm after:
Pseudo code
Set current home IP

Set old home IP from text file

Compare the current IP with the old home IP

If the IP is the same then exit and do nothing.

If the IP is different then do the following:

Add home IP to firewall allow list

Restart the firewall.

Add IP to cPanel Account MySQL Remote IP list.

Remove OLDHOMEIP from MySQL Remote IP list.

Replace text of old home IP with new IP

Actual code so far:
#!/bin/bash
# Get IP address of home and add to firewall
# Also add the IP to cPanel Remote MySQL for specified cPanels

# Get and set current home IP and previous logged one from file
HOMEIP=`host domain.tdl |awk {'print $4'}`
OLDHOMEIP=`cat /root/domain.tdl.txt`

# Username and Password
USER=xxxx
PASS=xxxx

# Check if the home IP has changed
if [ $OLDHOMEIP == $HOMEIP ]
then
end
else
echo "$HOMEIP Home IP add via script `date +%Y%m%d_%H:%M:%S`" >> /etc/firewall/firewall.allow
firewall --restart
curl -u $USER:$PASS -k "https://127.0.0.1:2083/xml-api/cpanel?cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=addhost&cpanel_xmlapi_apiversion=1&arg-0=$HOMEIP" -s >> /dev/null
curl -u $USER:$PASS -k "https://127.0.0.1:2083/xml-api/cpanel?cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=delhost&cpanel_xmlapi_apiversion=1&arg-0=$OLDHOMEIP" -s >> /dev/null
echo $HOMEIP > /root/domain.tdl.txt
fi

What I need is the part refined where it compares and exits if they match. If they do not match then it does the rest of them. I guess it could easily do all of them if they don't match and exit if they do, whatever works. I haven't been very good with this part of scripting yet. I know it's somewhat easy, but not sure how to make it happen right. Fighting through cPanel's API was a pain since the documentation isn't that great if you've never messed with it before. Lots of time reading and testing before getting those two curls right.

Any help getting this part right would be great.

turtle
2013-09-30, 10:04
I got the code changed with the help of a coworker. His input was certainly what I needed and along the lines of what I was after:
* * * * * /root/addiphash.sh > /dev/null 2>&1
* * * * * sleep 30;/root/addiphash.sh > /dev/null 2>&1

#!/bin/bash
# Get IP address of home and add to firewall
# Also add the IP to cPanel Remote MySQL for specified cPanels

# Get and set current home IP and previous logged one from file
homeIP=`host domain.tld|awk {'print $4'}`
oldHomeIP=`cat /root/domain.tld.txt`

# Check if the home IP has changed
if [ $oldHomeIP != $homeIP ]
then
echo "$oldHomeIP #Old Home IP removed from firewall `date +%Y%m%d_%H:%M:%S`" >> /var/log/ipchange.log
echo "$homeIP #Home IP added to firewall `date +%Y%m%d_%H:%M:%S`" >> /var/log/ipchange.log
sed -d '/ `echo $oldHomeIP|sed 's/\./\./g'`/d' /etc/firewall/firewall.allow
echo "$homeIP #Home IP added via script `date +%Y%m%d_%H:%M:%S`" >> /etc/firewall/firewall.allow
csf -r
curl --basic -H "Authorization: WHM root:`cat /root/.accesshash | tr -d "\n"`" -k "https://127.0.0.1:2087/xml-api/cpanel?user=username&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=addhost&cpanel_xmlapi_apiversion=1&arg-0=$homeIP" -s >> /dev/null
curl --basic -H "Authorization: WHM root:`cat /root/.accesshash | tr -d "\n"`" -k "https://127.0.0.1:2087/xml-api/cpanel?user=username&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=delhost&cpanel_xmlapi_apiversion=1&arg-0=$oldHomeIP" -s >> /dev/null
echo $homeIP > /root/domain.tld.txt
fi

# Unset variables
unset homeIP; unset oldHomeIP

I'm going to change the logging format to be more inline with other logs, but that will be later after work since I spent my first two hours here being highly unproductive for the company. :o