我最近发现,Bash本机支持tcp connections as file descriptors.使用:
exec 6<>/dev/tcp/ip.addr.of.server/445
echo -e "GET / HTTP/1.0\n" >&6
cat <&6
我使用6作为文件描述符,因为0,1,2是stdin、stdout和stderr.5有时被Bash for child processes人使用,所以3,4,6,7,8和9应该是安全的.
根据下面的 comments ,在脚本中测试local server的听力:
exec 6<>/dev/tcp/127.0.0.1/445 || echo "No one is listening!"
exec 6>&- # close output connection
exec 6<&- # close input connection
要确定是否有人在监听,请try 通过环回连接.如果失败,那么端口将被关闭,或者我们不被允许访问.然后,关闭连接.
根据您的用例修改此选项,例如发送邮箱、失败时退出脚本或启动所需的服务.