Telnet in windows using perl


















Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 9 years, 5 months ago. Active 9 years, 5 months ago. Viewed times. When I execute the script given below in Unix, it works fine.

The good news is that this code has already been written, and that its author was kind enough to bundle it in a useful module, Net::Telnet, available at CPAN. Net::Telnet Using Net::Telnet is pretty straightforward and simple. It then retrieves and prints the output. Although the code is self explanatory, here are a few things worth of noting: The Errmode option in the constructor for the telnet object specifies what kind of behaviour we want the object to have when it encounters an error.

The other option is return. This will cause the method that caused the error to return a false value. The method waitfor takes a regular expression as an argument and tries to match it in the stream that is transmitted from the remote host.

The telnet client collects everything the server sends to the client in a buffer. The waitfor method watches that buffer and tries to match the regex. Once the regex is matched the waitfor method returns two values.

At least when it is called in list context as we called it. The second value is the actual match. The first value is the content of the buffer up to the beginning of the match. In this case we used? This is a fixed-width look-behind. Even the part that was in our regex. Putting them in a block restricts their scope. Testing the echo The rest of test script is the main part of the testing, but we have covered almost all the aspects of the test script already.

One thing remains. In both of the cases below, first we call the print method of Net::Telnet. This simply sends its parameter to the server. The new method requires passing several parameters in form of a hash.

The most important is Host, the machine you're telnetting to. The default host is localhost. If you want to telnet to a port other than one telnet normally uses, specify this in the Port option. Error handling is done through the function whose reference is specified in the Errmode parameter.

Another important option is Prompt. When you log in or run a command, Net::Telnet uses the Prompt pattern to determine when the login or command has completed.

The default Prompt is:. If the prompt on the remote machine doesn't match the default pattern, you have to specify your own. Remember to include the slashes. Timeout lets you control how long in seconds network operations wait before they give up. The default is 10 seconds. If an error or timeout occurs in the Net::Telnet module, the default behavior is to raise an exception, which, if uncaught, prints a message to STDERR and exits. To change this, pass a subroutine reference to new in the Errmode argument.

If instead of a code subroutine, you specify the string "return" as the Errmode, methods return undef in scalar context or an empty list in list context on error, with the error message available via the errmsg method:. The login method is used to send a username and password to the remote machine. It uses the Prompt to decide when the login is complete and times out if the machine doesn't reply with a prompt:. To run a program and gather its output, use the cmd method. Pass it the string to send, and it returns the output of the command.

In list context, it returns one line per list element. In scalar context, it returns one long line.



0コメント

  • 1000 / 1000