4
%% Copyright Ericsson AB 2003-2009. All Rights Reserved.
4
%% Copyright Ericsson AB 2003-2010. All Rights Reserved.
6
6
%% The contents of this file are subject to the Erlang Public License,
7
7
%% Version 1.1, (the "License"); you may not use this file except in
8
8
%% compliance with the License. You should have received a copy of the
9
9
%% Erlang Public License along with this software. If not, it can be
10
10
%% retrieved online at http://www.erlang.org/.
12
12
%% Software distributed under the License is distributed on an "AS IS"
13
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
14
%% the License for the specific language governing rights and limitations
15
15
%% under the License.
22
22
%%% <p>Use this module to set up telnet connections, send commands and
23
23
%%% perform string matching on the result.
24
%%% (See the <code>unix_telnet</code> manual page for information
25
%%% about how ct_telnet may be used specifically with unix hosts.)</p>
24
%%% See the <c>unix_telnet</c> manual page for information about how to use
25
%%% ct_telnet, and configure connections, specifically for unix hosts.</p>
26
26
%%% <p>The following default values are defined in ct_telnet:</p>
28
28
%%% Connection timeout = 10 sec (time to wait for connection)
29
29
%%% Command timeout = 10 sec (time to wait for a command to return)
30
30
%%% Max no of reconnection attempts = 3
31
31
%%% Reconnection interval = 5 sek (time to wait in between reconnection attempts)
32
%%% Keep alive = true (will send NOP to the server every 10 sec if connection is idle)</pre>
33
33
%%% <p>These parameters can be altered by the user with the following
34
34
%%% configuration term:</p>
36
36
%%% {telnet_settings, [{connect_timeout,Millisec},
37
37
%%% {command_timeout,Millisec},
38
38
%%% {reconnection_attempts,N},
39
%%% {reconnection_interval,Millisec}]}.
39
%%% {reconnection_interval,Millisec},
40
%%% {keep_alive,Bool}]}.</pre>
41
41
%%% <p><code>Millisec = integer(), N = integer()</code></p>
42
42
%%% <p>Enter the <code>telnet_settings</code> term in a configuration
43
43
%%% file included in the test and ct_telnet will retrieve the information
44
%%% automatically.</p>
44
%%% automatically. Note that <c>keep_alive</c> may be specified per connection if
45
%%% required. See <c>unix_telnet</c> for details.</p></doc>
46
47
%%% @type connection_type() = telnet | ts1 | ts2
150
153
%%% name can only be closed with the handle value.</p>
152
155
%%% <p><code>TargetMod</code> is a module which exports the functions
153
%%% <code>connect(Ip,Port,Extra)</code> and <code>get_prompt_regexp()</code>
156
%%% <code>connect(Ip,Port,KeepAlive,Extra)</code> and <code>get_prompt_regexp()</code>
154
157
%%% for the given <code>TargetType</code> (e.g. <code>unix_telnet</code>).</p>
155
158
open(KeyOrName,ConnType,TargetMod,Extra) ->
156
159
case ct:get_config({KeyOrName,ConnType}) of
176
case ct:get_config({KeyOrName,keep_alive}) of
178
case ct:get_config({telnet_settings,keep_alive}) of
172
184
log(heading(open,{KeyOrName,ConnType}),"Opening connection to: ~p",[Addr1]),
173
185
ct_gen_conn:start(KeyOrName,full_addr(Addr1,ConnType),
174
{TargetMod,Extra},?MODULE)
186
{TargetMod,KeepAlive,Extra},?MODULE)
177
189
%%%-----------------------------------------------------------------
373
385
%%%=================================================================
374
386
%%% Callback functions
376
init(Name,{Ip,Port,Type},{TargetMod,Extra}) ->
388
init(Name,{Ip,Port,Type},{TargetMod,KeepAlive,Extra}) ->
377
389
S0 = case ct:get_config(telnet_settings) of
381
393
set_telnet_defaults(Settings,#state{})
383
case catch TargetMod:connect(Ip,Port,S0#state.conn_to,Extra) of
395
case catch TargetMod:connect(Ip,Port,S0#state.conn_to,KeepAlive,Extra) of
385
397
log(heading(init,{Name,Type}),
386
398
"Opened telnet connection\n"
389
401
"Command timeout: ~p\n"
390
402
"Reconnection attempts: ~p\n"
391
403
"Reconnection interval: ~p\n"
392
"Connection timeout: ~p",
404
"Connection timeout: ~p\n"
393
406
[Ip,Port,S0#state.com_to,S0#state.reconns,
394
S0#state.reconn_int,S0#state.conn_to]),
407
S0#state.reconn_int,S0#state.conn_to,KeepAlive]),
395
408
{ok,TelnPid,S0#state{teln_pid=TelnPid,
397
410
name={Name,Type},
398
411
target_mod=TargetMod,
412
keep_alive=KeepAlive,
400
414
prx=TargetMod:get_prompt_regexp()}};
401
415
{'EXIT',Reason} ->
415
429
set_telnet_defaults(Ss,S#state{reconns=Rs});
416
430
set_telnet_defaults([{reconnection_interval,RInt}|Ss],S) ->
417
431
set_telnet_defaults(Ss,S#state{reconn_int=RInt});
432
set_telnet_defaults([{keep_alive,_}|Ss],S) ->
433
set_telnet_defaults(Ss,S);
434
set_telnet_defaults([Unknown|Ss],S) ->
435
log(heading(set_telnet_defaults,{telnet_settings,Unknown}),
436
"Bad element in telnet_settings: ~p",[Unknown]),
437
set_telnet_defaults(Ss,S);
418
438
set_telnet_defaults([],S) ->
527
547
reconnect({Ip,Port,_Type},State) ->
528
548
reconnect(Ip,Port,State#state.reconns,State).
529
549
reconnect(Ip,Port,N,State=#state{target_mod=TargetMod,
550
keep_alive=KeepAlive,
532
553
reconn_int=ReconnInt}) ->
533
case TargetMod:connect(Ip,Port,ConnTo,Extra) of
554
case TargetMod:connect(Ip,Port,ConnTo,KeepAlive,Extra) of
535
556
{ok, NewPid, State#state{teln_pid=NewPid}};
536
557
Error when N==0 ->