Erlang -- ct_telnet

User's Guide
Reference Manual
Release Notes
PDF
Top

Common Test
Reference Manual
Version 1.13

Expand All
Contract All

Table of Contents

- common_test
  - Top of manual page
  - Module:Testcase/0
  - Module:Testcase/1
  - Module:all/0
  - Module:end_per_group/2
  - Module:end_per_suite/1
  - Module:end_per_testcase/2
  - Module:group/1
  - Module:groups/0
  - Module:init_per_group/2
  - Module:init_per_suite/1
  - Module:init_per_testcase/2
  - Module:suite/0
- ct_run
- ct
  - Top of manual page
  - abort_current_testcase/1
  - add_config/2
  - break/1
  - break/2
  - capture_get/0
  - capture_get/1
  - capture_start/0
  - capture_stop/0
  - comment/1
  - comment/2
  - continue/0
  - continue/1
  - decrypt_config_file/2
  - decrypt_config_file/3
  - encrypt_config_file/2
  - encrypt_config_file/3
  - fail/2
  - fail/1
  - get_config/1
  - get_config/2
  - get_config/3
  - get_event_mgr_ref/0
  - get_status/0
  - get_target_name/1
  - get_testspec_terms/0
  - get_testspec_terms/1
  - get_timetrap_info/0
  - get_verbosity/1
  - install/1
  - listenv/1
  - log/5
  - log/1
  - log/2
  - log/3
  - log/4
  - make_priv_dir/0
  - notify/2
  - pal/5
  - pal/1
  - pal/2
  - pal/3
  - pal/4
  - parse_table/1
  - print/5
  - print/1
  - print/2
  - print/3
  - print/4
  - reload_config/1
  - remove_config/2
  - require/2
  - require/1
  - run/2
  - run/3
  - run/1
  - run_test/1
  - run_testspec/1
  - set_verbosity/2
  - sleep/1
  - start_interactive/0
  - step/3
  - step/4
  - stop_interactive/0
  - sync_notify/2
  - testcases/2
  - timetrap/1
  - userdata/2
  - userdata/3
- ct_master
  - Top of manual page
  - abort/0
  - abort/1
  - basic_html/1
  - get_event_mgr_ref/0
  - progress/0
  - run/1
  - run/4
  - run/3
  - run_on_node/3
  - run_on_node/2
  - run_test/2
- ct_cover
  - Top of manual page
  - add_nodes/1
  - cross_cover_analyse/2
  - remove_nodes/1
- ct_ftp
  - Top of manual page
  - cd/2
  - close/1
  - delete/2
  - get/3
  - ls/2
  - open/1
  - put/3
  - recv/2
  - recv/3
  - send/2
  - send/3
  - type/2
- ct_ssh
  - Top of manual page
  - apread/4
  - apread/5
  - apwrite/4
  - apwrite/5
  - aread/3
  - aread/4
  - awrite/3
  - awrite/4
  - close/2
  - close/3
  - connect/1
  - connect/2
  - connect/3
  - del_dir/2
  - del_dir/3
  - delete/2
  - delete/3
  - disconnect/1
  - exec/4
  - exec/2
  - exec/3
  - get_file_info/2
  - get_file_info/3
  - list_dir/2
  - list_dir/3
  - make_dir/2
  - make_dir/3
  - make_symlink/3
  - make_symlink/4
  - open/3
  - open/4
  - opendir/2
  - opendir/3
  - position/3
  - position/4
  - pread/4
  - pread/5
  - pwrite/4
  - pwrite/5
  - read/3
  - read/4
  - read_file/2
  - read_file/3
  - read_file_info/2
  - read_file_info/3
  - read_link/2
  - read_link/3
  - read_link_info/2
  - read_link_info/3
  - receive_response/2
  - receive_response/3
  - receive_response/4
  - rename/3
  - rename/4
  - send/3
  - send/4
  - send/5
  - send_and_receive/3
  - send_and_receive/4
  - send_and_receive/5
  - send_and_receive/6
  - session_close/2
  - session_open/1
  - session_open/2
  - sftp_connect/1
  - subsystem/3
  - subsystem/4
  - write/3
  - write/4
  - write_file/3
  - write_file/4
  - write_file_info/3
  - write_file_info/4
- ct_netconfc
  - Top of manual page
  - action/2
  - action/3
  - close_session/1
  - close_session/2
  - copy_config/3
  - copy_config/4
  - create_subscription/1
  - create_subscription/4
  - create_subscription/6
  - create_subscription/5
  - create_subscription/3
  - create_subscription/2
  - delete_config/2
  - delete_config/3
  - edit_config/3
  - edit_config/5
  - edit_config/4
  - get/2
  - get/3
  - get_capabilities/1
  - get_capabilities/2
  - get_config/3
  - get_config/4
  - get_event_streams/3
  - get_event_streams/2
  - get_session_id/1
  - get_session_id/2
  - hello/1
  - hello/3
  - hello/2
  - kill_session/2
  - kill_session/3
  - lock/2
  - lock/3
  - only_open/2
  - only_open/1
  - open/2
  - open/1
  - send/2
  - send/3
  - send_rpc/2
  - send_rpc/3
  - unlock/2
  - unlock/3
- ct_rpc
  - Top of manual page
  - app_node/2
  - app_node/3
  - app_node/4
  - call/4
  - call/5
  - call/6
  - cast/4
  - cast/5
- ct_snmp
  - Top of manual page
  - get_next_values/3
  - get_values/3
  - load_mibs/1
  - register_agents/2
  - register_users/2
  - register_usm_users/2
  - set_info/1
  - set_values/4
  - start/2
  - start/3
  - stop/1
  - unload_mibs/1
  - unregister_agents/1
  - unregister_agents/2
  - unregister_users/1
  - unregister_users/2
  - unregister_usm_users/1
  - unregister_usm_users/2
- ct_telnet
  - Top of manual page
  - close/1
  - cmd/2
  - cmd/3
  - cmdf/3
  - cmdf/4
  - expect/2
  - expect/3
  - get_data/1
  - open/3
  - open/4
  - open/1
  - open/2
  - send/2
  - send/3
  - sendf/3
  - sendf/4
- unix_telnet
  - Top of manual page
  - connect/7
  - get_prompt_regexp/0
- ct_slave
  - Top of manual page
  - start/3
  - start/2
  - start/1
  - stop/2
  - stop/1
- ct_hooks
  - Top of manual page
  - Module:id/1
  - Module:init/2
  - Module:on_tc_fail/3
  - Module:on_tc_skip/3
  - Module:post_end_per_group/4
  - Module:post_end_per_suite/4
  - Module:post_end_per_testcase/4
  - Module:post_init_per_group/4
  - Module:post_init_per_suite/4
  - Module:post_init_per_testcase/4
  - Module:pre_end_per_group/3
  - Module:pre_end_per_suite/3
  - Module:pre_end_per_testcase/3
  - Module:pre_init_per_group/3
  - Module:pre_init_per_suite/3
  - Module:pre_init_per_testcase/3
  - Module:terminate/1
- ct_property_test
  - Top of manual page
  - init_per_suite/1
  - quickcheck/2

# ct_telnet

### MODULE

ct_telnet

### MODULE SUMMARY

Common Test specific layer on top of Telnet client ct_telnet_client.erl

### DESCRIPTION

Common Test specific layer on top of Telnet client ct_telnet_client.erl.

Use this module to set up Telnet connections, send commands, and perform string matching on the result. For information about how to use ct_telnet and configure connections, specifically for UNIX hosts, see the unix_telnet manual page.

Default values defined in ct_telnet:

- Connection timeout (time to wait for connection) = 10 seconds

- Command timeout (time to wait for a command to return) = 10 seconds

- Max number of reconnection attempts = 3

- Reconnection interval (time to wait in between reconnection attempts) = 5 seconds

- Keep alive (sends NOP to the server every 8 sec if connection is idle) = true

- Polling limit (max number of times to poll to get a remaining string terminated) = 0

- Polling interval (sleep time between polls) = 1 second

- The TCP_NODELAY option for the telnet socket is disabled (set to false) per default

These parameters can be modified by the user with the following configuration term:

     {telnet_settings, [{connect_timeout,Millisec},
                        {command_timeout,Millisec},
                        {reconnection_attempts,N},
                        {reconnection_interval,Millisec},
                        {keep_alive,Bool},
                        {poll_limit,N},
                        {poll_interval,Millisec},
                        {tcp_nodelay,Bool}]}.

Millisec = integer(), N = integer()

Enter the telnet_settings term in a configuration file included in the test and ct_telnet retrieves the information automatically.

keep_alive can be specified per connection, if necessary. For details, see unix_telnet.

### Logging

The default logging behavior of ct_telnet is to print information about performed operations, commands, and their corresponding results to the test case HTML log. The following is not printed to the HTML log: text strings sent from the Telnet server that are not explicitly received by a ct_telnet function, such as expect/3. However, ct_telnet can be configured to use a special purpose event handler, implemented in ct_conn_log_h, for logging all Telnet traffic. To use this handler, install a Common Test hook named cth_conn_log. Example (using the test suite information function):

     suite() ->
         [{ct_hooks, [{cth_conn_log, [{conn_mod(),hook_options()}]}]}].

conn_mod() is the name of the Common Test module implementing the connection protocol, that is, ct_telnet.

The cth_conn_log hook performs unformatted logging of Telnet data to a separate text file. All Telnet communication is captured and printed, including any data sent from the server. The link to this text file is located at the top of the test case HTML log.

By default, data for all Telnet connections is logged in one common file (named default), which can get messy, for example, if multiple Telnet sessions are running in parallel. Therefore a separate log file can be created for each connection. To configure this, use hook option hosts and list the names of the servers/connections to be used in the suite. The connections must be named for this to work (see ct_telnet:open/1,2,3,4).

Hook option log_type can be used to change the cth_conn_log behavior. The default value of this option is raw, which results in the behavior described above. If the value is set to html, all Telnet communication is printed to the test case HTML log instead.

All cth_conn_log hook options described can also be specified in a configuration file with configuration variable ct_conn_log.

Example:

     {ct_conn_log, [{ct_telnet,[{log_type,raw},
                                {hosts,[key_or_name()]}]}]}

Note

Hook options specified in a configuration file overwrite any hard-coded hook options in the test suite.

Logging Example:

The following ct_hooks statement causes printing of Telnet traffic to separate logs for the connections server1 and server2. Traffic for any other connections is logged in the default Telnet log.

     suite() ->
         [{ct_hooks,
           [{cth_conn_log, [{ct_telnet,[{hosts,[server1,server2]}]}]}]}].

As previously explained, this specification can also be provided by an entry like the following in a configuration file:

     {ct_conn_log, [{ct_telnet,[{hosts,[server1,server2]}]}]}.

In this case the ct_hooks statement in the test suite can look as follows:

     suite() ->
         [{ct_hooks, [{cth_conn_log, []}]}].

### Data Types

connection() = handle() | {target_name(), connection_type()} | target_name()

  For target_name(), see module ct.

connection_type() = telnet | ts1 | ts2
handle() = handle()

  Handle for a specific Telnet connection, see module ct.

prompt_regexp() = string()

  Regular expression matching all possible prompts for a specific target type. regexp must not have any groups, that is, when matching, re:run/3 (in STDLIB) must return a list with one single element.

### EXPORTS

close(Connection) -> ok | {error, Reason}

Types:

Connection = connection()
Reason = term()

Closes the Telnet connection and stops the process managing it.

A connection can be associated with a target name and/or a handle. If Connection has no associated target name, it can only be closed with the handle value (see ct_telnet:open/4).

cmd(Connection, Cmd) -> {ok, Data} | {error, Reason}

Equivalent to ct_telnet:cmd(Connection, Cmd, []).

cmd(Connection, Cmd, Opts) -> {ok, Data} | {error, Reason}

Types:

Connection = connection()
Cmd = string()
Opts = [Opt]
Opt = {timeout, timeout()} | {newline, boolean()}
Data = [string()]
Reason = term()

Sends a command through Telnet and waits for prompt.

By default, this function adds a new line to the end of the specified command. If this is not desired, use option {newline,false}. This is necessary, for example, when sending Telnet command sequences prefixed with character Interprete As Command (IAC).

Option timeout specifies how long the client must wait for prompt. If the time expires, the function returns {error,timeout}. For information about the default value for the command timeout, see the list of default values in the beginning of this module.

cmdf(Connection, CmdFormat, Args) -> {ok, Data} | {error, Reason}

Equivalent to ct_telnet:cmdf(Connection, CmdFormat, Args, []).

cmdf(Connection, CmdFormat, Args, Opts) -> {ok, Data} | {error, Reason}

Types:

Connection = connection()
CmdFormat = string()
Args = list()
Opts = [Opt]
Opt = {timeout, timeout()} | {newline, boolean()}
Data = [string()]
Reason = term()

Sends a Telnet command and waits for prompt (uses a format string and a list of arguments to build the command).

For details, see ct_telnet:cmd/3.

expect(Connection, Patterns) -> term()

Equivalent to ct_telnet:expect(Connections, Patterns, []).

expect(Connection, Patterns, Opts) -> {ok, Match} | {ok, MatchList, HaltReason} | {error, Reason}

Types:

Connection = connection()
Patterns = Pattern | [Pattern]
Pattern = string() | {Tag, string()} | prompt | {prompt, Prompt}
Prompt = string()
Tag = term()
Opts = [Opt]
Opt = {idle_timeout, IdleTimeout} | {total_timeout, TotalTimeout} | repeat | {repeat, N} | sequence | {halt, HaltPatterns} | ignore_prompt | no_prompt_check | wait_for_prompt | {wait_for_prompt, Prompt}
IdleTimeout = infinity | integer()
TotalTimeout = infinity | integer()
N = integer()
HaltPatterns = Patterns
MatchList = [Match]
Match = RxMatch | {Tag, RxMatch} | {prompt, Prompt}
RxMatch = [string()]
HaltReason = done | Match
Reason = timeout | {prompt, Prompt}

Gets data from Telnet and waits for the expected pattern.

Pattern can be a POSIX regular expression. The function returns when a pattern is successfully matched (at least one, in the case of multiple patterns).

RxMatch is a list of matched strings. It looks as follows [FullMatch, SubMatch1, SubMatch2, ...], where FullMatch is the string matched by the whole regular expression, and SubMatchN is the string that matched subexpression number N. Subexpressions are denoted with '(' ')' in the regular expression.

If a Tag is speciifed, the returned Match also includes the matched Tag. Otherwise, only RxMatch is returned.

Options:

idle_timeout

  Indicates that the function must return if the Telnet client is idle (that is, if no data is received) for more than IdleTimeout milliseconds. Default time-out is 10 seconds.

total_timeout

  Sets a time limit for the complete expect operation. After TotalTimeout milliseconds, {error,timeout} is returned. Default is infinity (that is, no time limit).

ignore_prompt | no_prompt_check

  >The function returns when a prompt is received, even if no pattern has yet been matched, and {error,{prompt,Prompt}} is returned. However, this behavior can be modified with option ignore_prompt or option no_prompt_check, which tells expect to return only when a match is found or after a time-out.

ignore_prompt

  ct_telnet ignores any prompt found. This option is useful if data sent by the server can include a pattern matching prompt regexp (as returned by TargedMod:get_prompt_regexp/0), but is not to not cause the function to return.

no_prompt_check

  ct_telnet does not search for a prompt at all. This is useful if, for example, Pattern itself matches the prompt.

wait_for_prompt

  Forces ct_telnet to wait until the prompt string is received before returning (even if a pattern has already been matched). This is equal to calling expect(Conn, Patterns++[{prompt,Prompt}], [sequence|Opts]). Notice that option idle_timeout and total_timeout can abort the operation of waiting for prompt.

repeat | repeat, N

  The pattern(s) must be matched multiple times. If N is speciified, the pattern(s) are matched N times, and the function returns HaltReason = done. This option can be interrupted by one or more HaltPatterns. MatchList is always returned, that is, a list of Match instead of only one Match. Also HaltReason is returned.

sequence

  All patterns must be matched in a sequence. A match is not concluded until all patterns are matched. This option can be interrupted by one or more HaltPatterns. MatchList is always returned, that is, a list of Match instead of only one Match. Also HaltReason is returned.

Example 1:

     expect(Connection,[{abc,"ABC"},{xyz,"XYZ"}],[sequence,{halt,[{nnn,"NNN"}]}])

First this tries to match "ABC", and then "XYZ", but if "NNN" appears, the function returns {error,{nnn,["NNN"]}}. If both "ABC" and "XYZ" are matched, the function returns {ok,[AbcMatch,XyzMatch]}.

Example 2:

     expect(Connection,[{abc,"ABC"},{xyz,"XYZ"}],[{repeat,2},{halt,[{nnn,"NNN"}]}])

This tries to match "ABC" or "XYZ" twice. If "NNN" appears, the function returns HaltReason = {nnn,["NNN"]}.

Options repeat and sequence can be combined to match a sequence multiple times.

get_data(Connection) -> {ok, Data} | {error, Reason}

Types:

Connection = connection()
Data = [string()]
Reason = term()

Gets all data received by the Telnet client since the last command was sent. Only newline-terminated strings are returned. If the last received string has not yet been terminated, the connection can be polled automatically until the string is complete.

The polling feature is controlled by the configuration values poll_limit and poll_interval and is by default disabled. This means that the function immediately returns all complete strings received and saves a remaining non-terminated string for a later get_data call.

open(Name) -> {ok, Handle} | {error, Reason}

Equivalent to ct_telnet:open(Name, telnet).

open(Name, ConnType) -> {ok, Handle} | {error, Reason}

Types:

Name = target_name()
ConnType = connection_type()
Handle = handle()
Reason = term()

Opens a Telnet connection to the specified target host.

open(KeyOrName, ConnType, TargetMod) -> {ok, Handle} | {error, Reason}

Equivalent to ct_telnet:ct_telnet:open(KeyOrName, ConnType, TargetMod, []).

open(KeyOrName, ConnType, TargetMod, Extra) -> {ok, Handle} | {error, Reason}

Types:

KeyOrName = Key | Name
Key = atom()
Name = target_name()
ConnType = connection_type()
TargetMod = atom()
Extra = term()
Handle = handle()
Reason = term()

Opens a Telnet connection to the specified target host.

The target data must exist in a configuration file. The connection can be associated with Name and/or the returned Handle. To allocate a name for the target, use one of the following alternatives:

- ct:require/2 in a test case

- A require statement in the suite information function (suite/0)

- A require statement in a test case information function

If you want the connection to be associated with Handle only (if you, for example, need to open multiple connections to a host), use Key, the configuration variable name, to specify the target. Notice that a connection without an associated target name can only be closed with the Handle value.

TargetMod is a module that exports the functions connect(Ip, Port, KeepAlive, Extra) and get_prompt_regexp() for the specified TargetType (for example, unix_telnet).

For target_name(), see module ct.

See also ct:require/2.

send(Connection, Cmd) -> ok | {error, Reason}

Equivalent to ct_telnet:send(Connection, Cmd, []).

send(Connection, Cmd, Opts) -> ok | {error, Reason}

Types:

Connection = connection()
Cmd = string()
Opts = [Opt]
Opt = {newline, boolean()}
Reason = term()

Sends a Telnet command and returns immediately.

By default, this function adds a newline to the end of the specified command. If this is not desired, option {newline,false} can be used. This is necessary, for example, when sending Telnet command sequences prefixed with character Interprete As Command (IAC).

The resulting output from the command can be read with ct_telnet:get_data/2 or ct_telnet:expect/2,3.

sendf(Connection, CmdFormat, Args) -> ok | {error, Reason}

Equivalent to ct_telnet:sendf(Connection, CmdFormat, Args, []).

sendf(Connection, CmdFormat, Args, Opts) -> ok | {error, Reason}

Types:

Connection = connection()
CmdFormat = string()
Args = list()
Opts = [Opt]
Opt = {newline, boolean()}
Reason = term()

Sends a Telnet command and returns immediately (uses a format string and a list of arguments to build the command).

### See Also

unix_telnet