(原创)自动扫描局域网并且发现指定端口开放的主机ip的shell脚本
脚本XMLBashJ#经过2天研究,学习shell和awk脚本,得到如下功能:
1。扫描局域网主机的端口开放情况
2。获取指定端口开发的主机ip
3。将获取的ip修改有关配置文件
代码如下,有疑问的可以找我探讨,一起学习!
#!/bin/bash
echo '
Start to delete the old configuration file......'
rm -rf scan_result.xml
rm -rf scan_result_ip.xml
rm -rf scan_result_ip.xml.bak
for ipv in {195..197}
do
echo '
Start to detect the status of port 9035 for host '$ipv'......'
nmap -v 10.0.2.$ipv -p 9035 --append-output -oN scan_result.xml
done
#cho 'Start to find the ip of the host which's 9035 port is open......'
awk 'BEGIN{RS="Interesting ports on bogon"; FS="("; OFS=")"}{for(j=1;j<=NF;j++){if($j~/open/){print $0;break}}}' scan_result.xml > scan_result_ip.xml
echo '
The ip of the host whose 9035 port is open have been found......'
sleep 3
sed 's/(//g' scan_result_ip.xml > scan_result_ip.xml.bak
sed 's/)://g' scan_result_ip.xml.bak > scan_result_ip.xml
head -1 scan_result_ip.xml > scan_result_ip.xml.bak
var=`cat scan_result_ip.xml.bak|sed -e "s/ //g"`
sleep 3
echo '
Start to configure for the edge server......'