Swift
Swift
Route Socket Connections through an SSH Object
See more Socket/SSL/TLS Examples
Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.
Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
let ssh = CkoSsh()!
// Connect and authenticate a standalone SSH object.
success = ssh.connect(hostname: "ssh.example.com", port: 22)
if success == false {
print("\(ssh.lastErrorText!)")
return
}
// The SSH password should come from a secure source rather than being hard-coded.
var sshPassword: String? = "mySshPassword"
success = ssh.authenticatePw(login: "sshUser", password: sshPassword)
if success == false {
print("\(ssh.lastErrorText!)")
return
}
let socket = CkoSocket()!
// Route subsequent Connect calls through the already connected and authenticated SSH object. The
// destination is reached by SSH port forwarding rather than by a direct TCP connection.
success = socket.useSsh(ssh: ssh)
if success == false {
print("\(socket.lastErrorText!)")
return
}
// Connect to the destination through the SSH tunnel.
var bTls: Bool = false
var maxWaitMs: Int = 5000
success = socket.connect(hostname: "db.internal", port: 5432, ssl: bTls, maxWaitMs: maxWaitMs)
if success == false {
print("\(socket.lastErrorText!)")
return
}
print("Connected to the destination through the SSH tunnel.")
}