Chinaunix首页 | 论坛 | 博客
  • 博客访问: 432263
  • 博文数量: 136
  • 博客积分: 5351
  • 博客等级: 少校
  • 技术积分: 1446
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-29 15:46
文章存档

2013年(2)

2012年(18)

2011年(116)

分类: Python/Ruby

2012-05-28 09:58:34

针对: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

                                                                                                  
阅读(4231) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~