[英]Checking if an SSH tunnel is up and running
I have a perl script which, when destilled a bit, looks like this: 我有一个perl脚本,当有点蒸馏时,看起来像这样:
my $randport = int(10000 + rand(1000)); # Random port as other scripts like this run at the same time
my $localip = '192.168.100.' . ($port - 4000); # Don't ask... backwards compatibility
system("ssh -NL $randport:$localip:23 root\@$ip -o ConnectTimeout=60 -i somekey &"); # create the tunnel in the background
sleep 10; # Give the tunnel some time to come up
# Create the telnet object
my $telnet = new Net::Telnet(
Timeout => 10,
Host => 'localhost',
Port => $randport,
Telnetmode => 0,
Errmode => \&fail,
);
# SNIPPED... a bunch of parsing data from $telnet
The thing is that the target $ip is on a link with very unpredictable bandwidth, so the tunnel might come up right away, it might take a while, it might not come up at all. 问题是目标$ ip是在带有非常不可预测的带宽的链路上,因此隧道可能会立即出现,可能需要一段时间,它可能根本不会出现。 So a sleep is necessary to give the tunnel some time to get up and running.
因此需要睡眠才能让隧道有一段时间起床和跑步。
So the question is: How can i test if the tunnel is up and running? 所以问题是:我如何测试隧道是否启动并运行? 10 seconds is a really undesirable delay if the tunnel comes up straight away.
如果隧道立即出现,10秒是非常不希望的延迟。 Ideally, i would like to check if it's up and continue with creating the telnet object once it is, to a maximum of, say, 30 seconds.
理想情况下,我想检查它是否已经启动并继续创建telnet对象,最多为30秒。
Edit: Ping doesn't help me mouch, as the remote end of the tunnel is generally up, but with a very high amount of packetloss 编辑: Ping不能帮助我mouch,因为隧道的远端通常是up,但是有很多的数据包损失
Solved: Extrapolating from the tip suggested by mikebabcock, sleep 10
has been replaced with this block which works like a charm: 解决:根据mikebabcock建议的提示推断,
sleep 10
已经被这个像魅力的块所取代:
my $starttime = time();
while (1)
{
# Check for success
if (system("nc -dzw10 localhost $randport > /dev/null") == 0) { last }
# Check for timeout
if (time() > $starttime + 30) { &fail() }
# 250ms delay before recheck
select (undef, undef, undef, 0.25);
}
Use netcat -- often nc
on Linux systems: 在Linux系统上使用netcat - 通常是
nc
:
nc -dvzw10 ${HOSTNAME} 23
Works for me, with a response like: 对我有用,回复如下:
Connection to ${HOSTNAME} 23 port [tcp/telnet] succeeded!
It also returns 0 on success, and is happy with a simple connection after which it goes away. 它也会在成功时返回0,并且对一个简单的连接感到满意,之后它就会消失。
You can integrate a ping to your ssh server and if it works fine the ssh tunnel is up 您可以将ping集成到您的ssh服务器,如果它正常工作,则ssh隧道已启动
# only a ping sample :-D
if ! ping -c 1 192.168.101.9
then
echo ":-("
else
echo ":-)"
fi
I think fping might be better then the usual ping, more script friendly. 我认为fping可能比通常的ping更好,脚本更友好。
fping -t 60000 [your server] fping -t 60000 [你的服务器]
should try to connect to the server 60seconds before giving up Something like 应该尝试在放弃之前60秒连接到服务器
if(fping -t 60000 [your server]) {
execute desired code;
} else {
execute this script again to rerun;;
}
I think you get the idea even if the coding isn't real. 即使编码不真实,我认为你也能得到这个想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.