Bonjour,
je souhaite mettre mon plugin de nagios (connexion au nas bufallo + reboot) en perl mais j'ai bcp de mal a trouvé l'équivalent de mon code php. J'ai trouvé la fonction html:parser mais je n'arrive pas a comprendre son fonctionnement parfaitement.
plugin nas.sh
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 <?php include_once("simple_html_dom.php"); // initialisation de la session $curl = curl_init(); $post_data = "txtAuthLoginUser=login&txtAuthLoginPassword=motdepasse&gPage=top&gMode=auth&txtHelpSearch=&gPage=top&gMode=&gType=auth&gKey=&gSSS=&gRRR=&hiddenDummyText=dummy"; // configuration des options //URL a recuperer curl_setopt($curl, CURLOPT_URL,"http://XXX.XXX.XXX.XX/cgi-bin/top.cgi"); //TRUE pour que PHP fasse un HTTP POST curl_setopt($curl, CURLOPT_POST, 1); //Toutes les donnees a passer lors dune operation de HTTP POST. curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); //TRUE retourne directement le transfert sous forme de chaine de la valeur retourner par curl_exec() au lieu de lafficher directement. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // execution de la session $page = curl_exec($curl); // fermeture des ressources curl_close($curl); // / / Cree un objet DOM a partir dune URL $html = str_get_html($page); //Cherche le bouton de type hidden et lire son contenu foreach($html->find('input[type=hidden]') as $input) { $tmp_data = $input->outertext; //Recherche par expression rationnelle insensible a la casse et recuperer les valeurs du champ value if (eregi('gSSS', $tmp_data)){ $gSSS = $input->getAttribute("value"); } if (eregi('gRRR', $tmp_data)){ $gRRR = $input->getAttribute("value"); } } print $gSSS . " / " . $gRRR; // etape 2 reboot : $curl = curl_init(); $post_data = "txtHelpSearch=&gPage=maintenance&gMode=shutdown&gType=reboot&gKey=undefined&gSSS=" . $gSSS . "&gRRR=". $gRRR . "&hiddenDummyText=dummy"; curl_setopt($curl, CURLOPT_URL,"http://XXX.XXX.XXX.XX/cgi-bin/top.cgi"); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $page = curl_exec($curl); curl_close($curl); if (eregi('Restarting the LinkStation now.',$page)){ print "Good job :) "; }else{ print "Echec redemrrage"; } //print $page; ?>:
voici mon 1er code Perl:
Mais rien n'est renvoyé.
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 #!/usr/bin/perl use strict; use warnings; use HTML::Parser; my $page = "http://XX.XX.XX.XX/cgi-bin/top.cgi"; # création de mon parser my $parser = HTML::Parser->new(); # définition des mes evenements $parser->handler( text => \&text, "dtext" ); $parser->handler( start => \&start, "tagname,attr" ); $parser->handler( end => \&end, "tagname" ); $parser->parse($page); # définition des callbacks # on les rassemble dans un bloc pour que les variables lexicales # sur lesquels on les clos soient invisibles du reste du script print get_data(); { my $in_span_textegras; my @data; sub start { my ($tag, $attr) = @_; $in_span_textegras++ if $tag eq 'input' and $attr->{type} eq 'hidden'; } sub end { my ($tag) = @_; $in_span_textegras-- if $tag eq 'input' and $in_span_textegras; } # tu souhaiteras peut-être faire ton traitement directement # dans cette fonction sub text { my ($text) = @_; push @data, $text if $in_span_textegras; } # si tu fais le traitement dans text() tu n'auras pas besoin # de cette fonction sub get_data { return @data; } }
Ni avec ce deuxieme code perl
Quelqu'un pourrait me guidé un peu svp?
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 #!/usr/bin/perl -w use strict; package GetSummary; use base "HTML::Parser"; my $self = "http://XX.XX.XX.XX/cgi-bin/top.cgi"; #my $text = "Restarting the LinkStation now." my $meta_gSSS; my $meta_gRRR; my $h1_flag; my $meta_reboot; my $post_data = "txtAuthLoginUser=login&txtAuthLoginPassword=motdepasse&gPage=top&gMode=auth&txtHelpSearch=&gPage=top&gMode=&gType=auth&gKey=&gSSS=&gRRR=&hiddenDummyText=dummy"; sub start { my ($self,$post_data, $tag, $attr) = @_; if ($tag =~ /^input$/i && $attr->{'type'} =~ /^hidden$/i) { # Si on trouve <INPUT Type=hidden> $meta_gSSS = $attr->{'gSSS'}; } if ($tag =~ /^input$/i && $attr->{'type'} =~ /^hidden$/i) { # Si on trouve <INPUT Type=hidden> $meta_gRRR = $attr->{'gRRR'}; } print $meta_gRRR; my $text2 = "txtHelpSearch=&gPage=maintenance&gMode=shutdown&gType=reboot&gKey=undefined&gSSS=".$meta_gSSS."&gRRR=".$meta_gRRR."&hiddenDummyText=dummy"; } # pour le reboot sub text { my ($self,$tag2, $attr2, $text2) = @_; if ($tag2 =~ /^input$/i && $attr2->{'type'} =~ /^reboot$/i) { # Si on trouve Restarting the LinkStation now. $meta_reboot = $attr2->{'Restarting the LinkStation now.'}; } } sub end { my ($self, $tag, $origtext) = @_; # reset appropriate flag if we see </H1> or </TITLE> if ($tag =~ /^h1$/i) { $h1_flag = 0; } if ($tag =~ /^title$/i) { $h1_flag = 0; } if ($tag =~ /^title$/i) { $h1_flag = 0; } } my $p = new GetSummary; while (<>) { $p->parse($_); } $p->eof; print "Summary information: ", $meta_gSSS || $meta_gRRR || "No summary information found.", "\n";
Partager