1. 服务器网卡配置文件

/etc/sysconfig/network/ifcfg-***(eth0)

linux-f1s9:/etc/sysconfig/network # cat ifcfg-eth0
BOOTPROTO='static'
BROADCAST=''
ETHTOOL_OPTIONS=''
IPADDR='10.148.128.200/24'
MTU=''
NAME='Broadcom Ethernet controller'
NETWORK=''
REMOTE_IPADDR=''
STARTMODE='auto'
USERCONTROL='no'

配置说明:

BOOTPROTO=static  网卡获得ip地址的方式
                  Static(静态 ip地址)
                  dhcp(通过dhcp协议获取ip)
                  bootip通过bootp协议获得的ip地址
BROADCAST=192.168.0.255 子网广播地址
HWADDR=:::8E::EE 网卡物理地址
IPADDR=12.168.1.117 网卡IP地址
IPV6INIT=no 是否启用IPV6
IPV6_AUTOCONF=no
NETMASK=255.255.255.0 网卡对应网络掩码
NETWORK=192.168.1.0 网卡对应的网络地址
ONBOOT=yes 系统启动时是否设置此网络接口,设置为yes时,系统启动时激活此设备。默认设置为yes

备注:IP netmask有以下两种写法:

    1.  IPADDR='10.148.128.200/24'     (合并)

    2.  IPADDR='10.148.128.200'       NETMASK=255.255.255.0  (分开)

     如果两种写法都存在,'10.148.128.200/24'  方式优先级更高。

2. 后台Shell脚本

2.1 获取IP信息

此处是通过ifconfig -a命令截取,当网卡名比较长的时候网卡名称实际上是显示不完整的。

#!/bin/bash
#########################################
#SCRIPT: getIPInfo.sh
#PLATFORM: Not platform dependent
#PURPOSE:获取网卡信息
#########################################
cd `dirname $`
ipinfo=host_ip_info.properties
logFile=/srv/ftpd/log/iptool.log
dealedCard="" function toInfoLog()
{
echo "$(date +%Y-%m-%d) $(date +%H:%M:%S) INFO:$@" >> $logFile
} if [ -f $ipinfo ]; then
rm -f $ipinfo
fi # 只显示网卡配置文件中的IP,不显示IP映射的监听IP。根据":v" 过滤,
netcards=`/sbin/ifconfig -a | awk '/Link encap:Ethernet/{print $1}'|grep -v ":v"`
for ncard in ${netcards}
do
flag=$(echo ${dealedCard}|grep ${ncard})
if [ "X$flag" != "X" ]
then
toInfoLog "read same netcard $ncard."
continue
fi
dealedCard="${dealedCard} $ncard"
# web界面无法显示通过yast新增的IP地址(网卡名称过长)
ips=`/sbin/ifconfig -a |grep "${ncard} " -A | awk -F: '/inet addr:/{print $2}' | awk '{print $1}'`
masks=`/sbin/ifconfig -a |grep "${ncard} " -A | awk -F: '/Mask:/{print $4}'`
count=
for ip in $ips
do
count=`expr $count + `
mask=$(echo $masks |cut -d' ' -f $count)
toInfoLog "$ncard $ip/${mask}"
#记录网卡IP信息
echo "$ncard $ip/${mask}" >> $ipinfo
done done

2.1  虚拟IP新增/删除操作

#!/bin/bash
#########################################
#SCRIPT: config_sysvirtual_ip.sh
#PLATFORM: Not platform dependent
#PURPOSE: [新增]或[删除]虚拟ip
#参数列表: $ 操作类型(add|del)
# $ 虚拟ip
# $ 掩码(例:)
# $ 网卡名
#例如:./config_sysvirtual_ip.sh mode 10.10.10.1 (255.255.255.0) eth0
#########################################
FULL_PATH=$
PATH_BIN=${FULL_PATH%%/config_sysvirtual_ip.sh*}
cd $PATH_BIN mode=$;
ip=$;
mask=$
netcard=$;
net_name=`echo $netcard|awk -F: '{print $1}'`
logFile=/srv/ftpd/log/iptool.log function toInfoLog()
{
echo "$(date +%Y-%m-%d) $(date +%H:%M:%S) INFO:$@" >> $logFile >&
} function toErrorLog()
{
echo "$(date +%Y-%m-%d) $(date +%H:%M:%S) ERROR:$@" >> $logFile >&
} # function get_netfile()
# {
# netcard=$
# net=`echo $netcard|awk -F: '{print $1}'`
# netdir="/etc/sysconfig/network"
# netfile="$netdir/ifcfg-$net"
# echo $netfile
# } function get_newlable()
{
tmp_lable=;
flag=;
#web添加的label号可能与YAST修改过的别名冲突。
#lable_list=`cat $net_cfgfile| grep LABEL|awk -F= '{print $2}'| awk -F\' '{print $}'`
lable_list=`cat $net_cfgfile| grep LABEL|awk -F= '{print $1}'| awk -F_ '{print $2}'` if [ -z "$lable_list" ]; then
new_lable=;
flag=;
else
for lable in $lable_list; do
if [ $lable -ge $tmp_lable ]; then
tmp_lable=$lable;
fi
done
fi
if [ $flag -eq ];then
new_lable=$(($tmp_lable+))
fi
echo "$tmp_lable $lable_list"
echo "new virtual ip_lable =$new_lable"
} function valid_ip()
{
for used_ip in `ifconfig | awk -F'addr:|Bcast' '/Bcast/{print $2}'`; do
if [ $used_ip = "$ip" ]; then
echo "exited ip $ip"
exit
fi
done
echo "valid ip"
} function add_ip()
{
valid_ip;
get_newlable;
if [ $? -eq ]; then
echo "exited ip"
exit
fi /sbin/ifconfig $net_name:$new_lable $ip_mask
if [ $? -eq ]; then
sed -i "$ a\LABEL_$new_lable='w$new_lable'" $net_cfgfile
sed -i "$ a\IPADDR_$new_lable='$ip_mask'" $net_cfgfile
echo "set virtual ip $ip_mask to $net_name:$new_lable"
/sbin/rcnetwork restart $net_name
else
echo "Wrong parameters"
exit ;
fi
} function del_ip()
{
#grep \'${ip}[\/\'] IP之后以\或'结尾。兼容IPADDR_1='3.3.3.3/24' 和IPADDR_1='3.3.3.3'
lable=`cat $net_cfgfile|grep \'${ip}[\/\']| awk -F= '{print $}'|awk -F_ '{print $}'`
if [ -n "$lable" ];then
#LABEL_X='Y' 通过yast操作过以后X会重排,X和Y不一定相等。其次IPADDR_label ip/mask不一定是在一行。
sed -i "/^ *LABEL_$lable=/d" $net_cfgfile;
sed -i "/IPADDR_$lable='$ip/d" $net_cfgfile;
# 删除对应可能存在的NETMASK行
sed -i "/NETMASK_$lable='/d" $net_cfgfile;
echo "delet ip: ${net_name}:w${lable} ${ip_mask}. rntCode=$?"
/sbin/rcnetwork restart $net_name
else
echo "Cannot del:no exits valid ip"
exit ;
fi
} # begin
# 校验IP和netmask有效性 ,略 net_cfgfile="/etc/sysconfig/network/ifcfg-$net_name"
ip_mask=$ip/$mask
new_lable=; if [ $# -eq -a -f $net_cfgfile ]; then
if [ $mode = "add" ];then
toInfoLog "add ip begin"
add_ip $mode $ip $long_mask $net_name >> $logFile >&
elif [ $mode = "del" ];then
toInfoLog "delete ip begin"
del_ip $mode $ip $long_mask $net_name >> $logFile >&
else
toErrorLog "Usage. $0 <add|del> <ip> <netmask> <netcard>"
exit ;
fi
else
toErrorLog "Usage. $0 <add|del> <ip> <netmask> <netcard>"
exit ;
fi

2.2 IP修改操作

修改操作可能会影响原有业务,除了修改网卡配置文件之外,还需要同步更新相关操作系统文件(如:/etc/hosts,/etc/ssh/sshd_config, /etc/vsftpd.conf等文件)中该IP信息。

入口:

#!/bin/bash
#########################################
#SCRIPT: modIPInfo.sh
#PURPOSE:修改IP
#########################################
FULL_PATH=$
PATH_BIN=${FULL_PATH%%/modIPInfo.sh*}
cd $PATH_BIN # 记日志统一格式输出
function toInfoLog()
{
echo "$(date +%Y-%m-%d) $(date +%H:%M:%S) INFO:$@"
} # 备份文件
function backup_proc()
{
toInfoLog "backup files begin..."
backup_restore_file backup $netcard
} # 回退文件
function restore_proc()
{
# 恢复文件即可。
backup_restore_file restore $netcard
#modify_proc $netcard $oldIp $oldmask $newIp $newmask
} # 修改IP
function modify_proc()
{
toInfoLog "modify_proc begin"
# 更新操作系统相关文件 目前只有/etc/hosts
modify_OS_file $oldIp $newIp
# 更新网卡配置文件
modify_net_file $netcard $newIp $newmask $oldIp $oldmask
} # main
function modifiyIP()
{
toInfoLog "mode ip begin"
backup_proc
modify_proc
if [ $? -ne ];then
toInfoLog "modify_net_file failed,restore ip."
restore_proc $netcard $oldIp $oldmask $newIp $newmask
exit
fi
toInfoLog "modifiyIP $newIp success."
} # begin
if [ $# -ne ]
then
echo "Usage. $0 <netcard> <newIp> <newmask(24)> <oldIp> <oldmask(24)>"
exit
fi
netcard=$
newIp=$
newmask=$
oldIp=$
oldmask=$ log_file=/srv/ftpd/log/iptool.log . /opt/tool/iptool/modIPInfoUtil.sh
modifiyIP >> ${log_file} >&

修改方法

#!/bin/bash
#########################################
#SCRIPT: modIPInfoUtil.sh
#PLATFORM: Not platform dependent
#PURPOSE: 修改IP
#参数列表:
#params:netcard newip newmask oldip oldmask
######################################### filebackupPath=/opt/tool/iptool # 记日志统一格式输出
function toInfoLog()
{
echo "$(date +%Y-%m-%d) $(date +%H:%M:%S) INFO:$@"
}
function toErrorLog()
{
echo "$(date +%Y-%m-%d) $(date +%H:%M:%S) ERROR:$@"
} function bak_restore_file()
{
mode=$
filename=$
if [ $# != ];then
toErrorLog "bak_restore_file params number error"
exit
fi
fileabs=$(echo $filename |awk -F/ '{print $NF}')
filebackup=${filebackupPath}/${fileabs}_lastBak
if [ $mode = "backup" ];then
if [ -f "${filename}" ];then
cp -rfp $filename $filebackup
if [ $? = ];then
toInfoLog "backup $filename success"
else
toErrorLog "backup $filename fail"
exit
fi
fi
elif [ $mode = "restore" ];then
if [ -f "$filebackup" ];then
cp -rfp $filebackup $filename
if [ $? = ];then
toInfoLog "restore $filename success"
else
toErrorLog "restore $filename fail"
exit
fi
fi
fi } function get_netfile()
{
netcard=$
net=`echo $netcard|awk -F: '{print $1}'`
netdir="/etc/sysconfig/network"
netfile="$netdir/ifcfg-$net"
echo $netfile
} function bak_restore_OS_file()
{
mode=$
netcard=$
#备份网卡信息
netfile=`get_netfile $netcard`
toInfoLog "netfile= $netfile"
bak_restore_file $mode $netfile bak_restore_file $mode /etc/hosts
# bak_restore_file $mode /etc/vsftpd.conf #备份dns文件
# bak_restore_file $mode /etc/resolv.conf
# bak_restore_file $mode /etc/named.conf
} function backup_restore_file()
{
mode=$
netcard=$
bak_restore_OS_file $mode $netcard
} #判断ip是否存在,exist——存在,notexist——不存在
function check_file_status()
{
file=$
ip=$
filter=`cat $file|grep $ip` if [ "X$filter" != "X" ];then
echo "exist"
else
echo "notexist"
fi
} #文件存在ip才进行替换,替换后判断新ip是否存在,不存在替换失败
function replaceIP()
{
oldIp=$
newIp=$
file=$
result=`check_file_status $file $oldIp` toInfoLog "replaceIP $file $oldIp $result"
if [ $result = "exist" ];then
toInfoLog "replaceIP $file oldIp=$oldIp ,newIp=$newIp "
sed -i 's/'${oldIp}'/'${newIp}'/g' $file
result=`check_file_status $file $newIp`
if [ $result = "notexist" ];then
toErrorLog "replace $file $oldIp to $newIp failed"
return
fi
fi
} function modify_net_file()
{
netcard=$
newIp=$
newmask=$
oldIp=$
oldmask=$ #备份网卡信息
netfile=`get_netfile $netcard` result=`check_file_status $netfile \'${newIp}[\/\']`
if [ $result = "exist" ];then
toErrorLog "newIp ${newIp} aready exist, modip failed"
return
fi new_ip_mask=${newIp}\/${newmask}
toInfoLog "new_ip_mask=$new_ip_mask ,old_ip_mask=${oldIp}/${oldmask}"
# 替换匹配模式兼容IP/mask 和IP、mask分行(预装环境)的情况 .'${oldIp}[\/\'].* 精确匹配,以防误修改。
sed -i "s/'${oldIp}[\/\'].*/'${newIp}\/${newmask}'/g" $netfile
result=`check_file_status $netfile $new_ip_mask`
if [ $result = "notexist" ];then
toErrorLog "replace ip failed"
return
fi
toInfoLog "replace ip ok. network restart..."
net=`echo $netcard|awk -F: '{print $1}'`
/sbin/rcnetwork restart $net
} #查找文件是否存在ip
check_OS_file()
{
ip=$
check_file_status /etc/ssh/sshd_config $ip
check_file_status /etc/hosts $ip
check_file_status /etc/my.cnf $ip
check_file_status /etc/vsftpd.conf $ip
#dns
check_file_status /var/lib/named/tdtech.com $ip
check_file_status /etc/resolv.conf $ip
check_file_status /etc/named.conf $ip
} # 修改IP时更新sshd_config对应IP 暂不用
function modify_OS_file()
{
oldIp=$
newIp=$
replaceIP $oldIp $newIp /etc/hosts
#sshd 暂不涉及
# replaceIP $oldIp $newIp /etc/ssh/sshd_config
# if [ -n "`service sshd status |grep running`" ]
# then
# service sshd restart
# fi #vsftp 暂不涉及
# replaceIP $oldIp $newIp /etc/vsftpd.conf
# if [ -n "`service vsftpd status |grep running`" ]
# then
# service vsftpd restart
# fi #db 暂不涉及
# replaceIP $oldIp $newIp /etc/my.cnf
# /opt/UBP/bin/modifydb_ip.sh $oldIp $newIp #dns
# replaceIP $oldIp $newIp /var/lib/named/tdtech.com
# replaceIP $oldIp $newIp /etc/resolv.conf
# replaceIP $oldIp $newIp /etc/named.conf
}

2.3  修改网关

比较简单,根据传入的参数更新文件/etc/sysconfig/network/routes并重启网卡。

#!/bin/bash
#########################################
#SCRIPT: updateGateway.sh
#PLATFORM: Not platform dependent
#PURPOSE: 修改Gateway
#参数列表:
#########################################
FULL_PATH=$
PATH_BIN=${FULL_PATH%%/updateGateway.sh*}
cd $PATH_BIN oldGateWay=$
gatewayAddr=$
gatewayfile=/etc/sysconfig/network/routes
Logfile=IPConfig.log function toInfoLog()
{
echo "$(date +%Y-%m-%d) $(date +%H:%M:%S) INFO:$@" }
function toErrorLog()
{
echo "$(date +%Y-%m-%d) $(date +%H:%M:%S) ERROR:$@"
}
#检查
function checkGateway()
{
if [ "X${gatewayAddr}" = "X" ]
then
toErrorLog "gatewayAddr null."
exit
fi
if [ "${oldGateWay}" = "${gatewayAddr}" ]
then
echo "mod gatewayAddr is same as before."
exit
fi
} function modGateway()
{
checkGateway
toInfoLog "begin update gateway."
# 备份
cp ${gatewayfile} ${gatewayfile}_bak
cat ${gatewayfile} >> ${Logfile}
# 修改
echo "default ${gatewayAddr} - -" > ${gatewayfile}
if [ $? -ne ]
then
cp ${gatewayfile}_bak ${gatewayfile}
toErrorLog "update ${gatewayfile} failed."
exit
fi
# 重启
service network restart
if [ $? -ne ]
then
cp ${gatewayfile}_bak ${gatewayfile}
service network restart
toErrorLog "update ${gatewayfile} failed when restart."
exit
fi
toInfoLog "update gateway success. ${gatewayAddr}"
} modGateway >> ${Logfile} >&

3. 前端配置页面

为了方便配置,web页面中提供IP配置功能,后台配置的修改由上述Shell脚本完成。

最新文章

  1. 【前端】iterable类型的 forEach方法
  2. zzy:java采用的是16位的Unicode字符集作为编码方式------理解
  3. remove adapter
  4. sql ltrim rtrim
  5. Atitit.软件的建模种类and 建模语言选型and UML???
  6. Fox Roddick interviw Federer before 2013 US Open
  7. hibernate逆向工程生成的实体映射需要修改
  8. Android ContentProvider完整案例
  9. java实现随机验证码的图片
  10. Andio Studio
  11. Hibernate基础学习(三)&mdash;Session
  12. 不完全图解HTTP
  13. 扩展Spring切面功能
  14. python通过一个语句分析几个常用函数和概念
  15. 一览Django框架(转载)
  16. hihocoder 1496 寻找最大值
  17. 关于springboot2.x 的 RedisCacheManager变化
  18. vue-i18n国际化插件
  19. 42.纯 CSS 创作一个均衡器 loader 动画
  20. ArcGIS Server + ArcGIS Portal 10.5

热门文章

  1. **JLink Warning: Mis-aligned memory write: Address: 0x20000000, NumBytes: 2, Alignment: 2 (Halfword-aligned)
  2. Vagrant安装步骤
  3. shell 命令 链接文件 ln
  4. C++之指针与数组区别
  5. Bootstrap 附加导航(Affix)插件
  6. Python 读取本地*.txt文件 替换 内容 并保存
  7. 一个切图仔的HTML笔记
  8. 学习Caffe(一)使用Caffe
  9. [NOI 2018]冒泡排序
  10. 线程池_ThreadPool