Sunday, September 6, 2009

Scripts for Automatic Network Configuration in Linux

Automatic network configuration in Redhat Linux

Pls run this scripts for Automatic network Configuration

#!/bin/sh
echo " =============================================="
echo " ITchoice Network Config for Redhat Linux "
echo " =============================================="
# config_network: Configure network settings
#
NETFILE=/etc/sysconfig/network
ETHFILE=/etc/sysconfig/network-scripts/ifcfg-eth
HOSTFILE=/etc/hosts
DNSFILE=/etc/resolv.conf
SSHFILE=/etc/ssh/sshd_config
CURIP=x.x.x.x

#
#if none of the files had ONBOOT set assume eth0
#
CURPORT=0
#
echo "Automatic network configuration"
echo ""
echo -n "Enter system hostname (default:xxxxxx ): "
read HOSTNAME
if ( test "$HOSTNAME" = "")
then
HOSTNAME="xxxxxx"
fi

echo -n "Enter IP address (default: x.x.x.x): "
read IPADDR
if ( test "$IPADDR" = "")
then
IPADDR="x.x.x.x"
fi
#Putting the values in /etc/hosts
cat /etc/hosts|grep -i $IPADDR >/dev/null 2>&1
if [ $? != "0" ];then
echo "$IPADDR $HOSTNAME" >> /etc/hosts
fi
echo " x.x.x.x itpc.itchoice.com" >> /etc/hosts
echo " x.x.x.x itpc2.itchoice.com" >> /etc/hosts
echo -n "Enter gateway (default:x.x.x.x): "
#read GATEWAY
if ( test "$GATEWAY" = "")
then
GATEWAY="x.x.x.x"
fi
echo -n "Enter netmask (default: x.x.x.x): "
#read NETMASK
if ( test "$NETMASK" = "")
then
NETMASK="x.x.x.x"
fi
rm -rf /etc/resolv.conf
touch /etc/resolv.conf
chmod 755 /etc/resolv.conf
echo "search itchoice.com
nameserver x.x.x.x
nameserver x.x.x.x
nameserver x.x.x.x" >> /etc/resolv.conf

#
# Confirm with user
#
echo ""
echo ""
echo "hostname: $HOSTNAME"
echo "IP address: $IPADDR"
echo "Gateway: $GATEWAY"
echo "Netmask: $NETMASK"
echo ""
echo -n "Is this correct (Y/N): "
read ANSWER
if ( test "$ANSWER" = "N" -o "$ANSWER" = "n" )
then
exit
fi
#
# Find out which port is currently running
# Added code to check if file exists
#
for ((i = 0; i <= 2; i++)) do if [ -e "${ETHFILE}$i" ]; then grep ONBOOT=yes ${ETHFILE}$i > /dev/null 2>&1
if [ $? -eq 0 ]; then
CURPORT=$i
fi
fi
done
#
# Strip this IP address from the nodelist.txt file
#
CURHOST=`uname -n`
if [ -e "${ETHFILE}${CURPORT}" ]; then
CURIP=`grep IPADDR ${ETHFILE}${CURPORT} | sed 's/IPADDR=//g'`
fi
#
echo "Re-creating file $NETFILE"
echo "HOSTNAME=$HOSTNAME">$NETFILE
echo "GATEWAY=$GATEWAY">>$NETFILE
echo "NETWORKING=yes">>$NETFILE
#
# Write the hosts file
#
echo ""
echo "Re-creating file $HOSTFILE"
echo "127.0.0.1 localhost">$HOSTFILE
echo "$IPADDR $HOSTNAME">>$HOSTFILE
#
echo "Restoring file ${ETHFILE}${CURPORT}"
echo "DEVICE=eth${CURPORT}">${ETHFILE}${CURPORT}
echo "IPADDR=$IPADDR">>${ETHFILE}${CURPORT}
echo "BOOTPROTO=static">>${ETHFILE}${CURPORT}
echo "NETMASK=$NETMASK">>${ETHFILE}${CURPORT}
echo "MII_NOT_SUPPORTED=yes">>${ETHFILE}${CURPORT}
echo "ONBOOT=yes">>${ETHFILE}${CURPORT}
echo "Bounce the network..."
echo "eth$CURPORT is $HOSTNAME at address $IPADDR on gateway $GATEWAY"
ifdown eth${CURPORT}
ifup eth${CURPORT}
#
# Change the hostname
#
hostname $HOSTNAME
#
cat /etc/ssh/sshd_config | sed 's/#PermitRootLogin/PermitRootLogin/g' >/tmp/kesu.conf
mv /tmp/kesu.conf /etc/ssh/sshd_config
#echo 'PermitRootLogin yes' >>/etc/ssh/sshd_config
cat /etc/ssh/sshd_config | sed 's/#PasswordAuthentication/PasswordAuthentication/g' >/tmp/kesu1.conf
mv /tmp/kesu1.conf /etc/ssh/sshd_config
chkconfig network on
chkconfig sshd on
chkconfig autofs on
chkconfig nscd on
chkconfig ip6tables off
chkconfig iptables off
service iptables stop
service ip6tables stop
service network restart
service sshd restart

No comments:

Post a Comment