LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 740|回复: 5

检查IP地址的脚本

[复制链接]
发表于 2004-12-27 13:06:28 | 显示全部楼层 |阅读模式
  1. #!/bin/sh
  2. #
  3. # legal IP address judgement.
  4. #       version 0.2.0
  5. #        2004-12-28
  6. #  

  7. if [ "$1" = '-h' -o "$1" = ''  ]
  8. then
  9. echo -e "\n  Usage: $0 IP_address\nExample: $0 192.168.1.1\n"
  10. exit 0
  11. fi

  12. illegalip(){
  13. echo $myip is Illegal IP address!
  14. exit 1
  15. }
  16. legalip(){
  17. if [ "$testip" -ge 0 -a "$testip" -le 255 ]
  18. then
  19. :
  20. else
  21. illegalip
  22. fi
  23. }

  24. myip=$1

  25. # if the string is not combin by "0-9" and "."; it is not a legal IP address.
  26. testip=$(echo $myip|sed -e 's/[0-9.]//g')
  27. if [ "$testip" != "" ]
  28. then illegalip
  29. fi

  30. # if the string is not format as x.x.x.x; it is not a legal IP address.
  31. testip=$(echo $myip|sed -n '/^[0-9]\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)[0-9]$/!p')
  32. if [ "$testip" != "" ]
  33. then illegalip
  34. fi

  35. # the ip x.x.x.x; if each x is not  between 0-255, it is illegal IP address.
  36. for ((i=1;i<5;i++))
  37. do
  38. testip=$(echo $myip|sed -e "s/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\\$i/")
  39. legalip
  40. done

  41. echo $myip is legal IP address!
复制代码


ps: 换回gnome了
如果有什么好的建议,请多多指教 :thank
发表于 2004-12-27 22:22:06 | 显示全部楼层
支持~~
指出一些小问题^-^
1,程序不够友好,当只给出一个程序名时,什么也没有提示。建议此时给出-h选项一样的输出
2,sed:-e 表达式 #1,字符 61:Invalid regular expression  --------这是我得到的输出,why??
 楼主| 发表于 2004-12-27 23:05:12 | 显示全部楼层
:thank
这样啊
可能是版本的问题:
$ sed --version
GNU sed version 4.1.2

$ echo $BASH_VERSION
3.00.0(1)-release
字符61在哪儿
现在应该可以啦,哈
发表于 2004-12-27 23:08:38 | 显示全部楼层
bash3.00的环境写这的这样,有些遗憾!  :p
 楼主| 发表于 2004-12-27 23:12:43 | 显示全部楼层
是不是还有的改进?:thank
 楼主| 发表于 2004-12-28 23:47:45 | 显示全部楼层
Post by tcpip
bash3.00的环境写这的这样,有些遗憾!  :p

:2cool
确实,直接用blultin只用了1/3时间,快好多;多谢指教:thank
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表