思想就是整个网段ping一遍,对于ping不通的,解析其失败的字符来判定
#!/bin/bash head_add=${1} address=${head_add%.*} echo address=$address for i in {1..255} do ping -c 2 $address.$i |grep -iq "tty" && (echo IP addr 192.168.0.$i ok;echo 192.168.0.$i >>./ok_ip) || (echo IP addr 192.168.0.$i bad;echo 192.168.0.$i >> ./bad_ip)
done
首先用到了字符串的截取
${expression}下面写用到的一种方法
使用 % 号操作符。用途是从右边开始删除第一次出现子字符串即其右边字符,保留左边字符。用法为%substr*,例如: str='http://www.你的域名.com/cut-string.html' echo ${str%/*} 得到的结果为http://www.你的域名.com,即删除从右边开始到第一个"/"及其右边所有字符
更多可以参考
https://www.cnblogs.com/onmyway20xx/p/4308113.html
接下来 ping -c 2,就是ping两次,ping不同的话会显示 unreach的字样
然后grep分析是否含有目标字符串
grep -q用于if逻辑判断很好用,如果有匹配的内容则立即返回状态值0,否则返回非0
接下来按逻辑输出即可,找到返回1,即输出&&紧跟命令,否则返回||后的
{
command
}&
这是多线程的写法,后跟wait表示所有线程执行完毕后,继续下面的动作
nmap命令也可用于扫描网段命令