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.
* * * * * /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.