IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Perl Discussion :

comment expliquer cet script?


Sujet :

Langage Perl

  1. #1
    Membre du Club
    Inscrit en
    Avril 2005
    Messages
    127
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 127
    Points : 46
    Points
    46
    Par défaut comment expliquer cet script?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    #!/usr/bin/perl -w
    #
    # If called as wake.pl -f file it reads lines of the form
    #
    # aa:bb:cc:dd:ee;ff 12.34.56.78 or
    # aa:bb:cc:dd:ee:ff foo.bar.com
    # aa:bb:cc:dd:ee:ff
    #
    # which are MAC addresses and hostnames of NICs to send a wakeup packet.
    # In the first two cases, unicast is used (and root permission may be
    # required if the ARP cache needs to be injected with a mapping).
    # In the third case, broadcast is used, and anybody can run the command.
    # Comments in the file start with #.
    #
    # Or MAC addresses can be specified on the command line
    #
    # wake.pl aa.bb.cc.dd.ee.ff
    #
    # Or both can be used:
    #
    # wake.pl -f addresses.cfg 11:22:33:44:55:66
    #
    # This program may have to be run with superuser privilege because it
    # may need to inject an ARP entry into the cache.
    # Be careful, you could corrupt valid entries if those NICs are
    # already active.
    #
    # Perl version by ken.yap@acm.org after DOS/Windows C version posted by
    # Steve_Marfisi@3com.com on the Netboot mailing list
    # Released under GNU Public License, 2000-01-05
    #
    use Getopt::Std;
    use Socket;
     
    getopt('f:', \%opts);
    if (exists($opts{'f'})) {
    	unless (open(F, $opts{'f'})) {
    		print "open: $opts{'f'}: $!\n";
    	} else {
    		while (<F>) {
    			next if /^\s*#/;	# skip comments
    			($mac, $ip) = split;
    			next if !defined($mac) or $mac eq '';
    			if (!defined($ip) or $ip eq '') {
    				&send_broadcast_packet($mac);
    			} else {
    				&send_wakeup_packet($mac, $ip);
    			}
    		}
    		close(F);
    	}
    }
    while (@ARGV) {
    	send_broadcast_packet(shift(@ARGV));
    }
     
    sub send_broadcast_packet {
    	($mac) = @_;
     
    	if ($mac !~ /[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}/i)  {
    		print "Malformed MAC address $mac\n";
    		return;
    	}
    	print "Sending wakeup packet to MAC address $mac\n";
    	# Remove colons
    	$mac =~ tr/://d;
    	# Magic packet is 6 bytes of FF followed by the MAC address 16 times
    	$magic = ("\xff" x 6) . (pack('H12', $mac) x 16);
    	# Create socket
    	socket(S, PF_INET, SOCK_DGRAM, getprotobyname('udp'))
    		or die "socket: $!\n";
    	# Enable broadcast
    	setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1)
    		or die "setsockopt: $!\n";
    	# Send the wakeup packet
    	defined(send(S, $magic, 0, sockaddr_in(0x2fff, INADDR_BROADCAST)))
    		or print "send: $!\n";
    	close(S);
    }
     
    sub send_wakeup_packet {
    	($mac, $ip) = @_;
     
    	if (!defined($iaddr = inet_aton($ip))) {
    		print "Cannot resolve $ip\n";
    		return;
    	}
    	if ($mac !~ /[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}/i)  {
    		print "Malformed MAC address $mac\n";
    		return;
    	}
    	# Inject entry into ARP table, in case it's not there already
    	system("arp -s $ip $mac") == 0
    		or print "Warning: arp command failed, you need to be root\n";
    	print "Sending wakeup packet to $ip at MAC address $mac\n";
    	# Remove colons
    	$mac =~ tr/://d;
    	# Magic packet is 6 bytes of FF followed by the MAC address 16 times
    	$magic = ("\xff" x 6) . (pack('H12', $mac) x 16);
    	# Create socket
    	socket(S, PF_INET, SOCK_DGRAM, getprotobyname('udp'))
    		or die "socket: $!\n";
    	# Send the wakeup packet
    	defined(send(S, $magic, 0, sockaddr_in(0x2fff, $iaddr)))
    		or print "send: $!\n";
    	close(S);
    }
    je dois le documenter, mais je ne comprends pas ce qu'il fait,
    si quelqu'un sait m'aider, je serai reconnaissant.
    mario

  2. #2
    Expert éminent
    Avatar de Jedai
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2003
    Messages
    6 245
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Avril 2003
    Messages : 6 245
    Points : 8 586
    Points
    8 586
    Par défaut
    Tu dois le documenter ? Mais il est complètement commenté et son usage est parfaitement expliqué dans le commentaire au début !
    A mon avis ton problème est plutôt que tu ne comprends pas ce qu'il est censé faire, tu ne sais pas ce qu'est un wakeup packet par exemple, je me trompe (si oui, soit plus précis sur ce qui te pose problème) ? Dans ce cas il s'agit plutôt d'une question de réseau, et tu devrais plutôt aller voir sur ce forum.

    --
    Jedaï

Discussions similaires

  1. comment executer un script perl svp?
    Par nico72 dans le forum Langage
    Réponses: 8
    Dernier message: 13/07/2005, 09h51
  2. [Poweramc 9.5] comment generé le script ?
    Par mictif dans le forum PowerAMC
    Réponses: 2
    Dernier message: 20/06/2005, 12h44
  3. Réponses: 1
    Dernier message: 23/02/2005, 11h23
  4. [DEBUTANT]Comment executer un script sql
    Par peaceinpal dans le forum PostgreSQL
    Réponses: 3
    Dernier message: 15/03/2004, 08h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo