[mdlug] efficient networking script?
Aaron Kulkis
akulkis00 at gmail.com
Wed Apr 6 13:18:06 EDT 2011
Tony Bemus wrote:
> Hello all,
> I just loaded an old laptop with a firewall distro called IP Fire
> (http://www.ipfire.org/), it is based on IP Cop. I have set it up with
> a green+red config. The red interface is a USB NIC(phone) and the green
> is the built in NIC. When I unplug the USB NIC it looses the red
> interface, as expected. then later I plug in the USB NIC and the red
> interface is there but doesn't configure the static ip. If I run
> "./etc/init.d/network restart" then the red interface get the static IP.
>
> I wrote a script that checks if the interface is there and if there is
> an IP address. If there is no interface it does nothing, if there is
> an interface and a IP address it does nothing. but if there is an
> interface and NO IP address then it restarts the network.
> This allows my wife and I to just plug in the USB NIC (phone) and wait a
> minute and then the Internet starts working, no commands to run.
>
> My question: Is this an efficient way to do this?
> I have fcron running the script every minuet.
>
>
> #!/bin/bash
>
> red=`ifconfig | grep red0 | wc -l`
> if [ "${red}" -gt "0" ]; then
> echo "Red Interface Exists";
> ip=`ifconfig | grep 192.168.1.2 | wc -l`
> if [ "${ip}" -gt "0" ]; then
> echo "IP address Exists";
> else
> echo "Restarting network";
> sh /etc/init.d/network restart
> fi
> else
> echo "No Red Interface";
> fi
>
You might want to adapt this script for your
own situation. I recommend keeping it in /local/bin
(where /local is on its own partition, so that it
is preserved between new installations).
#!/bin/bash
#
# quicknet
#
# quickly set up for networking on specified interface & IP
#
# Date Who What/notes
# ----------- ----------------------- ------------------------------------
# 26 Feb 2010 Aaron R. Kulkis Original version
# http://pastebin.com/yX6WSyMz
#
#
# Essence of program:
# prompt and read INTERFACE
# ifup $INTERFACE
# ifconfig
# prompt and read IP
# ifconfig $INTERFACE $IP
# ifconfig
# prompt and read ROUTER
# route add $ROUTER $INTERFACE
# route
# route add default gw $ROUTER $INTERFACE
# route
# ping ROUTER (confirm connection to router)
# ping 8.8.8.8 (Google open DNS host, confirms gateway operation))
# ping www.google.com (confirms DNS operation)
#
#
SELF=`basename $0`
#
#### GET INTERFACE ##################
#
printf "Use which interface ? "
read INTERFACE
printf "execute ifup $INTERFACE ?"
read RESPONSE
case RESPONSE in
y* | Y* )
set -x
ifup $INTERFACE
ifconfig $INTERFACE
set +x
;;
esac
#
#### GET ADDRESS ##################
#
printf "What IP address for $INTERFACE ? "
read IP
printf "\nAttempting to configure $INTERFACE\n"
set -x ; ifconfig $INTERFACE $IP ; set +x
printf "\nPress return key" ; read A
set -x ; ifconfig $INTERFACE ; set +x
#
#### DEFINE ROUTER ##################
#
printf "What is the name or IP address of the router/gateway? "
read ROUTER
printf "\nspecifying path to router $ROUTER\n"
set -x ; route add $ROUTER $INTERFACE ; set +x
printf "\nPress return key" ; read A
printf "Verifying route added to route table\n"
set -x ; route ; set +x
printf "\ndefining $ROUTER as the gateway.\n"
set -x ; route add default gw $ROUTER $INTERFACE ; set +x
printf "\nPress return key" ; read A
printf "Verify route added to route table\n"
set -x ; route ; set +x
#
#### VERIFY ROUTE COMPLETE ##################
#
printf "Verifing that route table complete:\n"
set -x ; route ; set +x
printf "test local network with 3 pings: "
set -x; ping -c 3 $ROUTER ; set +x
printf "test routing by pinging Google public DNS servers 3 times each\n"
set -x
ping -c 3 8.8.8.8
ping -c 3 8.8.4.4
set +x
printf "Testing DNS functionality:\n pinging www.google.com 3 times\n"
set -x; ping -c 3 www.google.com ; set +x
### END OF SCRIPT
Typical data file
(typical usage: cd /local/bin ; ./quicknet < norm1
----- Data file begins on next line
eth1
y
192.168.1.101
home
----- Data file ends on previous line.
Note that
akulkis at kulkilap:/local/bin> grep home /etc/hosts
192.168.1.254 2wirerouter 2wire home
In the data file, replace home with either the
IP address of your router or the symbolic name
for it which you have placed in /etc/hosts.
More information about the mdlug
mailing list