Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server (Not)   Client   Old   Guides   Examples
New in version: 6.2   6.3   6.4   6.5   7.0   7.1   7.2   7.3   7.4   7.5    Statistic  

SSH.Connect

Connects a new socket to the given IP & Port.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
SSH 6.3 Yes Yes Yes Yes Yes

MBS( "SSH.Connect"; SSH; IP { ; Port; Timeout; ProtocolPreference } )

Parameters

Parameter Description Example value
SSH The SSH session reference number from the plugin. $ssh
IP The IP to connect to. "192.168.1.123"
Port Optional
The port number to connect to.
Default is 22 which is standard port for ssh.
22
Timeout Optional
The connection timeout in seconds.
Default is 30 seconds.
30
ProtocolPreference Optional
Pass 4 to connect via IPv4.
Pass 6 to connect via IPv6.
Default is to try whatever the DNS suggests first.
4

Result

Returns OK or error.

Description

Connects a new socket to the given IP & Port.
Timeout defines timeout for connection in seconds. While waiting, the plugin yields time to other threads.

Version 6.3 now supports passing IPv4, iPv6, or a host name.

Examples

Connect to IP:

Set Variable [$r; Value:MBS( "SSH.Connect"; $ssh; "192.168.2.1" )]

Full example:

# Start a new SSH session
Set Variable [$ssh; Value:MBS( "SSH.New" )]
# Connect to an IP
Set Variable [$r; Value:MBS( "SSH.Connect"; $ssh; SSH::Host; SSH::Port )]
If [MBS("IsError")]
    Show Custom Dialog ["Connect error"; $r]
Else
    # Run the session handshake.
    Set Variable [$r; Value:MBS( "SSH.SessionHandshake"; $ssh)]
    If [MBS("IsError")]
        Show Custom Dialog ["Connect error"; $r]
    Else
        # We can show the hostkey. You can compare it to known hostkey.
        Set Field [SSH::HostKeyHash; MBS( "SSH.HostKey"; $ssh )]
        # We query supported authentication ways
        Set Variable [$authuserlist; Value:MBS( "SSH.UserAuthList"; $ssh; SSH::Username)]
        Set Field [SSH::UserAuthList; $authuserlist]
        Set Variable [$pos1; Value:Position ( $authuserlist ; "password"; 1; 1 )]
        Set Variable [$pos2; Value:Position ( $authuserlist ; "keyboard-interactive"; 1; 1 )]
        Set Variable [$pos3; Value:Position ( $authuserlist ; "publickey"; 1; 1 )]
        If [$pos1 > 0]
            # Authenticate with password
            Set Variable [$r; Value:MBS( "SSH.UserAuthPassword"; $ssh; SSH::Username; SSH::Password )]
            If [MBS("IsError")]
                Show Custom Dialog ["Connect error"; $r]
            End If
        Else If [$pos2 > 0]
            # Authenticate with answering question. Plugin passes password for answer.
            Set Variable [$r; Value:MBS( "SSH.UserAuthKeyboardInteractive"; $ssh; SSH::Username; SSH::Password )]
            If [MBS("IsError")]
                Show Custom Dialog ["Connect error"; $r]
            End If
        Else If [$pos3 > 0]
            # Authenticate with public key file
            Set Variable [$r; Value:MBS( "SSH.UserAuthPublicKey"; $ssh; SSH::Username; "your key path"; "your key path"; SSH::Password )]
            If [MBS("IsError")]
                Show Custom Dialog ["Connect error"; $r]
            End If
        End If
        If [MBS( "SSH.IsAuthenticated"; $ssh ) ≠ 1]
            Show Custom Dialog ["Authentication"; "no authenticated. Supported ways: " & $authuserlist]
        Else
            # Start a new channel
            Set Variable [$r; Value:MBS( "SSH.OpenSession"; $ssh )]
            If [MBS("IsError")]
                Show Custom Dialog ["Connect error"; $r]
            Else
                # Execute a command
                Set Variable [$r; Value:MBS( "SSH.Execute"; $ssh; SSH::Command )]
                If [MBS("IsError")]
                    Show Custom Dialog ["Connect error"; $r]
                Else
                    # We read text in several chunks until no new text is coming any more:
                    Set Variable [$text; Value:""]
                    Loop
                        Pause/Resume Script [Duration (seconds): ,1]
                        # Read some text:
                        Set Variable [$newtext; Value:MBS( "SSH.ReadText"; $ssh; 10000; "UTF8" )]
                        If [MBS("iserror") = 0]
                            If [Length($newText) > 0]
                                Set Variable [$text; Value:$text & $newText]
                            End If
                            Exit Loop If [Length($newText) = 0]
                        End If
                    End Loop
                    Set Field [SSH::Result; $text]
                End If
            End If
            # Close channel
            Set Variable [$r; Value:MBS( "SSH.Close"; $ssh )]
            # Disconnect
            Set Variable [$r; Value:MBS( "SSH.Disconnect"; $ssh )]
        End If
    End If
End If
# Release memory.
Set Variable [$r; Value:MBS( "SSH.Release"; $ssh )]

See also

Example Databases


SSH.CloseChannel   -   SSH.Disconnect

Feedback: Report problem or ask question.




Links
MBS FileMaker blog