Page 2 sur 3

Re: scripts Ping smartphone connecté reseau wifi

Publié : 29 oct. 2014, 14:55
par patrice
Très bizarre en effet,
Essayes avec ca en changeant mon ip par celle que tu veux pinger et celle de Domoticz aussi à changer
C'est le même script qui fonctionne chez moi et que je te remets ici pour que tu fasse un copier /coller


Code : Tout sélectionner

#!/usr/bin/perl
   use v5.14;
   use LWP::Simple;                # From CPAN
   use JSON qw( decode_json );     # From CPAN
   use Data::Dumper;               # Perl core module
   use strict;                     # Good practice
   use warnings;                   # Good practice
   use utf8;
   use feature     qw< unicode_strings >;
   # Configuration section, please update to your values
   my $domoticz = "192.168.1.81:8080";  # ip and port of your Domoticz server 
   my $domo_cmd = "http://$domoticz/json.htm?type=devices&filter=all&used=true&order=Name";

           # Array of (device idx, IP)
                     my %IP=(86=>'192.168.1.66');      # Ip 3
   my $debug=1;
   # Get the JSON url
   my $json = get( $domo_cmd );
   die "Could not get $domo_cmd!" unless defined $json;
   # Decode the entire JSON
   my $decoded = JSON->new->utf8(0)->decode( $json );
   my @results = @{ $decoded->{'result'} };
   #Put JSON switch and status in a Table
   my @tab;
   foreach my $f ( @results ) {
     if ($f->{"SwitchType"}) {
           $tab[$f->{"idx"}]=$f->{"Status"};
     }
   }
   # Now we go all over the IP to check if they are alive
   foreach my $k (keys %IP) {
           my $ip=$IP{$k};
           my $res=system("sudo ping $ip -w 3 2>&1 > /dev/null");
   #print $k." ".$res."\n";
           if (($res==0)&&($tab[$k] eq 'Off')) {
                   #If device answered to ping and device status is Off, turn it On in Domoticz
                   if ($debug) {print "$k is On\n"};
                  `curl -s "http://$domoticz/json.htm?type=command&param=switchlight&idx=$k&switchcmd=On"`;
           } elsif (($res!=0)&&($tab[$k] eq 'On')) {
                   #If device did NOT answer to ping and device status is On, turn it Off in Domoticz
                   if ($debug) {print "$k is Off\n"};
                  `curl -s "http://$domoticz/json.htm?type=command&param=switchlight&idx=$k&switchcmd=Off"`;
           } else {
                   if ($debug) {print "do nothing: $k is ".$tab[$k]."\n";}
           }
   }



Re: scripts Ping smartphone connecté reseau wifi

Publié : 29 oct. 2014, 18:03
par nickos
j'ai une erreur ligne 19

Code : Tout sélectionner

Could not get http://192.168.1.6:8080/json.htm?type=devices&filter=all&used=true&order=Name! at ./ping_by_ip.pl line 19.
toujours

Code : Tout sélectionner

die "Could not get $domo_cmd!" unless defined $json;
:evil: :twisted: :evil: :twisted:

Re: scripts Ping smartphone connecté reseau wifi

Publié : 29 oct. 2014, 20:41
par patrice
hello

la librairie json est petée

fais un

Code : Tout sélectionner

sudo apt-get install libjson-perl
il doit y avoir un message d'erreur qui s'affiche non ?

Re: scripts Ping smartphone connecté reseau wifi

Publié : 30 oct. 2014, 12:27
par nickos
salut patrice

j'ai désinstallé et reinstallé libjson-perl mais toujour la même erreur ligne 19..... :twisted:
et pas de message d'erreur. désolé

Re: scripts Ping smartphone connecté reseau wifi

Publié : 04 nov. 2014, 10:38
par patrice
hello
j'ai trouvé ça :D
http://easydomoticz.com/detection-de-p ... e-sur-ip/
alors j'ai pensé à toi

Re: scripts Ping smartphone connecté reseau wifi

Publié : 08 nov. 2014, 08:50
par AVBoX
Bonjour,

J'essaye d'utiliser le script http://easydomoticz.com/detection-de-pr ... ee-sur-ip/ mais la commande me renvoie cette erreur :

Code : Tout sélectionner

root@domoticzpi:~#  /home/pi/domoticz/scripts/checkip.py 192.168.0.3 19 10 120
08:40:26- script started.
Traceback (most recent call last):
  File "/home/pi/domoticz/scripts/checkip.py", line 64, in <module>
    lastreported = domoticzstatus()
  File "/home/pi/domoticz/scripts/checkip.py", line 43, in domoticzstatus
    json_object = json.loads(domoticzrequest(domoticzurl))
  File "/home/pi/domoticz/scripts/checkip.py", line 57, in domoticzrequest
    response = urllib2.urlopen(request)
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 401, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 424, in _open
    'unknown_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1244, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib2.URLError: <urlopen error unknown url type: a class="linkclass" href="http>
Si vous avez une idée d'où vient le problème, Merci

Re: scripts Ping smartphone connecté reseau wifi

Publié : 09 nov. 2014, 09:13
par patrice
bonjour,
L'erreur de ligne 64 vient (d'après nom expérience... ) d'un mauvais IDX
Ton idx 19 ne doit pas correspondre au switch virtuel que tu as crée


Verifies aussi que l'ip de ton domoticz est bon

Re: scripts Ping smartphone connecté reseau wifi

Publié : 09 nov. 2014, 19:54
par AVBoX
Pourtant l'IDX est bien le 19 j'vais essayer d'en crée un autre

Re: scripts Ping smartphone connecté reseau wifi

Publié : 30 déc. 2014, 08:37
par nickos
pour moi le problème était l’autorisation du script
vu sur le wiki de domoticz
http://www.domoticz.com/wiki/Domoticz_P ... using_Perl
je remet mon script

Code : Tout sélectionner

#!/usr/bin/perl
    use v5.14;
    use LWP::Simple;                # From CPAN
    use JSON qw( decode_json );     # From CPAN
    use Data::Dumper;               # Perl core module
    use strict;                     # Good practice
    use warnings;                   # Good practice
    use utf8;
    use feature     qw< unicode_strings >;
    # Configuration section, please update to your values
    my $domoticz = "192.168.5.72:8080";  # ip and port of your Domoticz server 
    my $domo_cmd = "http://$domoticz/json.htm?type=devices&filter=all&used=true&order=Name";
 
            # Array of (device idx, IP)
                      my %IP=(41=>'192.168.5.19',	# Ip 1
                              44=>'192.168.5.21',	# Ip 2
                             681=>'192.168.5.24');      # Ip 3
    my $debug=0;
    # Get the JSON url
    my $json = get( $domo_cmd );
    die "Could not get $domo_cmd!" unless defined $json;
    # Decode the entire JSON
    my $decoded = JSON->new->utf8(0)->decode( $json );
    my @results = @{ $decoded->{'result'} };
    #Put JSON switch and status in a Table
    my @tab;
    foreach my $f ( @results ) {
      if ($f->{"SwitchType"}) {
            $tab[$f->{"idx"}]=$f->{"Status"};
      }
    }
    # Now we go all over the IP to check if they are alive
    foreach my $k (keys %IP) {
            my $ip=$IP{$k};
            my $res=system("sudo ping $ip -w 3 2>&1 > /dev/null");
    #print $k." ".$res."\n";
            if (($res==0)&&($tab[$k] eq 'Off')) {
                    #If device answered to ping and device status is Off, turn it On in Domoticz
                    if ($debug) {print "$k is On\n"};
                   `curl -s "http://$domoticz/json.htm?type=command&param=switchlight&idx=$k&switchcmd=On"`;
            } elsif (($res!=0)&&($tab[$k] eq 'On')) {
                    #If device did NOT answer to ping and device status is On, turn it Off in Domoticz
                    if ($debug) {print "$k is Off\n"};
                   `curl -s "http://$domoticz/json.htm?type=command&param=switchlight&idx=$k&switchcmd=Off"`;
            } else {
                    if ($debug) {print "do nothing: $k is ".$tab[$k]."\n";}
            }
    }
donc un sudo chmod 777
dans cd /home/pi/domoticz/scripts

Code : Tout sélectionner

sudo chmod 777 ping_by_ip.pl 
test, toujour dans cd /home/pi/domoticz/scripts

Code : Tout sélectionner

./ping_by_ip.pl
et ça marche :lol:

Re: scripts Ping smartphone connecté reseau wifi

Publié : 02 janv. 2015, 12:42
par hobbe
Bonjour,

J'utilise un script lua, comme décrit sur le Wiki, mais un peu adapté pour les différents téléphones/machines du réseau.
Il faut un périphérique virtuel pour chacun, et connaître leur adresse IP, par configuration du routeur.

Code : Tout sélectionner

-- /domoticz/scripts/lua/script_time_presence.lua
-- Check availability of cell phone on network
-- Using ping on Windows: ping <ip> -n 2 -w 500>nul

commandArray = {}
time = os.date("*t")

-- On every 2 minutes, starting from 1 (1, 3, 5, ...)
if((time.min % 2) == 1) then

	local list_ip = {}
	local list_device = {}

	-- Devices to monitor
	list_ip[0] = '192.168.1.20'
	list_device[0] = 'Présence Alice'
	list_ip[1] = '192.168.1.21'
	list_device[1] = 'Présence Bob'
	list_ip[2] = '192.168.1.22'
	list_device[2] = 'Présence Charlie'

	for key, m_ip in pairs(list_ip) do
		local m_device = list_device[key]
		ping_success = os.execute('ping ' .. m_ip .. ' -n 2 -w 500>nul')
		if ping_success then
			print("¤¤¤ Ping " .. m_ip .. " (" .. m_device .. ") OK")
			if (otherdevices[m_device] == 'Off') then
				commandArray[m_device] = 'On'
			end
		else
			print("¤¤¤ Ping " .. m_ip .. " (" .. m_device .. ") failure")
			if (otherdevices[m_device] == 'On') then
				commandArray[m_device] = 'Off'
			end
		end
	end
end

return commandArray
Pas besoin de script externe et de cron, juste besoin de la commande ping (Windows, Linux).

Bon courage !