Scripting : Adding Static routes to remote Linux systems – Video

In our previous example we have discussed about a scenario , in which we have copied a script to each host and then we executed the script remotely using another calling script from the centralized host ( which is having publick key – password less authentication for each client ). But the problem in the previous scenario is we are making to connection to each host. And that is very expensive task in terms of network and time.
in real world, we will keep all the important scripts in a shared directory ( I mean NFS shared directory) and then we will call the client script using a remote calling script. In this way we can avoid the extra efforts require to copy the client script in each host every time when we modify the client script.
Coming to current scenario, I am showing you a demonstration of a task to remotely add a static route in each host mentioned in a hosts list file.
And actual route addition script will perform below checks before it actually add the route.
Check 1 : it will see if the route-eth0 already exists
IF YES – Go to Check 2
IF No – Go to check 3Check 2: Check if the new entry already added in the file.
IF YES – Script will go for next check 3
IF No – script will add a entry to the route-eth0. And go for Check 3Check 3: If the new route already enabled in the system
IF YES – Display Message and Exit
IF No – Go Forward for Route additionAction : Enable New Entry added to Static route file
Final Check : Is the new route added properly
IF YES – Display Success message
IF No – Display Error
;
You can look at the Demo video for the execution of the Script: Access Video
Below are the Actual Scripts and Files Used In this Scenario:
Hosts list:
[root@gurkul01 Scripts]# cat hostlist
gurkul02
gurkul04
Calling Script ( From Centralized Server) :
[root@gurkul01 Scripts]# cat Call-Script.sh
#!/bin/bashread -p “Enter the hostlist [Full Path] :” list
echo
read -p “Enter the Script you want to call :” prg
echoif [[ -z $list || -z $prg ]] ; then
echo
echo -e “Error : Please enter the Valid hostlist and Script path”
exit 1
fi
echo -e “Running Script : $prg on hostlist : $list”
for host in `cat $list`
do
echo -e “::::\t $host”
ssh -q -i /root/.ssh/ssh-key.pem $host $prg 2>; /dev/null
done[root@gurkul01 Scripts]#
;
Client Script that we are executing on each client host:
#!/bin/bash
host=`hostname`
pid=$$Rt=”199.9.200.0/24 via 10.4.0.1 dev eth0″
RtFile=”/etc/sysconfig/network-scripts/route-eth0″
>;/tmp/rt-entry.$pid
>;/tmp/fileentry.$pid
echo -e “Check :::\tChecking for Static route File\t….”
echo
if [ -f $RtFile ]; then
echo -e “\t\tStatic route file exists\t…. “
echo
echo -e “Check :::\tChecking if route entry already Exists \t … “if `grep ’199.9.200.0′ $RtFile 1>; /tmp/fileentry.$pid` ; then
echo -e “\t\tBelow entry already exist\t…. “
echo
echo “==============================================================”
cat /tmp/fileentry.$pid
echo “==============================================================”
echo
else
echo -e “\t\tFile had no entry for the new route .. Adding entry\t…”
echo -e $Rt >;>; /etc/sysconfig/network-scripts/route-eth0
echofi
echo -e “Check :::\tChecking if the route entry already enabled\t …”
if `/bin/netstat -rn|grep ’199.9.200.0′ 1>; /tmp/rt-entry.$pid` ; then
echo -e “\t\t$host ::: Static route already enabled …. exiting “
echo
echo “==============================================================”
cat /tmp/rt-entry.$pid
echo “==============================================================”
echo
exit 1
fiecho -e “\t\tRoute not enabled\t … “
echoelse
echo -e “\tStatic route file Doesn’t exist , Creating New one … “
echo -e $Rt >;>; /etc/sysconfig/network-scripts/route-eth0
echo
fiecho
echo -e ” Action :::\tEnabling new Routes on interface eth0″/etc/sysconfig/network-scripts/ifup-routes eth0 2>;/dev/null
echo ” …………….”
echo -e “Check :::\tchecking for the new Route added …..”
if [ ! `/bin/netstat -rn|grep '199\.9\.200\.0' >; /tmp/rt-entry.$pid` ]; then
echo -e “\t\t$host ::: New route added successfully”
cat /tmp/rt-entry.$pid
echo
else
echo -e “\t\t$host ::: Error: Route add failed ,check manually”
echo
fi
;
Test Case 1 : We are checking the execution of the script with an existing /etc/sysconfig/network-scripts/route-eth0 but the new static route is not exists in the file, on both the hosts.
The Output of the Calling Script:
;
[root@gurkul01 Scripts]# ./Call-Script.sh
Enter the hostlist [Full Path] : /Scripts/hostlist
Enter the Script you want to call : /Scripts/add-static-route.shRunning Script : /Scripts/add-static-route.sh on hostlist : /Scripts/hostlist
:::: gurkul02
Check ::: Checking for Static route File ….
Static route file exists ….Check ::: Checking if route entry already Exists …
File had no entry for the new route .. Adding entry …Check ::: Checking if the route entry already enabled …
Route not enabled …
Action ::: Enabling new Routes on interface eth0Check ::: checking for the new Route added …..
gurkul02 ::: New route added successfully
199.9.200.0 10.4.0.1 255.255.255.0 UG 0 0 0 eth0:::: gurkul04
Check ::: Checking for Static route File ….
Static route file exists ….Check ::: Checking if route entry already Exists …
File had no entry for the new route .. Adding entry …Check ::: Checking if the route entry already enabled …
Route not enabled …Action ::: Enabling new Routes on interface eth0
Check ::: Checking for the new Route added …..
gurkul04 ::: New route added successfully
199.9.200.0 10.4.0.1 255.255.255.0 UG 0 0 0 eth0
;
In this Below case, we are actually running the script on two host which are actually having the new route configured and enabled. And in this case, the expected result from the script is exit from the check 3.
;
[root@gurkul01 Scripts]# ./Call-Script.sh
Enter the hostlist [Full Path] : /Scripts/hostlist Enter the Script you want to call : /Scripts/add-static-route.sh
Running Script : /Scripts/add-static-route.sh on hostlist : /Scripts/hostlist
:::: gurkul02
Check ::: Checking for Static route File ….
Static route file exists ….Check ::: Checking if route entry already Exists …
Below entry already exist ….
==============================================================
199.9.200.0/24 via 10.4.0.1 dev eth0
==============================================================
Check ::: Checking if the route entry already enabled …
gurkul02 ::: Static route already enabled …. exiting
==============================================================
199.9.200.0 10.4.0.1 255.255.255.0 UG 0 0 0 eth0
==============================================================:::: gurkul04
Check ::: Checking for Static route File ….
Static route file exists ….Check ::: Checking if route entry already Exists …
Below entry already exist ….==============================================================
199.9.200.0/24 via 10.4.0.1 dev eth0
==============================================================Check ::: Checking if the route entry already enabled …
gurkul04 ::: Static route already enabled …. exiting==============================================================
199.9.200.0 10.4.0.1 255.255.255.0 UG 0 0 0 eth0
==============================================================
;
I will be adding more scripting examples ( along with demos) in further posts of this section. Just stay connected. You can subscribe the email updates on these posts by subscribing here
;








One Comment on “Scripting : Adding Static routes to remote Linux systems – Video”
Good answers in return of this query with solid arguments and describing everything regarding that.