2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-02-01 07:19:24 +00:00

FRITZBOX: New attribute "pwdFile". Robuster telnet login

git-svn-id: https://svn.fhem.de/fhem/trunk@7179 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
tpoitzsch 2014-12-09 21:38:30 +00:00
parent 89097c84b7
commit bdd53eb650

View File

@ -137,6 +137,7 @@ sub FRITZBOX_Initialize($)
."defaultUploadDir "
."fritzBoxIP "
."INTERVAL "
."pwdFile "
."ringWithIntern:0,1,2 "
."telnetUser "
.$readingFnAttributes;
@ -433,15 +434,11 @@ FRITZBOX_Readout_Run($)
my $returnStr = "$name|";
if ($hash->{REMOTE})
{
$result = FRITZBOX_Telnet_Open( $hash );
if ($result)
if ($result = FRITZBOX_Open_Connection( $hash ) )
{
$returnStr .= "Error|".$result;
return $returnStr;
}
}
if ($slowRun == 1)
{
@ -638,8 +635,7 @@ FRITZBOX_Readout_Run($)
$returnStr .= "|readoutTime|";
$returnStr .= sprintf "%.2f", time()-$startTime;
FRITZBOX_Telnet_Close ( $hash )
if $hash->{REMOTE};
FRITZBOX_Close_Connection ( $hash );
return $returnStr
@ -915,11 +911,10 @@ FRITZBOX_Ring_Run($)
$msg =~ s/^msg:\s*//;
$msg = substr($msg, 0, 30);
}
if ($hash->{REMOTE})
{
$result = FRITZBOX_Telnet_Open( $hash );
return "$name|0|$result" if $result;
}
$result = FRITZBOX_Open_Connection( $hash );
return "$name|0|$result"
if $result;
#Preparing 1st command array
@cmdArray = ();
@ -963,8 +958,7 @@ FRITZBOX_Ring_Run($)
}
FRITZBOX_Exec( $hash, \@cmdArray );
FRITZBOX_Telnet_Close( $hash )
if ($hash->{REMOTE});
FRITZBOX_Close_Connection( $hash );
return $name."|1|Ringing done";
}
@ -1090,16 +1084,21 @@ FRITZBOX_ConvertRingTone ($@)
# Opens a Telnet Connection to an external FritzBox
############################################
sub FRITZBOX_Telnet_Open($)
sub FRITZBOX_Open_Connection($)
{
my ($hash) = @_;
my $name = $hash->{NAME};
return undef
unless $hash->{REMOTE} == 1;
my $host = AttrVal( $name, "fritzBoxIP", "fritz.box" );
my $pwdFile = "fb_pwd.txt";
my $pwdFile = AttrVal( $name, "pwdFile", "fb_pwd.txt");
my $pwd;
my $msg;
my $before;
my $match;
FRITZBOX_Log $hash, 5, "Open password file '$pwdFile' to extract password";
if (open(IN, "<" . $pwdFile)) {
@ -1123,37 +1122,50 @@ sub FRITZBOX_Telnet_Open($)
return $msg;
}
if ( $user ne "" )
FRITZBOX_Log $hash, 5, "Wait for user or password prompt.";
unless ( ($before,$match) = $telnet->waitfor('/(user|password): $/i') )
{
FRITZBOX_Log $hash, 5, "Telnet: Wait for user prompt";
unless ($telnet->waitfor( '/user: $/i' ))
{
$msg = "Error while waiting for user prompt: ".$telnet->errmsg;
$msg = "Telnet error while waiting for user or password prompt: ".$telnet->errmsg;
FRITZBOX_Log $hash, 2, $msg;
$telnet->close;
$telnet = undef;
return $msg;
}
FRITZBOX_Log $hash, 5, "Telnet: Entering user";
$telnet->print( $user );
if ( $match eq "user: " && $user eq "")
{
$msg = "Telnet login requires user name but attribute 'telnetUser' not defined";
FRITZBOX_Log $hash, 2, $msg;
$telnet->close;
$telnet = undef;
return $msg;
}
elsif ( $match eq "user: ")
{
FRITZBOX_Log $hash, 5, "Entering user name";
$telnet->print( $user );
FRITZBOX_Log $hash, 5, "Telnet: Wait for password prompt";
FRITZBOX_Log $hash, 5, "Wait for password prompt";
unless ($telnet->waitfor( '/password: $/i' ))
{
$msg = "Error while waiting for password prompt: ".$telnet->errmsg;
$msg = "Telnet error while waiting for password prompt: ".$telnet->errmsg;
FRITZBOX_Log $hash, 2, $msg;
$telnet->close;
$telnet = undef;
return $msg;
}
FRITZBOX_Log $hash, 5, "Telnet: Entering password";
}
elsif ( $match eq "password: " && $user ne "")
{
FRITZBOX_Log $hash, 3, "Attribute 'telnetUser' defined but telnet login did not prompt for user name.";
}
FRITZBOX_Log $hash, 5, "Entering password";
$telnet->print( $pwd );
FRITZBOX_Log $hash, 5, "Telnet: Wait for command prompt";
FRITZBOX_Log $hash, 5, "Wait for command prompt";
unless ($telnet->waitfor( '/# $/i' ))
{
$msg = "Error while waiting for command prompt: ".$telnet->errmsg;
$msg = "Telnet error while waiting for command prompt: ".$telnet->errmsg;
FRITZBOX_Log $hash, 2, $msg;
$telnet->close;
$telnet = undef;
@ -1161,14 +1173,18 @@ sub FRITZBOX_Telnet_Open($)
}
return undef;
} # end FRITZBOX_Telnet_Open
} # end FRITZBOX_Open_Connection
# Closes a Telnet Connection to an external FritzBox
############################################
sub FRITZBOX_Telnet_Close($)
sub FRITZBOX_Close_Connection($)
{
my ($hash) = @_;
return undef
unless $hash->{REMOTE} == 1;
if (defined $telnet)
{
FRITZBOX_Log $hash, 4, "Close Telnet connection";
@ -1179,7 +1195,7 @@ sub FRITZBOX_Telnet_Close($)
{
FRITZBOX_Log $hash, 1, "Cannot close an undefined Telnet connection";
}
} # end FRITZBOX_Telnet_Close
} # end FRITZBOX_Close_Connection
# Executed the command on the FritzBox Shell
############################################
@ -1193,11 +1209,11 @@ sub FRITZBOX_Exec($$)
unless (defined $telnet)
{
return undef
if (FRITZBOX_Telnet_Open($hash));
if (FRITZBOX_Open_Connection($hash));
$openedTelnet = 1;
}
my $retVal = FRITZBOX_Exec_Remote($hash, $cmd);
FRITZBOX_Telnet_Close ( $hash ) if $openedTelnet;
FRITZBOX_Close_Connection ( $hash ) if $openedTelnet;
return $retVal;
}
else
@ -1508,6 +1524,12 @@ sub FRITZBOX_fritztris($)
<br>
IP address or URL of the Fritz!Box for remote telnet access. Default is "fritz.box".
</li><br>
<li><code>pwdFile &lt;fileName&gt;</code>
<br>
File that contains the password for telnet access. Default is 'fb_pwd.txt' in the root directory of FHEM.
<br>
If the Fritz!Box is configured differently, the user name has to be defined with this attribute.
</li><br>
<li><code>telnetUser &lt;user name&gt;</code>
<br>
User name that is used for telnet access. By default no user name is required to login.