Simple use of Perl's objective CT and XML modules

Source: Internet
Author: User

Trusted CT. PM module: Mainly used to interact with another server, such as FTP and telnet. It is also frequently used in writing monitoring data.
The method I used is very simple,
Spawn ($ command, @ Params), forks and execs $ command. returns an exact CT object upon success orundefIf the fork was unsuccessful or the command cocould not be found. spawn () passes its parameters unchanged to Perls exec (), so look there for detailed semantics. Started a new process to execute the $ command

New Round CT ()-> round CT ($ timeout, $ content), given $ timeout in seconds round CT will wait for $ object's handle to produce one of the match_patterns, which are matched exactly by default. if you want a Regexp match, prefix the pattern with '-re '. in $ timeout, content containing $ content is expected to appear. The exact match is performed by default. You can also use a regular expression, regular CT ($ timeout, Re => '')

New expect ()-> send ($ string): sends the given strings to the spawned command. Send the message

New wrong CT ()-> debug (0 | 1 | 2): print the debug information. Different numbers indicate different bug levels.

New trusted CT ()-> interact (), interacts with the user, and transfers control to the user

New reset CT ()-> soft_close (). The process is closed until the $ timeout time is reached.

New reset CT ()-> hard_close (). Close the process immediately.

New Round CT ()-> match (), returns the string matched by the last round CT () call, UNDEF if no string was matched. Return matching results

New Round CT ()-> match_number (), exp_match_number () returns the number of the pattern matched by the last round CT () call. keep in mind that the first pattern in a list of patterns is 1, not 0. returns UNDEF if no pattern was matched. returns the number of matches.

Example:
#! /Usr/bin/perl
Use reverse CT;
Use strict;

My $ timeout = 20;
My $ cmd = "Telnet ";
My $ exp = CT-> spawn ($ cmd, "192.168.0.1") or die "can't spawn $ cmd! ";
$ Exp-> expect CT ($ timeout,-Re => '[ll] ogin :');
$ Exp-> send ("test \ r \ n ");
$ Exp-> round CT ($ timeout,-Re => '[PP] assword :');
$ Exp-> debug (1 );
$ Exp-> send ("test \ r \ n ");
$ Exp-> expect CT ($ timeout,-Re => 'last login ');
$ Exp-> send ("PS-Ef | grep Java \ r \ n ");
$ Exp-> expect CT ($ timeout,-Re => 'java ');
Print $ exp-> match_number ();
$ Exp-> soft_close ();

XML: simple:The interfaces of XML and Perl can be used to read data from XML and store the data in the form of hash or hash, or to format the data into XML files through Perl. Here we mainly introduce reading structured data from XML.
XML: simple-> New ()-> xmlin (), read data from XML
XML: simple-> New ()-> xmlout (), write data to XML
Example:
#! /Usr/bin/perl-W
Use strict;
Use XML: simple;
Use Data: dumper;
My $ simple = xml: simple-> New ();
My $ DATA = $ simple-> xmlin ('pets. xml ');
# Debug
Print dumper ($ data). "\ n ";
# End
Dumper () is used to view the data structure in the memory.
In this example, pets. XML is
<? XML version = '1. 0'?>
<Pets>
<Cat>
<Name> madness </Name>
<DOB> 1 February 2004 </DOB>
<Price> 150 </price>
</CAT>
<Name> Maggie </Name>
<DOB> 12 October 1, 2005 </DOB>
<Price> 75 </price>
<Owner> Rosie </owner>
</Dog>
<Cat>
<Name> little </Name>
<DOB> 23 jun e 2006 </DOB>
<Price> 25 </price>
</CAT>
</Pets>
Results After dumpe ()
$ Var1 = {
'Cat' => {
'Little '=> {
'Dob' => '23 June 2006 ',
'Price' => '25'
},
'Madness '=> {
'Dob' => '1 February 2004 ',
'Price' => '123'
}
},
'Dog '=> {
'Owner' => 'rosi ',
'Dob' => '12 October 2005 ',
'Name' => 'maggi ',
'Price' => '75'
}
};
We can access the hash content in the form of @ {$ var1 {cat} [0]-> {DOB}. The value is "23 June 2006"

For more articles on XML and Perl interfaces, refer to this link.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.