# connect

Connects.

    connect <command line parameters>

## Remarks

If MACRO is not linked to Tera Term, this command runs Tera Term with <command line parameters>, and links it to MACRO.

If MACRO has already been linked to Tera Term and Tera Term is not connected to the host, this command causes Tera Term to connect to the host specified by <command line parameters>. See "Tera Term command line" and "TTSSH command line" for the format of <command line parameters>.

If MACRO has already been linked to Tera Term and Tera Term has already been connected to the host, this command is ignored.

As a result of this command, the system variable "result" is set to one of the following values depending on the link and connection status:

Value | Status
--- | ---
0 | Link to Tera Term has not been made.
1 | Connection to the host has not been made, but link to Tera Term has been made.
2 | Both connection and link have been made.

To test the current link and connect status before executing the "connect" command, use the "testlink" command.

Communication commands except "connect", "cygconnect" and "testlink" can not be executed before the link is established.

## Connection method

There are 3 types of connection you can establish from Tera Term macro:

- Telnet connection (port 23)
- SSH1 or SSH2 connection (port 22)
- Connection via COM port

### Telnet connections

    connect 'myserver:23 /nossh /T=1'
    connect 'myserver:23'

Using /nossh is strongly recommended. Without this parameter Tera Term will start connecting with the same method (telnet or SSH) that was used during last time when teraterm.ini file was saved. In case it was SSH than your macro will try to connect via SSH and will eventually fail.

Using /T=1 is recommended. Without this parameter Tera Term will start connecting with the value of 'Telnet' in teraterm.ini file. When the Telnet value is off, Tera Term may not work well.

### SSH connections

    connect 'myserver /ssh'

This way will start SSH connection without defining whether SSH1 or SSH2 has to be used.

    connect 'myserver /ssh /1'
    connect 'myserver /ssh /2'

Parameters /1 and /2 force to use SSH1 or SSH2 method.

    connect 'myserver /ssh /auth=password /user=username /passwd=password'
    connect 'myserver /ssh /1 /auth=password /user=username /passwd=password'
    connect 'myserver /ssh /2 /auth=password /user=username /passwd=password'
    connect 'myserver /ssh /auth=publickey /user=username /passwd=password /keyfile=private-key-file'
    connect 'myserver /ssh /1 /auth=publickey /user=username /passwd=password /keyfile=private-key-file'
    connect 'myserver /ssh /2 /auth=publickey /user=username /passwd=password /keyfile=private-key-file'
    connect 'myserver /ssh /auth=challenge /user=username /passwd=password'
    connect 'myserver /ssh /1 /auth=challenge /user=username /passwd=password'
    connect 'myserver /ssh /2 /auth=challenge /user=username /passwd=password'

Above 9 ways allow to skip popup dialog and pass username and password directly from macro. Please note that /auth=password or /auth=publickey or /auth=challenge is the parameter saying that authentication will be done by entering password or private-key-file thus you should not replace the word 'password' or the word 'publickey' in it with actual password.

    connect 'myserver /ssh /auth=pageant /user=username'
    connect 'myserver /ssh /1 /auth=pageant /user=username'
    connect 'myserver /ssh /2 /auth=pageant /user=username'

Above 3 ways allow to skip popup dialog and pass username directly from macro.

    connect 'myserver /ssh /auth=password /user=username /ask4passwd'
    connect 'myserver /ssh /1 /auth=password /user=username /ask4passwd'
    connect 'myserver /ssh /2 /auth=password /user=username /ask4passwd'
    connect 'myserver /ssh /auth=publickey /user=username /ask4passwd /keyfile=private-key-file'
    connect 'myserver /ssh /1 /auth=publickey /user=username /ask4passwd /keyfile=private-key-file'
    connect 'myserver /ssh /2 /auth=publickey /user=username /ask4passwd /keyfile=private-key-file'
    connect 'myserver /ssh /auth=challenge /user=username /ask4passwd'
    connect 'myserver /ssh /1 /auth=challenge /user=username /ask4passwd'
    connect 'myserver /ssh /2 /auth=challenge /user=username /ask4passwd'

Above 9 ways pass username and authentication method directly from macro, and the popup asks for password.

### Connections via COM port

    connect '/C=x'

Here x represents COM port number. For example to connect via COM port 1 the command will look like: connect '/C=1'

source URL: http://logmett.com/forum/viewtopic.php?t=28

## Examples

    ; No command line parameter
    connect ''

    ; Timeout will occur if connection does not succeed within five seconds.
    connect 'myserver /timeout=5'

    ; Run Tera Term and open telnet session
    connect 'myserver:23 /nossh'

    ; Run Tera Term and open SSH session
    connect 'myserver /ssh'

    ; Run Tera Term and open SSH1 session
    connect 'myserver /ssh /1'

    ; Run Tera Term and open SSH2 session
    connect 'myserver /ssh /2'

    ; Run Tera Term,open SSH session and do not prompt for user name and password
    connect 'myserver /ssh /auth=password /user=username /passwd=password'

    ; Run Tera Term, open SSH1 session and do not prompt for user name and password
    connect 'myserver /ssh /1 /auth=password /user=username /passwd=password'

    ; Run Tera Term, open SSH2 session and do not prompt for user name and password
    connect 'myserver /ssh /2 /auth=password /user=username /passwd=password'

    ; Run Tera Term, open SSH session and, not prompt for user name and password and use private key file
    connect 'myserver /ssh /auth=publickey/user=username /passwd=password /keyfile=private-key-file'

    ; Run Tera Term, open SSH1 session and, not prompt for user name and password and use private key file
    connect 'myserver /ssh /1 /auth=publickey/user=username /passwd=password /keyfile=private-key-file'

    ; Run Tera Term, open SSH2 session and, not prompt for user name and password and use private key file
    connect 'myserver /ssh /2 /auth=publickey /user=username /passwd=password /keyfile=private-key-file'

    ; Tera Term automatically connects the remote host with the public key
    ; authentication, and not prompt for user name and password and use private key file.
    connect 'myserver /ssh /auth=pageant /user=username'

    ; Run Tera Term and open serial port connection. Port number is x=1..Maximum value(MaxComPort).
    connect '/C=x'

    CommandLine = '111.111.11.11'
    connect CommandLine

    ; Sample macro for Tera Term
    ;  Telnet login

    ; open Tera Term
    ;   command line = 'TERATERM myhost.mydomain' (telnet)
    connect 'myhost.mydomain'

    ; set username
    UsernamePrompt = 'Username:'
    Username = 'MYNAME'
    PasswordPrompt = 'Password:'
    ; get password from file
    getpassword 'password.dat' 'mypassword' Password

    ; login
    wait   UsernamePrompt
    sendln Username

    wait   PasswordPrompt
    sendln Password

    ; OK, auto login complete.

    ; sample macro of Tera Term
    ;
    ; File: ssh2login.ttl
    ; Description: auto login with SSH2 protocol
    ; Environment: generic

    username = 'nike'
    hostname = '192.168.1.3'

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    msg = 'Enter password for user '
    strconcat msg username
    passwordbox msg 'Get password'

    msg = hostname
    strconcat msg ':22 /ssh /auth=password /user='
    strconcat msg username
    strconcat msg ' /passwd='
    strconcat msg inputstr

    connect msg

    [test.bat]
    ttpmacro.exe paramtest.ttl "192.168.3.2"
    ttpmacro.exe paramtest.ttl "192.168.3.2" cal
    ttpmacro.exe paramtest.ttl "192.168.3.2" uptime
    ttpmacro.exe paramtest.ttl "192.168.3.2" "man ls" echo foo
    ttpmacro.exe paramtest.ttl "192.168.3.2" "uname -a"
    ttpmacro.exe paramtest.ttl "192.168.3.2" "cat /proc/cpuinfo"
    pause

    [paramtest.ttl]
    if paramcnt != 3 then
    	messagebox "Argument error" "ERROR"
    	end
    endif

    prompt = 'yutaka@sai(~) '
    sprintf2 var "%s:22 /2 /ssh /auth=password /user=yutaka /passwd=yutaka" param2
    messagebox var "test"
    connect var
    wait prompt

    sendln param3
    wait prompt

    pause 3

    sendln 'exit'
    pause 3

## Note

1. In these examples parameters shown with Italic font have to be replaced with their actual values.
2. A user's password can include space characters. If the password includes the space character, a user should put the password in quotations by using `"' character. Also, if the password includes the " character, a user should write the " two times.
   NOTICE: A user can not use the "@" character instead of the space character from Tera Term 4.63.
3. Please remember that entering actual username and password in Tera Term macro will cause them to be stored as an open text and it is your responsibility to keep such macro is secure location.
4. When a user re-connect the remote host soon after disconnecting the server, a user could not connect the server. For more detail, refer to "here".

## See also

- closett
- cygconnect
- disconnect
- testlink
- unlink
- NOTICE: Immediately re-connect the server after disconnection
- Variables