Bash:如何使telnet或nc在后台工作,直到连接关闭
下面,我将描述我正在努力解决的问题以及我目前做出的决定请告诉我更聪明/更小的决定,也很高兴收到反馈 因此,本地主机上有一个发布服务器和一个客户端,它们通过8080端口进行通信。我可以将telnet或nc连接到此端口,并正常地将输出写入日志,但不能使相同的命令在后台工作Bash:如何使telnet或nc在后台工作,直到连接关闭,bash,telnet,netcat,Bash,Telnet,Netcat,下面,我将描述我正在努力解决的问题以及我目前做出的决定请告诉我更聪明/更小的决定,也很高兴收到反馈 因此,本地主机上有一个发布服务器和一个客户端,它们通过8080端口进行通信。我可以将telnet或nc连接到此端口,并正常地将输出写入日志,但不能使相同的命令在后台工作 nohup ./telnet_expect.sh & 我看到的是,当在后台启动时,它们在获得第一个输入后立即停止(真的是这样吗?),但在前台,它们工作正常,只有在publisher关闭此端口的连接后才会停止 这是通常发生
nohup ./telnet_expect.sh &
我看到的是,当在后台启动时,它们在获得第一个输入后立即停止(真的是这样吗?),但在前台,它们工作正常,只有在publisher关闭此端口的连接后才会停止
这是通常发生的情况:
> telnet localhost 8080 | tee output.log (or >>output.log, no matter)
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Publisher开始通过端口发送信息
***some necessary output***
Publisher关闭端口
Connection closed by foreign host.
但当在后台启动时,它会立即停止,而不等待输出:
> nohup telnet localhost 8080 | tee output.log (or <command> &, or nohup <command> &)
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
>nohup telnet localhost 8080 | tee output.log(或&,或nohup&)
正在尝试127.0.0.1。。。
已连接到本地主机。
转义字符为“^]”。
连接被外部主机关闭。
这是我想到的一个expect脚本。它正在后台启动
nohup ./telnet_expect.sh &
该脚本生成新的bash会话,并执行到文件的常规重定向
通常我通过env变量安排bash和expect之间的通信,这里我没有实现它,因为整个用例足够小
#!/bin/bash
echo -e "\n$(date +%Y-%m-%d_%H:%M:%S,%3N) -- Started telnet_expect.sh\n"
logFile="/export/home/<user>/<folder>/output.log"
logFileExpect="/export/home/<user>/<folder>/expect.log"
echo > "$logFile"
echo > "$logFileExpect"
until nc -w 1 localhost 8080
do
sleep 1
done
expect -c "
log_user 1
set timeout 250
proc err_exit {msg} {
puts \"---\"
puts stderr \"\$msg\"
send \"exit status: \$?\r\"
exit 1
}
exp_internal -f /export/home/<user>/<folder>/expect.log 1
spawn bash
send \"telnet localhost 8080 >> /export/home/<user>/<folder>/output.log\r\"
sleep 10
expect {
\"*onnection closed by foreign host*\" {
send \"echo Success\r\"
}
timeout {
err_exit \"\nError: timeout\n\"
}
}
send \"exit\r\"
expect eof
"
echo -e "\n$(date +%Y-%m-%d_%H:%M:%S,%3N) -- Finished telnet_expect.sh\n"
这是我想要的剧本。它正在后台启动
nohup ./telnet_expect.sh &
该脚本生成新的bash会话,并执行到文件的常规重定向
通常我通过env变量安排bash和expect之间的通信,这里我没有实现它,因为整个用例足够小
#!/bin/bash
echo -e "\n$(date +%Y-%m-%d_%H:%M:%S,%3N) -- Started telnet_expect.sh\n"
logFile="/export/home/<user>/<folder>/output.log"
logFileExpect="/export/home/<user>/<folder>/expect.log"
echo > "$logFile"
echo > "$logFileExpect"
until nc -w 1 localhost 8080
do
sleep 1
done
expect -c "
log_user 1
set timeout 250
proc err_exit {msg} {
puts \"---\"
puts stderr \"\$msg\"
send \"exit status: \$?\r\"
exit 1
}
exp_internal -f /export/home/<user>/<folder>/expect.log 1
spawn bash
send \"telnet localhost 8080 >> /export/home/<user>/<folder>/output.log\r\"
sleep 10
expect {
\"*onnection closed by foreign host*\" {
send \"echo Success\r\"
}
timeout {
err_exit \"\nError: timeout\n\"
}
}
send \"exit\r\"
expect eof
"
echo -e "\n$(date +%Y-%m-%d_%H:%M:%S,%3N) -- Finished telnet_expect.sh\n"
您可以改用netcat命令:
nohup nc -d localhost 8080 >>console-8080.txt &
如果要将日志文件缩进日期:
nohup nc -d localhost 8080 | while read line ; do echo `date +'%d%m%Y-%H%M%S'`: $line >>console-8080.txt; done &
要在nc进程关闭时重新启动它,请执行以下操作:
#!/bin/bash
ip=$1
port=$2
while [ 1 ]
do
nc -d $ip $port | while read line ; do echo `date +'%d%m%Y-%H%M%S'`: $line >>console-$ip-$port.txt; done
sleep .1
done
您可以改用netcat命令:
nohup nc -d localhost 8080 >>console-8080.txt &
如果要将日志文件缩进日期:
nohup nc -d localhost 8080 | while read line ; do echo `date +'%d%m%Y-%H%M%S'`: $line >>console-8080.txt; done &
要在nc进程关闭时重新启动它,请执行以下操作:
#!/bin/bash
ip=$1
port=$2
while [ 1 ]
do
nc -d $ip $port | while read line ; do echo `date +'%d%m%Y-%H%M%S'`: $line >>console-$ip-$port.txt; done
sleep .1
done