实例:bash 脚本测试 httpd 可以使用哪些端口


环境: RHEL6

随手写的一个脚本,留着以后参考bash的语法。
循环1024-20000,逐个端口测试能不能被 httpd listen.

#!/bin/bash

> /tmp/httpd_port_test.log

for ((i=1024; i<=20000; i++))
do
echo "" >> /tmp/httpd_port_test.log
echo "Checking $i" >> /tmp/httpd_port_test.log
/etc/init.d/httpd stop
sed -i "s/^Listen [0-9]*$/Listen $i/g" /etc/httpd/conf/httpd.conf
/etc/init.d/httpd start
if [ "$?" -ne 0 ]
then
echo "ERROR: port $i failed."
echo "ERROR: port $i failed." >> /tmp/httpd_port_test.log
else
echo "Success: port $i" >> /tmp/httpd_port_test.log
fi
done

端口范围跟 selinux 有关。

 ~]# semanage port -l | grep -w http_port_t
 http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
 ~]# semanage port -l | grep -w 8080
http_cache_port_t              tcp      3128, 8080, 8118, 8123, 10001-10010