6.6

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

转--输入ip显示对应域名

已知:/etc/hosts的内容为
192.168.1.11  oldboy11.etiantian.org
192.168.1.21  oldboy21.etiantian.org
192.168.1.31  oldboy31.etiantian.org
#192.168.1.111  oldboy111.etiantian.org
请用shell脚本实现,怎么才能在输入IP后找到/etc/hosts里对应的唯一的hostname?

 

法1)脚本过滤法

1. [root@old_boy scripts]# cat judgehost.sh   

2. #!/bin/bash  

3. echo "please input ip address:" 

4. read ip  

5. [ -n "`grep "$ip " /etc/hosts`" ] && \  #注意前面的过滤条件结尾带有空格。  

6. echo "The hostname is: `grep "$ip " /etc/hosts |awk '{print $2}'`" || \  

7. echo "The ip is invalid" 

 提示:
1)这是一个grep过滤加条件判断的实现语法:
2)条件判断语法为[ -n "ddd" ] && echo 1 || echo 0
3)[ -n "`grep "$ip " /etc/hosts`" ] && \  #注意前面的过滤条件结尾带有空格。这里啊,是为了排除下面的重复情况
 192.168.1.11  oldboy11.etiantian.org
 192.168.1.111  oldboy111.etiantian.org
----------------我是每种方法分隔符---------------
法2)脚本精确匹配法:

1. #!/bin/bash  

2. #author oldboy  

3. #qq 31333741  

4. #judge input  

5. if [ $# -ne 1 ]  

  1. 6.   then 
  2. 7.     echo "input error!" 
  3. 8.     exit 1  

9. fi  

  1. 10.  

11. flag=0  

12. exec < /etc/hosts  

13. while read line  

14. do  

  1. 15.  if [ "$1" = "`echo $line|awk '{print $1}'`" ]  
  2. 16.    then 
  3. 17.        flag=1  
  4. 18.        echo "the $1 's hostname is `echo $line|awk '{print $2}'`"   
  5. 19.        break;  
  6. 20.  fi  

21. done   

22. [ $flag -eq 0 ] && echo " sorrry,not find $1 's hostname!" 

  1. 23.  

提示:此题,请大家学习while的用法及设置flag的思路。

执行结果:
[root@old_boy scripts]# sh oldboy.sh 192.168.1.11
the 192.168.1.11 's hostname is oldboy11.etiantian.org
[root@old_boy scripts]# sh oldboy.sh 192.168.1.21
the 192.168.1.21 's hostname is oldboy21.etiantian.org
[root@old_boy scripts]# sh oldboy.sh 192.168.1.311
 sorrry,not find 192.168.1.311 's hostname!
----------------我是每种方法分隔符---------------
 

特别提示:下面的方法中,我老师大量的使用了awk的不同方法来实现同样的功能,来告诉大家,awk是很强大的, 希望同学们能按照老师的教学要求精通之。

法3)awk精确匹配:
准备:
[root@old_boy scripts]# tail -4 /etc/hosts
192.168.1.11  oldboy11.etiantian.org
192.168.1.111  oldboy111.etiantian.org
192.168.1.21  oldboy21.etiantian.org
192.168.1.31  oldboy31.etiantian.org
脚本:

1. [root@old_boy scripts]# cat awkhost1.sh   

2. awk 'BEGIN {a="'$1'"} {if($1==a) print $2; }' /etc/hosts  

执行结果:
[root@old_boy scripts]# sh awkhost1.sh 192.168.1.21
oldboy21.etiantian.org
[root@old_boy scripts]# sh awkhost1.sh 192.168.1.31
oldboy31.etiantian.org
[root@old_boy scripts]# sh awkhost1.sh 192.168.1.11
oldboy11.etiantian.org
提示:注意a="'$1'"的用法,$1为命令行传参。awk程序中调用系统变量的方法a="'$1'"。
----------------我是每种方法分隔符---------------
法4)awk精确匹配法

1. [root@old_boy scripts]# cat awkhost2.sh   

2. awk '{if($1=="'$1'") print $2}' /etc/hosts  

执行结果:
[root@old_boy scripts]# awkhost2.sh 192.168.1.11
oldboy11.etiantian.org
[root@old_boy scripts]# awkhost2.sh 192.168.1.21
oldboy21.etiantian.org
[root@old_boy scripts]# awkhost2.sh 192.168.1.311
----------------我是每种方法分隔符---------------
法5)awk过滤法

1. [root@old_boy scripts]# cat awkhost4.sh   

2. awk '/'"${1} "'/''{print $2}' /etc/hosts  

3. 执行结果:  

4. [root@old_boy scripts]# awkhost4.sh 192.168.1.21  

5. oldboy21.etiantian.org  

6. [root@old_boy scripts]# awkhost4.sh 192.168.1.11  

7. oldboy11.etiantian.org  

8. [root@old_boy scripts]# awkhost4.sh 192.168.1.31  

9. oldboy31.etiantian.org  

10. 提示:除了语法外,这道题有个学问,就是过滤时传参结尾要带个空格,这样才能过滤重复IP的情况  

11. 如:  

  1. 12.  192.168.1.11  oldboy11.etiantian.org  
  2. 13.  192.168.1.111  oldboy111.etiantian.org 

----------------我是每种方法分隔符---------------
法6)awk过滤法

1. [root@old_boy scripts]# cat awkhost5.sh   

2. awk '{if($1~/'$1'/) print $2}'  /etc/hosts ##如果文件第一列包含命令行第一个参数字符则打印第二列  

3. 执行结果:  

4. [root@old_boy scripts]# awkhost5.sh 192.168.1.31  

5. oldboy31.etiantian.org  

6. [root@old_boy scripts]# awkhost5.sh 192.168.1.11  

7. oldboy11.etiantian.org  

8. oldboy111.etiantian.org ------>这里有bug了。  

9. [root@old_boy scripts]# awkhost5.sh 192.168.1.21  

10. oldboy21.etiantian.org  

11. 改进下来排除bug:  

12. [root@old_boy scripts]# cat awkhost5-1.sh   

13. awk '{if($1~/'$1' /) print $2}'  /etc/hosts ==>用上面加空格的思路不对。  

14. [root@old_boy scripts]# cat awkhost5-1.sh   

15. awk '{if($1~/'$1'$/) print $2}'  /etc/hosts #增加一个正则表达式$  

16. 执行结果:  

17. [root@old_boy scripts]# awkhost5-1.sh 192.168.1.21  

18. oldboy21.etiantian.org  

19. [root@old_boy scripts]# awkhost5-1.sh 192.168.1.11  

20. oldboy11.etiantian.org  

21. [root@old_boy scripts]# awkhost5-1.sh 192.168.1.31  

22. oldboy31.etiantian.org 

----------------我是每种方法分隔符---------------
法7)awk -v精确匹配法
 

1. 命令行测试:  

2. [root@old_boy scripts]# awk -v p=192.168.1.21 '$1 == p{print $2}' /etc/hosts  

3. oldboy21.etiantian.org  

4. [root@old_boy scripts]# awk -v p=192.168.1.11 '$1 == p{print $2}' /etc/hosts  

5. oldboy11.etiantian.org  

6. [root@old_boy scripts]# awk -v p=192.168.1.11 '$1 == p {print $2}' /etc/hosts  

7. oldboy11.etiantian.org  

8. 实际脚本: 

9. [root@old_boy scripts]# cat awkhost6.sh   

10. #!/bin/bash  

11. #p=$1  

12. #awk -v p="$p" '$1 == p{print $2}' /etc/hosts  

13. awk -v p="$1" '$1 == p{print $2}' /etc/hosts 

执行结果:
[root@old_boy scripts]# sh  awkhost6.sh  192.168.1.11
oldboy11.etiantian.org
[root@old_boy scripts]# sh  awkhost6.sh  192.168.1.21
oldboy21.etiantian.org
提示:
1)传参非awk程序,因此写法p="$1"
2)man awk
       -v var=val
       --assign var=val
              Assign the value val to the variable var, before execution of the program begins.   Such  vari-
              able values are available to the BEGIN block of an AWK program.
----------------我是每种方法分隔符---------------
法8:精确匹配简单的写法

1. [root@old_boy scripts]# cat awkhost9.sh   

2. awk  '$1 == "'$1'" {print $2}' /etc/hosts  

3. 执行结果:  

4. [root@old_boy scripts]# sh awkhost9.sh  192.168.1.11  

5. oldboy11.etiantian.org  

6. [root@old_boy scripts]# sh awkhost9.sh  192.168.1.21  

7. oldboy21.etiantian.org  

8. [root@old_boy scripts]# sh awkhost9.sh  192.168.1.31  

9. oldboy31.etiantian.org  

10. 特别提示:这里我老师大量的使用了awk的不同方法来实现同样的功能,很强大吧,  

11. 希望同学们能按照老师的教学要求精通之。 

----------------我是每种方法分隔符---------------
法9:学生的一个不成熟的实现法

1. #!/bin/bash  

2. b=/$PWD/wang.txt  

3. echo -n "plase input ip : " 

4. read a  

5. if [ $a == "192.168.1.11" ]  

  1. 6.         then 

7. cat $b | grep $a | awk -F ' ' '{print $2}' 

  1. 8.  

9. elif [ $a  == "192.168.1.21" ]   

  1. 10.         then 

11. cat $b | grep $a | awk -F ' ' '{print $2}' 

  1. 12.  

13. elif [ $a  == "192.168.1.31" ]  

  1. 14.         then 

15. cat $b | grep $a | awk -F ' ' '{print $2}' 

  1. 16.         else 

17. echo "plase input the correct IP address " && exit 1  

18. fi  

19. 提示:大家看看问题在哪?脚本不通用。  

  1. 20.  

----------我老师改进后 

1. #!/bin/bash  

2. #author oldboy  

3. #qq 31333741  

4. hosts_file="$PWD/oldboy.txt" 

5. #judge file  

6. [ ! -f $hosts_file ] && echo "no test file!" && exit 1  

7. echo -n "plase input ip : " 

8. read ip  

9. #judge ip format  

10. [ "${#a}" -lt 8 ] && [ "`echo $ip|sed 's/[0-9]//g'`" != "..." ] && \  

11. echo "Plase input the correct IP address" && exit 1  

  1. 12.  

13. #start  

14. result1=$(grep "$ip" $hosts_file|awk '{print $1}')  

15. if [ "$ip" == "$result1" ]  

  1. 16.   then   
  2. 17.         grep "$ip" $hosts_file|awk '{print $2}' 
  3. 18.         exit 0  

19. else 

  1. 20.         echo  "Not find the hostname of $ip" 
  2. 21.         exit 1  

22. fi  

23. 提示:此法不可取,画蛇添足了。  

posted on 2012-06-14 19:47  6.6  阅读(236)  评论(0)    收藏  举报