Tcl
Tcl
REST through SSH Tunnel
See more REST Examples
Demonstrates how to connect through an SSH Tunnel (via port-forwarding) to make REST API calls.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set tunnel [new_CkSocket]
set sshHostname "sftp.example.com"
set sshPort 22
# Connect to an SSH server and establish the SSH tunnel:
set success [CkSocket_SshOpenTunnel $tunnel $sshHostname $sshPort]
if {$success == 0} then {
puts [CkSocket_lastErrorText $tunnel]
delete_CkSocket $tunnel
exit
}
# Authenticate with the SSH server via a login/password
# or with a public key.
# This example demonstrates SSH password authentication.
set success [CkSocket_SshAuthenticatePw $tunnel "mySshLogin" "mySshPassword"]
if {$success == 0} then {
puts [CkSocket_lastErrorText $tunnel]
delete_CkSocket $tunnel
exit
}
# OK, the SSH tunnel is setup. Now open a channel within the tunnel.
# (Any number of channels may be created from the same SSH tunnel.
# Multiple channels may coexist at the same time.)
# This example connects to a REST server through the SSH tunnel.
# It will connect to the Amazon AWS service for this example.
set rest [new_CkRest]
set bTls 1
set port 443
set maxWaitMs 5000
# This returns a socket object that is a single channel within the SSH tunnel.
set channel [new_CkSocket]
set success [CkSocket_SshNewChannel $tunnel "s3.amazonaws.com" $port $bTls $maxWaitMs $channel]
if {$success == 0} then {
puts [CkSocket_lastErrorText $tunnel]
delete_CkSocket $tunnel
delete_CkRest $rest
delete_CkSocket $channel
exit
}
# Use the connection. (This connection is a TLS running on an SSH channel through an SSH tunnel.
# In other words, TLS is wrapped within the SSH tunnel.)
set success [CkRest_UseConnection $rest $channel 1]
if {$success != 1} then {
puts [CkRest_lastErrorText $rest]
delete_CkSocket $tunnel
delete_CkRest $rest
delete_CkSocket $channel
exit
}
# Provide AWS credentials for the REST call.
set authAws [new_CkAuthAws]
CkAuthAws_put_AccessKey $authAws "AWS_ACCESS_KEY"
CkAuthAws_put_SecretKey $authAws "AWS_SECRET_KEY"
CkAuthAws_put_ServiceName $authAws "s3"
set success [CkRest_SetAuthAws $rest $authAws]
# List all buckets for the account...
set responseXml [CkRest_fullRequestNoBody $rest "GET" "/"]
if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
puts [CkRest_lastErrorText $rest]
delete_CkSocket $tunnel
delete_CkRest $rest
delete_CkSocket $channel
delete_CkAuthAws $authAws
exit
}
set xml [new_CkXml]
set success [CkXml_LoadXml $xml $responseXml]
# Show the full XML returned.
puts [CkXml_getXml $xml]
# Iterate over the buckets, showing each bucket name.
set success [CkXml_FindChild2 $xml "Buckets"]
if {[CkXml_FirstChild2 $xml] == 1} then {
puts [CkXml_getChildContent $xml Name]
while {[CkXml_NextSibling2 $xml] == 1} {
puts [CkXml_getChildContent $xml Name]
}
}
# Move the internal pointer back to the root node.
CkXml_GetRoot2 $xml
delete_CkSocket $tunnel
delete_CkRest $rest
delete_CkSocket $channel
delete_CkAuthAws $authAws
delete_CkXml $xml