Creating and using a SFTP connection using ColdFusion

Published: {ts '2013-10-15 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/creating-and-using-a-sftp-connection-using-coldfusion/

Last week I needed to get a ColdFusion server to read from a SFTP site to list some files. I accomplished this using ColdFusion's built in <cfftp> tag since it also support sftp.

Get the server key

Before you can connect to a sftp site with the <cfftp> tag you first need to get the server fingerprint. The fingerprint is a 16 pair colon separated list of hex numbers which the server returns to identify itself.

12:2a:3e:c3:de:56:38:2b:e3:f6:0c:49:9a:d8:de:3d

The easiest way to get this is use a tool like bitvise to establish a connection to the sftp site. Once the connection is establish you can copy the fingerprint out of the bitvise console log.

Create the connection

Once you have the fingerprint and the credentials, opening the connection is a simple matter of calling the <cfftp> tag with them and specifying the secure flag.

Successful: #cfftp.succeeded#

Read a directory

While we are at it lets list the contents of a directory on the sftp site using the connection we created.

Timouts

As per Ben Nadel's blog post you need to set both the requesttimeout setting on the page and the "timeout" attribute on the cfftp tag when doing large operations.

Close the connection

Finally we must remember to close the connection we created. Note that if you just specified the credential in the listDir call instead of establishing a connection separately that this step would not be needed.

Putting it all together