null

精灵城市

芮之家归来

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

linux shell 查看在使用的IP地址(实际操作步骤) ]

 

简单的例子:look_actived_ip.sh

#!/bin/bash IP=192.168.0 for i in `seq 224 226`         #此处的符号“ ` ”是“~”键的,如果是 "  键的 ' ,seq不起作用

# seq:用于产生从某个数到另外一个数之间的所有整数,seq的例子在后面给出 #for i in 192.168.1.{1,254} do   ping -c 1 $IP.$i >  /dev/null 2>&1   if [ `echo $?` -eq 0 ];then   echo -n "$IP.$i is online"   else   echo -n "$IP.$i is offline" fi done

#------------------------ 以下内容为思维扩散内容 -----------------------------------------------------

扩展shell:将actived ip 输出到文本/var/local/actived_ip.txt中保存,每次运行look_actived_ip.sh前先清空actived_ip.txt的内容,将新的结果写入该文本,并统计有多少个IP是actived

[root@chinesefonts test]# vi look_actived_ip.sh

#!/bin/bash :>actived_ip.txt                #清空文本actived_ip.txt的所有内容; : 是一个占位符,不会产生任何输入 IP=192.168.0 for i in `seq 1 10` #for i in 192.168.1.{1,254}       #上面两行相当于这一行,但如果使用此行,下面就不需要使用$IP变量 do   ping -c 1 $IP.$i >  /dev/null 2>&1     # /dev/null   if [ `echo $?` -eq 0 ];then                 # -eq:不等于   echo "$IP.$i" >> actived_ip.txt      # >> 表示追加输出到文本中   else   echo -n "           $IP.$i is offline " fi done

sort /var/local/test/actived_ip.txt | wc -l      # wc -l:统计有多少行

#-----------------------以下内容为思维扩散内容,不用写在上面的shell脚本中-------------------------------------------------

sort /var/local/test/actived_ip.txt | uniq           #uniq:去除重复的行

sort /var/local/test/actived_ip.txt | uniq | wc -l         #去除重复行后统计有多少条记录

#===================== seq 例子============================================

#------------------------代码--------------------------------

#!/bin/bash # Example for seq for i in `seq 1 10`; do echo $i; done

#-----------------------运行结果----------------------------------------

[root@chinesefonts test]# ./seq.sh 1 2 3 4 5 6 7 8 9 10

posted on 2013-05-26 22:03  bigboo  阅读(1214)  评论(0编辑  收藏  举报
First link Second link Third link