使用shell检查443端口
脚本如下:
[root@lzw oldboy]# more check_443.sh
#!/bin/bash
#check_443_port
#2019-8-27
PORT=443
count=0
for i in $(cat ip_list.dat)
do
((count++))
echo "count=$count"
( sleep 1;) | telnet $i $PORT >>telnet_result.txt
done
cat telnet_result.txt | grep -B 1 \] | grep [0-9] | awk '{print $3}' | cut -d '.' -f 1,2,3,4 >> telnet_alive.txt
cat ip_list.dat telnet_alive.txt | sort | uniq -u >>telnet_die.txt
检查IP地址列表:
[root@lzw oldboy]# more ip_list.dat
220.178.124.93
220.178.124.94
220.178.124.132
220.178.124.133
运行脚本:
[root@lzw oldboy]# sh check_443.sh
count=1
Connection closed by foreign host.
count=2
telnet: connect to address 220.178.124.94: Connection refused
count=3
Connection closed by foreign host.
count=4
telnet: connect to address 220.178.124.133: Connection refused
查看脚本运行结果:
[root@lzw oldboy]# more telnet_result.txt
Trying 220.178.124.93...
Connected to 220.178.124.93.
Escape character is '^]'.
Trying 220.178.124.94...
Trying 220.178.124.132...
Connected to 220.178.124.132.
Escape character is '^]'.
Trying 220.178.124.133...
查看开通443端口的地址:
[root@lzw oldboy]# more telnet_alive.txt
220.178.124.93
220.178.124.132
查看未开通443端口的地址:
[root@lzw oldboy]# more telnet_die.txt
220.178.124.133
220.178.124.94
注释:本案例首先要检查是否安装Telnet包,Linux系统默认没有安装。本脚本主要定义本次要探测IP端口,通过for循环依次读取ip_list.dat文件中每行的IP地址,当读取第一行的IP地址220.178.124.93时,Telnet220.178.124.93地址是否开通443端口,检查结果写入telnet_result.txt文件。依次类推,读取完ip_list.dat文件中IP地址跳出循环,生成最终的telnet_result.txt文件。
接下来,只是对telnet_result.txt文本的编辑。可以通过分开执行命令理解脚本的含义:
1)过滤出符号 ] 之前的一行:
[root@lzw oldboy]# cat telnet_result.txt | grep -B 1 \]
Connected to 220.178.124.93.
Escape character is '^]'.
--
Connected to 220.178.124.132.
Escape character is '^]'.
2)过滤出包含数字的0-9行:
[root@lzw oldboy]# cat telnet_result.txt | grep -B 1 \] | grep [0-9]
Connected to 220.178.124.93.
Connected to 220.178.124.132.
3)输出第三个域的字符串,每个域以空格隔开。
[root@lzw oldboy]# cat telnet_result.txt | grep -B 1 \] | grep [0-9] | awk '{print $3}'
220.178.124.93.
220.178.124.132.
4)剪切符号.之前的数字
[root@lzw oldboy]# cat telnet_result.txt | grep -B 1 \] | grep [0-9] | awk '{print $3}' | cut -d '.' -f 1,2,3,4
220.178.124.93
220.178.124.132
5)对比ip_list.dat telnet_alive.txt 文件内容,列出不同的内容放到telnet_die.txt文件。
- 点赞
- 收藏
- 关注作者
评论(0)