LinuxSir.cn,穿越时空的Linuxsir!

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

如果检测系统是否已经连接到 internet

[复制链接]
发表于 2008-11-17 17:25:35 | 显示全部楼层 |阅读模式
我的想法是这样的,把 ping 的结果输出到的 /tmp/xx$$,然后抓 /tmp/xx$$ 中的字符串分析……
我把网线拨了,然后等啊等啊,等啊等啊……竟然卡在`ping google.com`一直没有反应……

请问弟兄们一般是怎么检测的?
  1. # paths to programs
  2. ECHO=/bin/echo
  3. IFCONFIG=/sbin/ifconfig
  4. PING=/bin/ping
  5. # defaults
  6. ME=`basename $0`
  7. # make sure run by root
  8. if [ "`/usr/bin/id -u`" != 0 ] ; then
  9.     $ECHO "$ME: You must be root to run this script" >& 2
  10.     exit 1
  11. fi
  12. # test connecting network
  13. if [ -f $PING ] ; then
  14.     $ECHO "test connecting network..."
  15.     TMP_FILE=/tmp/$ME-$$
  16.     $PING -c 3 google.com > $TMP_FILE
  17. fi
复制代码
发表于 2008-11-17 18:47:34 | 显示全部楼层
或许有一个对你有用,有问题先看看 man
  1.        -w deadline
  2.               Specify a timeout, in seconds, before ping exits regardless of how many packets  have
  3.               been  sent  or received. In this case ping does not stop after count packet are sent,
  4.               it waits either for deadline expire or until count probes are answered  or  for  some
  5.               error notification from network.
  6.        -W timeout
  7.               Time  to  wait for a response, in seconds. The option affects only timeout in absense
  8.               of any responses, otherwise ping waits for two RTTs.
复制代码
回复 支持 反对

使用道具 举报

发表于 2008-11-17 21:02:19 | 显示全部楼层
if !( ping -c 3  -W 2 google.com  )
then
    echo "Couldn't ping."
else
    echo "ing successful!";
fi
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-11-17 21:48:57 | 显示全部楼层
非常谢谢两位!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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