早上要写个自动化的脚本,从DHCP切换到Static具体不多说了,直接贴脚本吧!
- #!/bin/sh
- # Get current network configration
- ETH=`ip route | awk '$5 ~ /eth[0-9]/ {print $5}'`
- ADDR=`ifconfig $ETH | grep "inet addr" | awk -F: '{ print $2 }'| awk '{ print $1 }'`
- BCAST=`ifconfig $ETH | grep "inet addr" | awk -F: '{ print $3 }'| awk '{ print $1}'`
- MASK=`ifconfig $ETH | grep "inet addr" | awk -F: '{ print $4 }'`
- GATEWAY=`ip route | grep default | awk '{ printf $3 }'`
- FILE=/etc/sysconfig/network-scripts/ifcfg-$ETH
- # Modify network configration file
- cp $FILE $FILE.bak
- sed -i 's/dhcp/static/g' $FILE
- echo "BROADCAST="$BCAST >> $FILE
- echo "IPADDR="$ADDR >> $FILE
- echo "NETMASK="$MASK >> $FILE
- echo "GATEWAY="$GATEWAY >> $FILE
- # Restart network service
- service network restart
- if [ $? -ne 0 ]; then
- echo "Failed to switch dhcp to static, reverse to dhcp now!"
- cp $FILE.bak $FILE
- service network restart
- else
- echo "Success to switch dhcp to static!"
- fi
脚本相当简单,就不多说了!
阅读(5404) | 评论(1) | 转发(0) |