apunkabollywood
asked on
need to grep default gateways for all interfaces using shell script
Hi All,
Help me to get default gateways of all interfaces on a server :
right now i have some command but only for single interface - it means it pick same gateway for all interfaces
Help me to get default gateways of all interfaces on a server :
right now i have some command but only for single interface - it means it pick same gateway for all interfaces
ASKER
Hi Rhukat ,
I dont have issue in finding default gateway but problem is coming when a server has 3 or 4 gateway configured on 3 or 4 nic's ..so pls advice
I dont have issue in finding default gateway but problem is coming when a server has 3 or 4 gateway configured on 3 or 4 nic's ..so pls advice
If you have multiple gateways configured the system will only use one (the default). So even if there are multiple gateways using the route -n command will show you the one that is actually being used. The others are useless.
ASKER
Actually i need to create a inventory on weekely basis and around 1000 servers are there ..thats why i m suing netstat ...so please advice how to collect each default gateway defined per nic?
Well are these NICs using dhcp? Which file holds the gateway info?
The reason why I ask is because typically the gateway statement goes in /etc/sysconfig/network and there is only one.
If you use DHCP then you will not be able to pull the information you are requesting because the system only selects one gateway and uses it.
Without setting up multiple gateways via static routes the system will have no record of the additional gateways.
If you use DHCP then you will not be able to pull the information you are requesting because the system only selects one gateway and uses it.
Without setting up multiple gateways via static routes the system will have no record of the additional gateways.
ASKER
No all are assigned with static ips ...
for single nic gateway info i am using
netstat -rn|sed -n '/^0.0.0.0/{ s/^[0. ]*//; s/ .*$//; p; }'
but issue is this it greps same gate way for all nics
My Script:
========================== ========== ========== ========== ===
ifconfig -a | grep "eth \?"| awk '{print $1}' > /tmp/ethlist.txt
DEFAULTGW=`netstat -rn|sed -n '/^0.0.0.0/{ s/^[0. ]*//; s/ .*$//; p; }'`
for i in `cat /tmp/ethlist.txt`
do
STATUS=`ifconfig $i | grep UP | cut -c 9-12`
echo $STATUS | grep -q [UP]
if [ $? -eq 0 ] ;then
STATUS=UP
IPADDRESS=`ifconfig $i | head -2 | tail -1 | awk '{print $2}' | cut -d: -f2`
SUBNETMASK=`ifconfig $i | head -2 | tail -1 | awk '{print $4}' | cut -d: -f2`
echo "$HOSTNAME,$i,$IPADDRESS,$ SUBNETMASK ,$DEFAULTG W,$STATUS" >> $OUTPUT_DIR/$OUTPUT_FILE
else
STATUS=DOWN
#echo "$HOSTNAME,$i,$IPADDRESS,$ SUBNETMASK ,$DEFAULTG W,$STATUS" >> $OUTPUT_DIR/$OUTPUT_FILE
fi
done
for single nic gateway info i am using
netstat -rn|sed -n '/^0.0.0.0/{ s/^[0. ]*//; s/ .*$//; p; }'
but issue is this it greps same gate way for all nics
My Script:
==========================
ifconfig -a | grep "eth \?"| awk '{print $1}' > /tmp/ethlist.txt
DEFAULTGW=`netstat -rn|sed -n '/^0.0.0.0/{ s/^[0. ]*//; s/ .*$//; p; }'`
for i in `cat /tmp/ethlist.txt`
do
STATUS=`ifconfig $i | grep UP | cut -c 9-12`
echo $STATUS | grep -q [UP]
if [ $? -eq 0 ] ;then
STATUS=UP
IPADDRESS=`ifconfig $i | head -2 | tail -1 | awk '{print $2}' | cut -d: -f2`
SUBNETMASK=`ifconfig $i | head -2 | tail -1 | awk '{print $4}' | cut -d: -f2`
echo "$HOSTNAME,$i,$IPADDRESS,$
else
STATUS=DOWN
#echo "$HOSTNAME,$i,$IPADDRESS,$
fi
done
Do you declare the gateway in the sysconfig network-scripts?
Can you post the output of:
cat /etc/sysconfig/network-scr ipts/ifcfg -eth0
cat /etc/sysconfig/network
Thanks
Can you post the output of:
cat /etc/sysconfig/network-scr
cat /etc/sysconfig/network
Thanks
ASKER
yes gateway informatiom is there in
cat /etc/sysconfig/network-scr ipts/ifcfg -eth0
cat /etc/sysconfig/network-scr ipts/ifcfg -eth1
but still let me clear i have 1000 servers and can be rhel 3 or 4 or 5 or 6
cat /etc/sysconfig/network-scr
cat /etc/sysconfig/network-scr
but still let me clear i have 1000 servers and can be rhel 3 or 4 or 5 or 6
well if it is in the config files you can do this:
grep -i gateway /etc/sysconfig/network-scr ipts/ifcfg -eth0
If you would have posted the output of the command instead of the command itself I could have given a more exact answer.
grep -i gateway /etc/sysconfig/network-scr
If you would have posted the output of the command instead of the command itself I could have given a more exact answer.
ASKER
Here is the current ouput of above script:
HOSTNAME,eth0,10.1.253.100 ,255.255.2 55.192,10. 1.253.126, UP
HOSTNAME,eth1,172.20.20.10 4,255.255. 255.0,10.1 .253.126,U P
look above for both nic its picking same gateway ..and given gateway is of eth0 but for eth1 also picking same....i need to have whatever gateway asign to both diff nic should display diff.
HOSTNAME,eth0,10.1.253.100
HOSTNAME,eth1,172.20.20.10
look above for both nic its picking same gateway ..and given gateway is of eth0 but for eth1 also picking same....i need to have whatever gateway asign to both diff nic should display diff.
I am trying to help you, but you will not do what I am asking you to do. It is making it really hard to help.
I need you to enter the following command and paste the output here.
cat /etc/sysconfig/network-scr ipts/ifcfg -eth0
It should look something like this:
HWADDR="00:26:98:84:AB:72"
DEVICE="p33p1"
ONBOOT="yes"
NM_CONTROLLED="no"
BOOTPROTO="static"
ONBOOT="yes"
IPADDR="192.168.1.2"
NETMASK="255.255.255.0"
I need you to enter the following command and paste the output here.
cat /etc/sysconfig/network-scr
It should look something like this:
HWADDR="00:26:98:84:AB:72"
DEVICE="p33p1"
ONBOOT="yes"
NM_CONTROLLED="no"
BOOTPROTO="static"
ONBOOT="yes"
IPADDR="192.168.1.2"
NETMASK="255.255.255.0"
ASKER
# Intel Corporation 82545EM Gigabit Ethernet Controller
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
HWADDR=00:50:56:84:42:09
NETMASK=225.255.255.0
IPADDR=172.21.41.21
GATEWAY=172.21.41.126
TYPE=Ethernet
This is just an sample ..and GATEWAY line can we some time up or down too.
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
HWADDR=00:50:56:84:42:09
NETMASK=225.255.255.0
IPADDR=172.21.41.21
GATEWAY=172.21.41.126
TYPE=Ethernet
This is just an sample ..and GATEWAY line can we some time up or down too.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks you
To filter the gateway address alone, you can use
route -n | grep '^0\.0\.\0\.0[ \t]\+[1-9][0-9]*\.[1-9][0-