我需要Linux Shell脚本的帮助,该脚本应自动检查主机是否可访问。最好每3-5秒ping端口22。如果端口可访问,程序应执行命令服务HelloWorld stop。如果主机不可访问,则脚本应在计算机上自动执行命令,例如服务HelloWorld启动。
有人知道如何实现吗?
我有类似的东西,但是不能正常工作,
#!/bin/bash
IP='192.168.1.1'
fping -c1 -t300 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
service helloworld stop
else
service helloworld start
fi
答案 0 :(得分:1)
尝试以下代码
public class MyLabel : Label
{
static MyLabel()
{
ContentProperty.OverrideMetadata(typeof(MyLabel),
new FrameworkPropertyMetadata(
new PropertyChangedCallback(OnContentChanged)));
}
private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyLabel lbl = d as MyLabel;
if (lbl.ContentChanged != null)
{
DependencyPropertyChangedEventArgs args = new DependencyPropertyChangedEventArgs( ContentProperty, e.OldValue, e.NewValue);
lbl.ContentChanged(lbl, args);
}
}
public event DependencyPropertyChangedEventHandler ContentChanged;
}
通过这种方式,可以检查特定端口的IP是否可达