Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

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.

<!--- Try open the connection --->
<cfftp action = "open"
      username = "username"
      connection = "MyConnection"
      password = "password"
      fingerprint = "12:2a:3e:c3:de:56:38:2b:e3:f6:0c:49:9a:d8:de:3d"
      server = "sftp.myserver.com"
      secure = "yes">


<!--- Output the connection status --->
<p>Successful: <cfoutput>#cfftp.succeeded#</cfoutput>
<cfdump var="#MyConnection#">

Read a directory

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

<cfftp action="listDir"
      name="RemoteDirectoryContents"
      directory="/"
      connection = "MyConnection">


<cfdump var="#RemoteDirectoryContents#">

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.

<!--- Give the page extra time when doing large operations --->
<cfsetting requesttimeout="300" />

<!--- Give the cfftp tag the same "timeout" value for doing large operations --->
<cfftp action="listDir"
      name="RemoteDirectoryContents"
      directory="/"
      timeout="300"
      connection = "MyConnection">

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.

<!--- Close the connection. --->
<cfftp action="close" connection="MyConnection"/>

Putting it all together

<!--- Give the page extra time when doing large operations --->
<cfsetting requesttimeout="300" />

<!--- Try open the connection --->
<cfftp action = "open"
      username = "username"
      connection = "MyConnection"
      password = "password"
      fingerprint = "12:2a:3e:c3:de:56:38:2b:e3:f6:0c:49:9a:d8:de:3d"
      server = "sftp.myserver.com"
      secure = "yes">


<!--- If connection established --->
<cfif cfftp.succeeded>
      <!--- Get and dump the file list of a remote directory using the created connection --->
      <cfftp action="listDir"
            name="RemoteDirectoryContents"
            directory="/"
            timeout="300"
            connection = "MyConnection">


      <cfdump var="#RemoteDirectoryContents#">

      <!--- Close the connection. --->
      <cfftp action="close" connection="MyConnection"/>
</cfif>

What Do You Think?

Reader Comments

Jake Munson's Gravatar
Jake Munson
Friday, October 25, 2013 at 10:44:45 AM EDT

Very timely post, for me! I am working on a project where I could use this. In fact, I had tried to get sftp to work, but eventually gave up. I was going to just use ftp, but now I'll try to follow your directions and keep things more secure. :)

Ian's Gravatar
Ian
Thursday, April 10, 2014 at 10:26:56 AM EDT

Hey Steven. Google has you high up for 'coldfusion 9 sftp'. Just what I was looking for. Ta mate.

Ian's Gravatar
Ian
Friday, April 11, 2014 at 10:57:34 AM EDT

Hey Steven

I built a recursive function to upload a folder and all it's contents...
https://gist.github.com/ianosullivan/10475307

Post a Comment

Comment Etiquette:

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain or appear to be advertisments, will not be published.
  • Comments that appear to be created for the purpose of linkbuilding to commercial sites will be removed.

We are all adults here so play nice.

*
*



Archives Blog Listing

Tag Listing

Learn CF In A Week

Treehouse

 
Fork me on GitHub