SSH.Connect
-----------

Connects a new socket to the given IP &amp; Port.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [SSH](component_SSH.md) | [6.3](newinversion63.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

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

**MBS**( **"SSH.Connect";** /\* Connects a new socket to the given IP &amp; Port. \*/  
**$SSH**; /\* The SSH session reference number from the plugin. \*/   
**$IP**; /\* The IP to connect to.e.g. "192.168.1.123" \*/   
**$Port**; /\* Optional; The port number to connect to.  
Default is 22 which is standard port for ssh.e.g. 22 \*/   
**$TimeOut**; /\* Optional; The connection timeout in seconds for the TCP/IP connection.   
Default is 30 seconds.e.g. 30 \*/   
**$ProtocolPreference**) /\* Optional; Pass 4 to connect via IPv4.  
Pass 6 to connect via IPv6.  
Default is to try whatever the DNS suggests first.e.g. 4 \*/ 

### Parameters

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

### Result

Returns OK or error.

### Description

Connects a new socket to the given IP &amp; 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](SSHNew.md)" )\]  
\# 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](SSHSessionHandshake.md)"; $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](SSHHostKey.md)"; $ssh )\]  
 # We query supported authentication ways  
 Set Variable \[$authuserlist ; Value:MBS ( "[SSH.UserAuthList](SSHUserAuthList.md)"; $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 &gt; 0\]  
 # Authenticate with password  
 Set Variable \[$r ; Value:MBS ( "[SSH.UserAuthPassword](SSHUserAuthPassword.md)"; $ssh ; SSH::Username ; SSH::Password )\]  
 If \[MBS ("IsError")\]  
 Show Custom Dialog \["Connect error"; $r \]  
 End If  
 Else If \[$pos2 &gt; 0\]  
 # Authenticate with answering question. Plugin passes password for answer.  
 Set Variable \[$r ; Value:MBS ( "[SSH.UserAuthKeyboardInteractive](SSHUserAuthKeyboardInteractive.md)"; $ssh ; SSH::Username ; SSH::Password )\]  
 If \[MBS ("IsError")\]  
 Show Custom Dialog \["Connect error"; $r \]  
 End If  
 Else If \[$pos3 &gt; 0\]  
 # Authenticate with public key file  
 Set Variable \[$r ; Value:MBS ( "[SSH.UserAuthPublicKey](SSHUserAuthPublicKey.md)"; $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](SSHIsAuthenticated.md)"; $ssh ) ≠ 1\]  
 Show Custom Dialog \["Authentication"; "no authenticated. Supported ways: " &amp; $authuserlist \]  
 Else  
 # Start a new channel  
 Set Variable \[$r ; Value:MBS ( "[SSH.OpenSession](SSHOpenSession.md)"; $ssh )\]  
 If \[MBS ("IsError")\]  
 Show Custom Dialog \["Connect error"; $r \]  
 Else  
 # Execute a command  
 Set Variable \[$r ; Value:MBS ( "[SSH.Execute](SSHExecute.md)"; $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](SSHReadText.md)"; $ssh ; 10000; "UTF8" )\]  
 If \[MBS ("iserror") = 0\]  
 If \[Length($newText ) &gt; 0\]  
 Set Variable \[$text ; Value:$text &amp; $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](SSHDisconnect.md)"; $ssh )\]  
 End If  
 End If  
End If  
\# Release memory.   
Set Variable \[$r ; Value:MBS ( "[SSH.Release](SSHRelease.md)"; $ssh )\]  
### See also

- [SQL.Connect](SQLConnect.md)
- [SSH.Disconnect](SSHDisconnect.md)
- [SSH.Execute](SSHExecute.md)
- [SSH.IsAuthenticated](SSHIsAuthenticated.md)
- [SSH.OpenSession](SSHOpenSession.md)
- [SSH.Release](SSHRelease.md)
- [SSH.SessionHandshake](SSHSessionHandshake.md)
- [SSH.Tunnel.Run](SSHTunnelRun.md)
- [SSH.UserAuthKeyboardInteractive](SSHUserAuthKeyboardInteractive.md)
- [SSH.UserAuthPassword](SSHUserAuthPassword.md)

### Release notes

- **Version 8.3**
    - Updated code for [Socket.Connect](http://www.mbsplugins.eu/SocketConnect.shtml), [ServerSocket.SendQuery](http://www.mbsplugins.eu/ServerSocketSendQuery.shtml), [SSH.Connect](http://www.mbsplugins.eu/SSHConnect.shtml) and SendCommand to use new unified socket connection code, which uses IPv4 and IPv6, queries DNS if needed and tries several IPs if needed.
- **Version 7.2**
    - Added ProtocolPreference parameter to [SSH.Connect](http://www.mbsplugins.eu/SSHConnect.shtml) and [Socket.Connect](http://www.mbsplugins.eu/SocketConnect.shtml) to request IPv4 or IPv6.

### Example Databases

- [Network/SSH Tunnel](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/SSH%20Tunnel.shtml#1ScriptAnchor_)
- [Network/SSH](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/SSH.shtml#1ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 8.3pr3](https://www.mbsplugins.de/archive/2018-06-21/MBS_FileMaker_Plugin_version_8/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 7.2pr4](https://www.mbsplugins.de/archive/2017-04-26/MBS_FileMaker_Plugin_version_7/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 6.3 for OS X/Windows](https://www.mbsplugins.de/archive/2016-07-12/MBS_FileMaker_Plugin_63_for_OS/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 6.3pr4](https://www.mbsplugins.de/archive/2016-06-24/MBS_FileMaker_Plugin_version_6/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 29th May 2016 , last changed 4th December 2019

  
[SSH.ConfigureKeepAlive](SSHConfigureKeepAlive.md) - [SSH.Disconnect](SSHDisconnect.md)

[HTML Version](SSHConnect.shtml)