linux用ip命令得到的ip不是静态,shell实现动态ip地址获取改为静态ip

针对:http://blog.chinaunix.net/uid-25760152-id-3215750.html

问题写一个脚本实现自动改一下ip上网,出现那个问题的原因现在还没弄清楚,再想想。

#!/bin/bash

#this shell program file is  used when you

# are in a new net environment;you can get an ip

#,but you can not ping an external netip

#then you can surf the Internet by using a static ip

conf_file="/etc/sysconfig/network-scripts/ifcfg-eth0"

ip=`ifconfig  eth0 |  grep  "inet addr"`

ipaddr=`echo   $ip  | awk -F:  '{ print $2 }' | awk  '{print $1}'`

#ipaddr=`echo   $ip  | awk -F:  '{ print $2 }' |  sed   -r 's/ +\w+$//g' `

#ipaddr=`echo $ip | awk -F: '{print $2}' | sed -r 's/[a-Z]+$//'`

gateway=`echo "$ipaddr" | awk -F "." '$4="1"' | sed  -e 's/ /\./g'`

ping -c 4  g.cn >> /tmp/ping_result.txt

first_line=`cat /tmp/ping_result.txt | sed -n '1p'`

if ( [[ "$first_line" =~ "unkown host " ]] ); then

cat  $conf_file | sed  '/BOOTPROTO/d'  >>ifcfg_1.txt

cat  ifcfg_1.txt > $conf_file

cat $conf_file | grep IPADDR|awk -F "=" 'gsub($2,"'$ipaddr'")'  >>ifcfg_2.txt

cat $conf_file | grep GATEWAY|awk -F "=" 'gsub($2,"'$gateway'")' >>ifcfg_2.txt

cat $conf_file |sed -e  '/IPADDR/d' -e '/GATEWAY/d' >ifcfg_3.txt

cat ifcfg_2.txt | sed  -e  's/\w\+ /&=/g' >>ifcfg_4.txt

cat   ifcfg_3.txt  >$conf_file

cat   ifcfg_4.txt >>$conf_file

echo  "BOOTPROTO=static" >>$conf_file

fi

# at  last you should  remove  the  file  /tmp/ping_result.txt

rm  /tmp/ping_result.txt

rm  ./ifcfg_1.txt

rm  ./ifcfg_2.txt

rm  ./ifcfg_3.txt

rm  ./ifcfg_4.txt

38,1          Bot

阅读(3684) | 评论(0) | 转发(0) |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值