new(options) public

Creates a new Net::Telnet object.

Attempts to connect to the host (unless the Proxy option is provided: see below). If a block is provided, it is yielded status messages on the attempt to connect to the server, of the form:

Trying localhost...
Connected to localhost.

options is a hash of options. The following example lists all options and their default values.

host = Net::Telnet::new(
         "Host"       => "localhost",  # default: "localhost"
         "Port"       => 23,           # default: 23
         "Binmode"    => false,        # default: false
         "Output_log" => "output_log", # default: nil (no output)
         "Dump_log"   => "dump_log",   # default: nil (no output)
         "Prompt"     => /[$%#>] \z/n, # default: /[$%#>] \z/n
         "Telnetmode" => true,         # default: true
         "Timeout"    => 10,           # default: 10
           # if ignore timeout then set "Timeout" to false.
         "Waittime"   => 0,            # default: 0
         "Proxy"      => proxy         # default: nil
                         # proxy is Net::Telnet or IO object
       )

The options have the following meanings:

Host

the hostname or IP address of the host to connect to, as a String. Defaults to “localhost”.

Port

the port to connect to. Defaults to 23.

Binmode

if false (the default), newline substitution is performed. Outgoing LF is converted to CRLF, and incoming CRLF is converted to LF. If true, this substitution is not performed. This value can also be set with the #binmode() method. The outgoing conversion only applies to the #puts() and #print() methods, not the #write() method. The precise nature of the newline conversion is also affected by the telnet options SGA and BIN.

Output_log

the name of the file to write connection status messages and all received traffic to. In the case of a proper Telnet session, this will include the client input as echoed by the host; otherwise, it only includes server responses. Output is appended verbatim to this file. By default, no output log is kept.

Dump_log

as for Output_log, except that output is written in hexdump format (16 bytes per line as hex pairs, followed by their printable equivalent), with connection status messages preceded by ‘#’, sent traffic preceded by ‘>’, and received traffic preceded by ‘<’. By default, not dump log is kept.

Prompt

a regular expression matching the host’s command-line prompt sequence. This is needed by the Telnet class to determine when the output from a command has finished and the host is ready to receive a new command. By default, this regular expression is /[$%#>] z/n.

Telnetmode

a boolean value, true by default. In telnet mode, traffic received from the host is parsed for special command sequences, and these sequences are escaped in outgoing traffic sent using #puts() or #print() (but not #write()). If you are using the Net::Telnet object to connect to a non-telnet service (such as SMTP or POP), this should be set to “false” to prevent undesired data corruption. This value can also be set by the #telnetmode() method.

Timeout

the number of seconds to wait before timing out both the initial attempt to connect to host (in this constructor), and all attempts to read data from the host (in #waitfor(), #cmd(), and #login()). Exceeding this timeout causes a TimeoutError to be raised. The default value is 10 seconds. You can disable the timeout by setting this value to false. In this case, the connect attempt will eventually timeout on the underlying connect(2) socket call with an Errno::ETIMEDOUT error (but generally only after a few minutes), but other attempts to read data from the host will hand indefinitely if no data is forthcoming.

Waittime

the amount of time to wait after seeing what looks like a prompt (that is, received data that matches the Prompt option regular expression) to see if more data arrives. If more data does arrive in this time, Net::Telnet assumes that what it saw was not really a prompt. This is to try to avoid false matches, but it can also lead to missing real prompts (if, for instance, a background process writes to the terminal soon after the prompt is displayed). By default, set to 0, meaning not to wait for more data.

Proxy

a proxy object to used instead of opening a direct connection to the host. Must be either another Net::Telnet object or an IO object. If it is another Net::Telnet object, this instance will use that one’s socket for communication. If an IO object, it is used directly for communication. Any other kind of object will cause an error to be raised.

Show source
Register or log in to add new notes.