| 0 comments ]

Some cases it's just easier to re-setup DHCP using a script. This script will setup a DHCP connection on a Wired Ethernet device and configure a hostname of your liking. This script has only been developed for RedHat/Fedora tm based systems running Open Client layer.

NetworkManager and static IP Setup

Static IP address users should consider turning off Network Manager first (see info below)

  1. Download the script by right clicking on the following link:
  2. Save the file to your Desktop (or some other accessible location):
  3. Open a terminal session by right clicking on Desktop and selecting Open Terminal (or locate the terminal icon from the menu)
  4. change directory to your Desktop (or wherever you downloaded the file) :
    cd $HOME/Desktop
  5. Type
    ls *network*
    to confirm it is there.
  6. type chmod a+x ./setup_network.sh
  7. run the command with: ./setup_network.sh
    [quickm@chad scripts]$ ./setup_network.sh
    +------------------------------------------------------------+
    | |
    | Configure DHCP IP Address on (eth0) ........... 1 |
    | |
    | Configure STATIC IP Address on (eth0) ........... 2 |
    | |
    | Change default device from (eth0) to ? ............ 3 |
    | |
    | Show current settings on (eth0) ................... 4 |
    | |
    | Quit ............................................. Q |
    | |
    +------------------------------------------------------------+
    Choose 1 - Q


  8. Select the number 1 and press the enter key to setup eth0,
    if you are unsure which device, click on option 3 to see what is available on your system.
  9. Input your full hostname (i.e. ILOVELINUX.fishkill.ibm.com), you can makeup any name you like but the domain (fishkill.ibm.com) should be site specific or use (ibm.com).

NetworkManager and static IP Setup

Static IP address users should consider turning off Network Manager.

The Network Manager will override static ipaddress configurations and therefore requires the option NM_CONFTROLLED=no included in /etc/sysconfig/network-scripts/ifcfg-eth0 config file. If Wireless is not being used, users may want to consider turning off the service all together (i.e. chkconfig --level 35 NetworkManager off or via the services graphical tool).

Testing:

reboot the system or restart the network service. service network restart


setup_network-1.sh:
#!/bin/bash
# ----------------------------------------------------------------#
# setup DHCP or static on REDHAT SYSTEMS ONLY
# ----------------------------------------------------------------#
# M QUICK v1.0b 2006/09

CMDLN_ARGS="$@" # Command line arguments for this script
export CMDLN_ARGS
empty_str=""
sleep_timer=5
USAGE="Usage: $0 [ static | dhcp ] [ ethX ]"

## GLOBAL IP VARIABLES
iface="eth0"
full_host=$empty_str
part_host=$empty_str
ipaddr=$empty_str
mask=$empty_str
gate=$empty_str
dns1=$empty_str
dns2=$empty_str
domain=$empty_str
sub_domain=$empty_str

# ----------------------------------------------------------------#
# Check for Root credentials
# ----------------------------------------------------------------#
# if passed silent option, will exit on failure.
chk_root () {

temp_cond="$1"
if [ ! $( id -u ) -eq 0 ]; then
if [ "$temp_cond" != "silent" ] ;then
echo "Please enter root's password."
exec su -c "${0} ${CMDLN_ARGS}" # Call this prog as root
# su -c \"$0 $@\" # another way to do it
exit ${?} # sice we're 'execing' above, we wont reach this exit
# unless something goes wrong.
else
# the 'silent' option has been set - just exit.
echo "root privilages have not been met ."
exit 1
fi
fi

}
# ----------------------------------------------------------------#
# Assign arguments to variables 'iface' and 'net_type'
# ----------------------------------------------------------------#
set_input_args(){

temp_val1="`echo $CMDLN_ARGS | awk '{print $1}'`"
temp_val2="`echo $CMDLN_ARGS | awk '{print $2}'`"

if [ `echo $temp_val1 | grep eth` ] ;then
iface=$temp_val1
net_type=$temp_val2
else
iface=$temp_val2
net_type=$temp_val1
fi
}
# ----------------------------------------------------------------#
# Check Passed in Values for correctness, quit on errors
# ----------------------------------------------------------------#
## confirm input values are correct, if not print usage mssg
check_input_args() {

set_input_args # acquire values passed to the script

# print warning if script is run on a non Red Hat system
if ! [ "`cat /etc/redhat-release | egrep 'Red|Hat'`" ] ;then
echo "WARNING: This script has only been tested on Red Hat based systems!"
sleep 3
fi

# print usage message if no values are passed (or fail to be assigned)
if [[ "$net_type" = "" || "$iface" = "" ]] ;then
echo "$USAGE"
exit 1;
fi

# print this if net_type isn't set correctly
if ! [[ "$net_type" = "static" || "$net_type" = "dhcp" ]] ;then
echo "Error: Invalid value ($net_type), either dhcp or static are valid ..."
echo "$USAGE"
exit 1;
fi

# print this if the interface is invalid
if ! [ "`echo $iface | grep eth`" ] ;then
echo "Error: Invalid interface ($iface), will not continue ..."
echo "$USAGE"
exit 1;
fi

# print this if the interface is not present on the system as a warning.
if ! [ "`/sbin/ifconfig | grep $iface`" ] ;then
echo "WARNING: $iface not found by ifconfig! "
sleep 2
fi
}
# ----------------------------------------------------------------#
# Check host name for validity
# ----------------------------------------------------------------#
isvalid_host() {
if [[ "`echo $1 | cut -d '.' -f1`" = "$empty_str" || "`echo $1 | cut -d '.' -f2`" = "$empty_str" || "`echo $1 | cut -d '.' -f3`" = "$empty_str" || "`echo $1 | cut -d '.' -f4`" = "$empty_str" ]] ;then
echo "invalid value [$1]"
exit 1
fi
if [ "$full_host" = "$part_host" ] ;then
echo "hostname error .. [ $full_host = $part_host ]"
exit 1
fi
}

# ----------------------------------------------------------------#
# Get Hostname and DSSO (global vars)
# ----------------------------------------------------------------#
# sets hostname values 'full_host' 'part_host' etc.
set_hostname_values() {

local full_host="$1"
part1="`echo $full_host | cut -d '.' -f1`"
part2="`echo $full_host | cut -d '.' -f2`"
part3="`echo $full_host | cut -d '.' -f3`"
part4="`echo $full_host | cut -d '.' -f4`"
if [[ "$part1" = "$empty_str" || "$part2" = "$empty_str" || "$part3" = "$empty_str" ]] ;then
echo "Error: Invalid hostname [ $full_host ] .."
exit 1
fi
part_host="$part1"
if [ "$part4" = "$empty_str" ] ;then
domain="$part2.$part3"
sub_domain="$part2.$part3"
else
domain="$part2.$part3.$part4"
sub_domain="$part3.$part4"
fi

}
# ----------------------------------------------------------------#
# Gather Enduser information on host/ip/etc.
# ----------------------------------------------------------------#
## Query user for ip info depending on static or dhcp choice
gather_ip_information() {
local net_type="$1"
if [ "$net_type" = "static" ] ;then
echo " * Static IP Address setup *"
echo " Please input the following information:"
echo " "
echo -e " Full Hostname of machine (xxxx.site.ibm.com):"
read full_host
set_hostname_values "$full_host"
echo -e " IP Address:"
read ipaddr
isvalid_host "$ipaddr"
echo -e " subnet mask:"
read mask
isvalid_host "$mask"
echo -e " Gateway:"
read gate
isvalid_host "$gate"
echo -e " DNS server 1:"
read dns1
isvalid_host "$dns1"
echo -e " DNS server 2:"
read dns2
isvalid_host "$dns2"
else
echo " * DHCP IP Address setup *"
echo " Please input the following information:"
echo " "
local confirm="NULL"
local x=0
echo "A default hostname is required :" ;sleep 2
while ! [ "$confirm" = "$full_host" ] ;do
if [ $x -gt 0 ] ;then
echo "Error, your values did not match, please try again .."
fi
echo " => Please input a FULL hostname for your system (i.e. host.fishkill.ibm.com ):"
read full_host
echo " => Please (re)input your desired full hostname (again):"
read confirm
(( x += 1 ))
done
set_hostname_values "$full_host"
fi
}
# ----------------------------------------------------------------#
# Setup DHCP Config files
# ----------------------------------------------------------------#
# Set the RH config files to DHCP
switch_to_dhcp(){

echo "Setting up /etc/hosts .. "
echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts
echo "127.0.0.1 $full_host $part_host" >> /etc/hosts
sleep 2

echo "Setting up /etc/sysconfig/network-scripts/ifcfg-$iface .."
echo "DEVICE=$iface" > /etc/sysconfig/network-scripts/ifcfg-$iface
echo "BOOTPROTO=dhcp" >>/etc/sysconfig/network-scripts/ifcfg-$iface
echo "ONBOOT=yes" >>/etc/sysconfig/network-scripts/ifcfg-$iface
echo "TYPE=Ethernet" >>/etc/sysconfig/network-scripts/ifcfg-$iface
sleep 2

echo "Setting up /etc/sysconfig/network .."
echo "HOSTNAME=$full_host" > /etc/sysconfig/network
echo "NETWORKING=yes" >> /etc/sysconfig/network
sleep 2

echo "Setting up /etc/resolv.conf .."
sleep 2
}

# ----------------------------------------------------------------#
# Setup STATIC Config files
# ----------------------------------------------------------------#
## set the RH config files to Static
switch_to_static(){

echo "Setting up /etc/hosts .. "
echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts
echo "$ipaddr $full_host $part_host" >> /etc/hosts
sleep 2

echo "Setting up /etc/sysconfig/network-scripts/ifcfg-$iface .."
echo "DEVICE=$iface" > /etc/sysconfig/network-scripts/ifcfg-$iface
echo "BOOTPROTO=static" >>/etc/sysconfig/network-scripts/ifcfg-$iface
echo "IPADDR=$ipaddr" >>/etc/sysconfig/network-scripts/ifcfg-$iface
echo "NETMASK=$mask" >>/etc/sysconfig/network-scripts/ifcfg-$iface
echo "GATEWAY=$gate" >>/etc/sysconfig/network-scripts/ifcfg-$iface
echo "HOSTNAME=$full_host" >>/etc/sysconfig/network-scripts/ifcfg-$iface
echo "ONBOOT=yes" >>/etc/sysconfig/network-scripts/ifcfg-$iface
echo "TYPE=Ethernet" >>/etc/sysconfig/network-scripts/ifcfg-$iface
sleep 2

echo "Setting up /etc/sysconfig/network .."
echo "HOSTNAME=$full_host" > /etc/sysconfig/network
echo "NETWORKING=yes" >> /etc/sysconfig/network
echo "GATEWAY=$gate" >> /etc/sysconfig/network
echo "NETWORKDELAY=10" >> /etc/sysconfig/network
sleep 2

echo "Setting up /etc/resolv.conf .."
echo "search $domain $sub_domain " > /etc/resolv.conf
echo "nameserver $dns1" >> /etc/resolv.conf
echo "nameserver $dns2" >> /etc/resolv.conf
sleep 2

}
# ----------------------------------------------------------------#
# ADD IBM DHCP QIP Information
# ----------------------------------------------------------------#
## IBM DHCP QIP addtion to "network" file
add_IBM_qip_settings() {
local quote=$'\042'
local dollar=`echo -e "\044"`
local one="1"
#local dollar_one="$dollar$one"
echo "# IBM QIP SETTINGS (added by $0 script)" >> /etc/sysconfig/network
echo "if [ -x /opt/ibm/c4eb/bin/valid-qip-hostname ]; then" >> /etc/sysconfig/network
echo " #" >> /etc/sysconfig/network
echo " # Ensure hostnames like 'localhost' or 'loopback' are not used." >> /etc/sysconfig/network
echo " #" >> /etc/sysconfig/network
echo -e " /opt/ibm/c4eb/bin/valid-qip-hostname \042\044HOSTNAME\042" >> /etc/sysconfig/network
echo "fi" >> /etc/sysconfig/network
echo -e "if [ \044? -eq 0 ]; then" >> /etc/sysconfig/network
echo " # hostname is OK, so propogate to DNS" >> /etc/sysconfig/network
echo -e " DHCP_HOSTNAME=\042\044HOSTNAME\042" >> /etc/sysconfig/network
echo "fi" >> /etc/sysconfig/network
echo "# also the following will cause dhclient to append ibm.com to domain" >> /etc/sysconfig/network
echo -e "new_domain_name=\042\044{new_domain_name} ibm.com\042" >> /etc/sysconfig/network
echo "# ifup will brink up wireless device" >> /etc/sysconfig/network
echo "check_link_down()" >> /etc/sysconfig/network
echo "{" >> /etc/sysconfig/network
echo -e " if ! LC_ALL=C iwconfig $dollar$one 2>&1 | grep -q \042no wireless extensions\042; then" >> /etc/sysconfig/network
echo " return 1" >> /etc/sysconfig/network
echo " fi" >> /etc/sysconfig/network
echo " if [ -x /sbin/mii-tool -o -x /sbin/ethtool ]; then" >> /etc/sysconfig/network
echo -e " if ! LC_ALL=C ip link show dev $dollar$one 2>/dev/null| grep -q UP ; then" >> /etc/sysconfig/network
echo -e " ip link set dev $dollar$one up >/dev/null 2>&1" >> /etc/sysconfig/network
echo " fi" >> /etc/sysconfig/network
echo " timeout=0" >> /etc/sysconfig/network
echo -e " while [ \044timeout -le 10 ]; do" >> /etc/sysconfig/network
echo -e " check_mii_tool $dollar$one" >> /etc/sysconfig/network
echo -e " m=\044?" >> /etc/sysconfig/network
echo -e " check_ethtool $dollar$one" >> /etc/sysconfig/network
echo -e " e=\044?" >> /etc/sysconfig/network
echo -e " if [ \044m -eq 1 ] || [ \044e -eq 1 ] ; then" >> /etc/sysconfig/network
echo " return 1" >> /etc/sysconfig/network
echo " fi" >> /etc/sysconfig/network
echo -e " if [ \044m -eq 2 ] && [ \044e -eq 2 ] ; then" >> /etc/sysconfig/network
echo " return 1" >> /etc/sysconfig/network
echo " fi" >> /etc/sysconfig/network
echo " usleep 500000" >> /etc/sysconfig/network
echo -e " timeout=\044((timeout+1))" >> /etc/sysconfig/network
echo " done" >> /etc/sysconfig/network
echo " return 0" >> /etc/sysconfig/network
echo " fi" >> /etc/sysconfig/network
echo " return 1" >> /etc/sysconfig/network
echo "}" >> /etc/sysconfig/network
}

# ----------------------------------------------------------------#
# Print Values Compiled
# ----------------------------------------------------------------#
print_input_variables(){

echo " Network type: $net_type"
echo " Interface: $iface"
echo " Full Host: $full_host"
echo " Part Host: $part_host"
echo " IP address: $ipaddr"
echo " Net Mask: $mask"
echo " Gateway: $gate"
echo " DNS: $dns1 $dns2"
echo " Domain: $domain $sub_domain"
}
# ----------------------------------------------------------------#
# Menu for Main function
# ----------------------------------------------------------------#
menu(){

printf " +------------------------------------------------------------+\n"
printf " | |\n"
printf " | Configure DHCP IP Address on ($iface) ........... 1 |\n"
printf " | |\n"
printf " | Configure STATIC IP Address on ($iface) ........... 2 |\n"
printf " | |\n"
printf " | Change default device from ($iface) to ? ............ 3 |\n"
printf " | |\n"
printf " | Show current settings on ($iface) ................... 4 |\n"
printf " | |\n"
printf " | Quit ............................................. Q |\n"
printf " | |\n"
printf " +------------------------------------------------------------+\n"
echo " Choose 1 - Q "
}
# ----------------------------------------------------------------#
# Netowrk Main function
# ----------------------------------------------------------------#
setup_network() {
#check_input_args # check arguments
#chk_root
gather_ip_information "$net_type" # query user for ip info
print_input_variables # print data

echo " Continuing in $sleep_timer seconds, press cntrl + C to stop ... "
sleep $sleep_timer

# set hostname now (not necessary)
hostname $full_host

# switch to static or dhcp functions
if [[ "$net_type" = "static" ]] ;then
switch_to_static
else
switch_to_dhcp
fi

add_IBM_qip_settings #add IBM qip lines to "network" file

echo " Finished ... (restart network settings)"
}
show_status() {

temp="`ifconfig $iface | grep inet | awk '{print $2}' | cut -d: -f 2| cut -d. -f 1,2`"
echo " ------------------------------ "
echo " ip addr: `ifconfig $iface | grep inet | awk '{print $2}' | cut -d: -f 2`"
temp="`ifconfig $iface | grep inet | awk '{print $2}' | cut -d: -f 2 | cut -d. -f 1,2`"
echo " route: `route | grep $temp | awk '{print $1}'`"
echo " gate: `ifconfig $iface | grep inet | awk '{print $4}' | cut -d: -f 2`"
echo " MAC: `ip link show $iface | grep ff:ff | awk '{print $2}'`"
echo " device: `/sbin/mii-tool $iface`"
echo " ------------------------------ "
}
change_iface(){
set -a iface_list
x=1
echo "Available Devices On System:"
echo "-----------------------------------"
for each in `/sbin/ifconfig | grep Link | grep -v lo | grep -v vmnet | awk '{print $1}'` ;do
iface_list[$x]=$each
echo " $x ..................... ${iface_list[$x] }"
(( x+=1 ))
done
echo " Q ..................... QUIT"
echo " Select an avaiable device: 1 - X "
read -e choice
if [ "$choice" = "q" ] || [ "$choice" = "Q" ] ;then
echo "nothing selected ..."
else
echo "Setting device to ${iface_list[$choice]} ..."
iface=${iface_list[$choice]}
fi
}
network_manager(){
# input for this script is on or off.. ie network_manager on
input=$1
if [ "$input" = "off" ] || [ "$input" = "OFF" ] ;then
echo "turning off NetworkManager, you will lose the desktop applet ..";sleep 2
/sbin/service NetworkManager stop
/sbin/chkconfig --level 35 NetworkManager off
echo "to turn back on -> /sbin/service NetworkManager start;/sbin/chkconfig --level 35 NetworkManager on"
elif [ "$input" = "on" ] || [ "$input" = "ON" ] ;then
echo "turning on NetworkManager, you will be overridden with the desktop applet ..";sleep 2
/sbin/service NetworkManager start
/sbin/chkconfig --level 35 NetworkManager on
echo "finished ... run script again for static to turn off .. "
else
echo "No action was taken with network_manager ... ";sleep 2
fi
}

# ----------------------------------------------------------------#
# MAIN
# ----------------------------------------------------------------#

if [ ! $( id -u ) -eq 0 ]; then
echo " $HOSTNAME "
chk_root # check for root
fi
while [ 1 ] ;do
menu
read choice
case $choice in
1)
net_type="dhcp"
setup_network
network_manager "on"
;;
2)
net_type="static"
setup_network
network_manager "off"
;;
3) change_iface ;sleep 1
;;
4) show_status
sleep 4
;;
*) echo "Quitting application ..."
exit 1
;;
esac
done
# ----------------------------------------------------------------#
# ----------------------------------------------------------------#

0 comments

Post a Comment