Cygwinからssh接続に使用しているスクリプトをインターネット接続有効な時だけ動作する様に修正
IsInternetConnected.ps1
$Win32 = &{
$cscode = @"
[DllImport("Connect.dll")]
public static extern int IsInternetConnected();
// The return value 0 indicates that the user is connected to the Internet.
// The return value 1 indicates that the user is not currently connected to the Internet.
// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/get_connected/isinternetconnected
"@
return (add-type -memberDefinition $cscode -name "Win32ApiFunctions" -passthru)
}
$Connected = $Win32::IsInternetConnected()
Write-Output $Connected
exit $Connected$ cat ssh.sh
# !/bin/bash
while (/bin/true) do
result=0
output=$(powershell -F "C:\IsInternetConnected.ps1" 2>&1 > /dev/null) || result=$?
if [ ! "$result" = "0" ]; then
sleep 3
continue
fi
ssh hogehoge -hoge
sleep 3
done;autossh?ネット接続されていないのにリトライするのはCPU時間とバッテリの無駄かなと。
インターネット接続されているのに、InternetGetConnectedStateがFALSEとなる。
https://social.msdn.microsoft.com/Forums/ja-JP/4b3a965b-d829-458e-9d24-b865afaafdb7/internetgetconnectedstatefalse?forum=vcgeneralja
InternetGetConnectedState()は
1.利用可能なインターネット接続が存在するか、つまり
接続する「準備」ができているかどうか。を戻すのであって、
2.実際繋がっていることを保障するものではありません。
なぜなら、本質的には
3.通信は有効なデータが戻ってくるまで繋がっているかどうかの判定は不可能。
だからです。
とはいえ微妙な動作することも多々ある(再起動すると直ったりする)