2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-19 12:46:03 +00:00

98_WOL.pm: Don't check sshHost during startup, added params ($IP, $MAC, $BC) to commandref,$BC in WOL_by_cmd (thanks Otto)

git-svn-id: https://svn.fhem.de/fhem/trunk@24101 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
KernSani 2021-03-27 20:48:39 +00:00
parent df6ab05b39
commit cd68d6ca38

View File

@ -21,6 +21,7 @@
#
#============================================================================
# Changelog:
# 2021-03-26, v1.05: Don't check sshHost during startup, added commandref Params, $BC in WOL_by_cmd (thanks Otto)
# 2020-01-08, v1.04: Minor fixes and improved error handling
# Added option to execute perl/FHEM/System command for wakeup (local or ssh)
# Added option to execute shutdown command via ssh
@ -40,7 +41,20 @@ use IO::Socket;
use Blocking;
use Time::HiRes qw(gettimeofday);
my $version = "1.04";
my $version = "1.05";
use constant {
SQ_MINIMUM_INTERVAL => 300,
LOG_CRITICAL => 0,
LOG_ERROR => 1,
LOG_WARNING => 2,
LOG_SEND => 3,
LOG_RECEIVE => 4,
LOG_DEBUG => 5,
TCPPACKETSIZE => 16384,
};
my $EMPTY = q{};
my $SPACE = q{ };
################################################################################
sub WOL_Initialize($) {
@ -50,14 +64,14 @@ sub WOL_Initialize($) {
$hash->{DefFn} = "WOL_Define";
$hash->{UndefFn} = "WOL_Undef";
$hash->{AttrFn} = "WOL_Attr";
$hash->{AttrList} =
"interval shutdownCmd:textField-long wolCmd:textField-long sysCmd:textField-long sshHostShutdown sysInterface useUdpBroadcast sshHost "
$hash->{AttrList}
= "interval shutdownCmd:textField-long wolCmd:textField-long sysCmd:textField-long sshHostShutdown sysInterface useUdpBroadcast sshHost "
. $readingFnAttributes;
}
################################################################################
sub WOL_Define($$) {
my ( $hash, $def ) = @_;
my @a = split( "[ \t][ \t]*", $def );
my @a = split( /[ \t][ \t]*/xsm, $def );
my $u = "wrong syntax: define <name> WOL <MAC_ADRESS> <IP> <mode> <repeat> ";
return $u if ( int(@a) < 4 );
@ -159,8 +173,7 @@ sub WOL_UpdateReadings($) {
my $abortFn = "WOL_PingAbort";
if ( !( exists( $hash->{helper}{RUNNING_PID} ) ) ) {
$hash->{helper}{RUNNING_PID} =
BlockingCall( $blockingFn, $arg, $finishFn, $timeout, $abortFn, $hash );
$hash->{helper}{RUNNING_PID} = BlockingCall( $blockingFn, $arg, $finishFn, $timeout, $abortFn, $hash );
$hash->{helper}{RUNNING_PID}{loglevel} = 4;
}
else {
@ -244,10 +257,10 @@ sub WOL_wake($) {
#$host = '255.255.255.255' if ( !defined $host );
$host = AttrVal( $name, "useUdpBroadcast", "" );
if ($host eq "" && $hash->{MODE} =~/UDP|BOTH/) {
my @ip = split(/\./,$hash->{IP});
if ( $host eq "" && $hash->{MODE} =~ /UDP|BOTH|CMD/ ) {
my @ip = split( /\./, $hash->{IP} );
$ip[3] = "255";
$host = join("\.",@ip);
$host = join( "\.", @ip );
Log3 $name, 1, "[$name] Guessing broadcast address: $host";
}
@ -264,15 +277,18 @@ sub WOL_wake($) {
readingsBulkUpdate( $hash, "packet_via_UDP", $host );
}
if ( $hash->{MODE} eq "CMD" ) {
WOL_by_cmd( $hash, "on" );
WOL_by_cmd( $hash, "on", $host );
}
readingsEndUpdate( $hash, defined( $hash->{LOCAL} ? 0 : 1 ) );
}
################################################################################
# method to wake/shutdown via cmd
sub WOL_by_cmd($$) {
my ( $hash, $mode ) = @_;
sub WOL_by_cmd {
my $hash = shift;
my $mode = shift;
my $bc = shift;
my $name = $hash->{NAME};
my $mac = $hash->{MAC};
my $ip = $hash->{IP};
@ -299,9 +315,15 @@ sub WOL_by_cmd($$) {
}
}
if (!$bc) {
$bc = $EMPTY;
}
#Replacements
$cmd =~ s/\$MAC/$mac/g;
$cmd =~ s/\$IP/$ip/g;
$cmd =~ s/\$BC/$bc/g;
#Execute via SSH if sshHost given
if ( $host ne "" ) {
@ -324,7 +346,8 @@ sub WOL_by_cmd($$) {
# Log3 $hash, 1, "[$name] ssh command should be one of ether-wake|etherwake|wakonlan|wake (see sysCmd attribute)!";
# return undef;
#}
$sshCmd .= " -T ".$cmd;
$sshCmd .= " -T " . $cmd;
#Enclose SSH command in double quotes
$sshCmd .= "\"";
$cmd = $sshCmd;
@ -400,7 +423,7 @@ sub WOL_by_ew($$) {
}
if ( $sysCmd eq "" ) {
Log3 $name, 1,
"[$name] no system command for WOL found - use '/usr/bin/ether-wake' or '/usr/bin/wakeonlan' or define Attribut sysCmd";
"[$name] no system command for WOL found - use '/usr/bin/ether-wake' or '/usr/bin/wakeonlan' or define Attribut sysCmd";
return undef;
}
@ -462,7 +485,8 @@ sub WOL_Attr($$$) {
}
}
if ( ( $attrName eq "sshHost" or $attrName eq "sshHostShutdown" ) and $cmd eq "set" ) {
# check if sshHost exists only at creation/change of the attribute, $init_done should prevent the check during initialization )
if ( ( $attrName eq "sshHost" or $attrName eq "sshHostShutdown" ) and $cmd eq "set" && $init_done ) {
my $cmd = "timeout 5 ssh -q $attrVal exit || echo 1";
my $res = qx ($cmd);
if ( ($res) ) {
@ -550,7 +574,7 @@ So, for example a Buffalo NAS can be kept awake.
<li><code>attr &lt;name&gt; sysCmd &lt;string&gt;</code>
<br>Custom command executed to wakeup a remote machine, i.e. <code>/usr/bin/ether-wake or /usr/bin/wakeonlan</code></li>
<li><code>attr &lt;name&gt; wolCmd &lt;command&gt;</code>
<br>Custom command executed to wakeup a remote machine. Can be &lt;command&gt;, as in at, notify oder Watchdog. If the attribute sshHost is set, the command will be executed as a shell command in remote system</li>
<br>Custom command executed to wakeup a remote machine. Can be &lt;command&gt;, as in at, notify oder Watchdog. If the attribute sshHost is set, the command will be executed as a shell command in remote system. Replacements $MAC, $IP and $BC could be used for MAC, IP and UdpBroadcast.</li>
<li><code>attr &lt;name&gt; shutdownCmd &lt;command&gt;</code>
<br>Custom command executed to shutdown a remote machine. You can use &lt;command&gt;, like you use it in at, notify or Watchdog If the attribute sshHostShutdown is set, the command will be executed as a shell command in remote system</li>
<br><br>
@ -644,7 +668,7 @@ So kann z.B. ein Buffalo NAS "wach" gehalten werden.
<li><code>attr &lt;name&gt; sysCmd &lt;string&gt;</code>
<br>Eigener Befehl, um ein entferntes Gerät aufzuwecken, z.B. <code>/usr/bin/ether-wake or /usr/bin/wakeonlan</code></li>
<li><code>attr &lt;name&gt; wolCmd &lt;command&gt;</code>
<br>Eigener Befehl, um ein entferntes Gerät aufzuwecken. Es können &lt;command&gt;, wie in at, notify oder Watchdog verwendet werden. Wenn das Attribut sshHost gesetzt ist, wird ein shell Befehl im remote System ausgeführt.
<br>Eigener Befehl, um ein entferntes Gerät aufzuwecken. Es können &lt;command&gt;, wie in at, notify oder Watchdog verwendet werden. Wenn das Attribut sshHost gesetzt ist, wird ein shell Befehl im remote System ausgeführt. Die Platzhalter $MAC, $IP und $BC koennen verwendet werden und werden durch MAC, IP und UdpBroadcast ersetzt.
</li>
<li><code>attr &lt;name&gt; shutdownCmd &lt;command&gt;</code>
<br>Eigener Befehl, um ein entferntes Gerät herunter zu fahren. Es können &lt;command&gt;, wie in at, notify oder Watchdog verwendet werden. Wenn das Attribut sshHostShutdown gesetzt ist, wird ein shell Befehl im remote System ausgeführt.