bash 开发的一个例子
最近项目迁移,有很多个服务器,需要开通端口。为了检测某个服务器端口开通情况,于是写了一个shell脚本。
某台服务器A访问服务器B的 22 80 8080 端口是否通,将返回结果 发送到C服务,由C服务处理,汇总,并展示
具体逻辑如下
1.C服务期提供查询服务,给定ip返回需要探测的ip和端口。
2.从返回的字符串中获取到ip和端口
3 .nc -zvw 5 ${IP} ${PORT}
使用命令检测
4 . ping -c 3 ${IP}
5 .将结果拼接好返回给C
#!/bin/bash
path=`pwd`
cd ${path}
url=http://49.232.50.87:7777/tel/net
ip=199.99.9.0
curl -s -F "s=$ip" $url > iplist.txt
#解决末尾有一%的问题
awk '{print $0}' iplist.txt > iplist2.txt
iplisttext=`cat iplist2.txt`
echo "完整的字符串====>\n${iplisttext}"
OLD_IFS="$IFS"
IFS="|"
arr=($iplisttext)
IFS="$OLD_IFS"
for s in ${arr[@]}
do
OLD_IFS="$IFS"
IFS="_"
ip_port=($s)
ip=${ip_port[0]}
port=${ip_port[1]}
echo "$ip $port"
# result=`nmap ${ip} -p${port}|grep open|wc -l`
# echo "${ip} ${port} ${result}" >> telresult.txt
nc -zvw 5 ${IP} ${PORT} 2>/dev/null
res=$?
ping -c 3 ${IP}
res2=$?
done
text=`cat iplist2.txt`
curl -s -F "s=$text" $url
echo "=====over===="