coldfusion sftp

I have added my cfsftp.cfc code in a zipped file to this blog. It is available as open source so please feel free to download and use.

This allows you to transfer files to a host like the [CFFTP] tag but uses the sftp secure protocol.

Looking forward to some feedback and hoping i haven't made any silly mistakes in the code or documentation.

There are several enhancements I have in mind for a v1.1.0 :-)

Let me know what you think.

Comments
Critter's Gravatar Does it allow you to execute raw commands and get the response back?
# Posted By Critter | 4/8/06 11:45 PM
Damien's Gravatar Awesome! Someone tipped me on to this on CFTalk, I'd not seen it before.

An idea or two:
* key-based authentication
* 'mv' function to move/rename files

They've got v0.1.26 out that fixes a number of bugs, you might want to do a 1.0.1 release of your code with it bundled?

Excellent work, Michael.
# Posted By Damien | 4/14/06 5:18 PM
Russ  Michaels's Gravatar You should post it on the dev exchange, there are not many solutions about for this.
# Posted By Russ Michaels | 4/19/06 3:18 PM
michael traher's Gravatar Russ, good idea. Currently it is posted on http://www.remotesynthesis.com/cfopensourcelist&qu...;.
I'll get that sorted. Mike
# Posted By michael traher | 4/19/06 4:53 PM
Doug Jessee's Gravatar I appreciate you posting this

I have tried to use it, but am having problems getting the 'known_hosts' file onto the webserver.

We use IIS and ColdFusion.

Any help would be appreciated.

I looked for your email address, but could not find it.

Doug Jessee
# Posted By Doug Jessee | 6/13/06 1:31 PM
Michael Traher's Gravatar Hi Doug,

If you have the latest version it will now create a know_hosts file for you.

Take a look at the example.cfm file in the distribution - it shows one way for you to implement this. See below,

<cftry>
<!--- try connection --->
<cfset mysftp.connect(host="yourhost",
username="youruser",
password="yourpass",
knownhostsfile="c:\known_hosts")>
<cfcatch type="any">
<!--- if failure is unknowhostkey then add it! --->
<cfif #findnocase("unknownhostkey",cfcatch.message)#>
<cfset mysftp.addhostkey(host="yourhost",
username="youruser",
password="yourpassword",
knownhostsfile="c:\known_hosts")>

<cfoutput>Adding host key for yourhost<br/></cfoutput>
<!--- retry connection --->
<cfset mysftp.connect(host="yourhost",
username="youruser",
password="yourpassword",
knownhostsfile="c:\known_hosts")>

</cfif>
</cfcatch>
</cftry>
<!--- you should be connected by the time you get here --->

Let me know if that helps.
# Posted By Michael Traher | 6/13/06 2:25 PM
Alain Lepage's Gravatar Hi, i am currently testing the cfsftp cfc, i can send/receive files but i got a problem, the file received are all truncated to 16 kb and file sent to ssh server are 32 kb on the destination.

I wonder if anyone have an idea about this problem,

I am using cfmx 7.01
OpenSSH 3.8.1p1 as Test SSH server
My ssh server work fine with other client


Any help will be greatly appreciated,

Alain
# Posted By Alain Lepage | 8/9/06 6:42 PM
Michael Traher's Gravatar Hi Alain, I have not tested it with files of that size but I will now! I vaguely remember reading something about this on the jcraft site (they write the java library that I use). I'll do some tests tomorrow (UK time) and then report back.

Mike T
# Posted By Michael Traher | 8/10/06 12:11 AM
Tony S's Gravatar I too am having a problem transferring files larger than 16262 bytes. Theyre simply truncated if larger than that. Any info on getting past this issue would be greatly appreciated. Thanks for a cool cfc.
# Posted By Tony S | 8/14/06 4:49 PM
Bien C's Gravatar Mike, fantastic cfc. It's about time someone give CF some SFTP capabilities. Have you figured out the 16kb issue yet? I am experiencing the same thing when it comes to downloading files.
# Posted By Bien C | 8/19/06 9:54 AM
Michael Traher's Gravatar Hi Bien, Not fixed yet but I think I know what it is. I have to confess that my java is a bit weak, and the fix to this requires that I play around with the java so it will work with CF. So that is the delay. I hope to post a fix in the next two weeks.
# Posted By Michael Traher | 8/19/06 2:50 PM
Bien C's Gravatar Thanks! I'll keep my eye out for your update.
# Posted By Bien C | 8/19/06 2:58 PM
michael traher's Gravatar version 1.3.1 should fix the larger files being truncated problem. Please read the release notes carefully as I have changed the name of the jar file! Let me know if any problems!.
Thanks for your patience.
# Posted By michael traher | 8/28/06 10:55 AM
Tony S's Gravatar Thank you, this sounds good, however the link dosnt seem to be working yet...
# Posted By Tony S | 8/28/06 2:34 PM
michael traher's Gravatar oops! - finger trouble - try now
# Posted By michael traher | 8/28/06 6:25 PM
Max P.'s Gravatar Hi Michael, great job on the CF SFTP implementation! This is badly needed, and definitely appreciated.

Unfortunately I have an issue where the first use of the cfc/java object works fine, but any subsequent use just hangs up and never returns (no CF timeout either). After restarting the CF server, it works fine again, once, then hangs the next time. I'm testing with just a simple dir() call, and I do a disconnect() at the end of the session. This is on Windows 2K3 with CF 7. Seems to work for others, so I'm a bit baffled. Any ideas are apprecited.

Thanks,
-Max
# Posted By Max P. | 10/7/06 7:23 AM
Brian's Gravatar I am looking to use this tag to fulfill my Secure FTP needs. I have a security certificate that I need to pass to the SFTP host. Would this security certificate have its own passphrase to use it? I keep getting an error stating an access problem to the location of my certificate...java.io.FileNotFoundException: C:\Inetpub\components (Access is denied). Is there something that I am missing? I would really like to get this working and close some of the holes we have in coldfusion. Are there any other docs to help get me up and running? Thanks. Brian
# Posted By Brian | 10/31/06 9:28 PM
michael traher's Gravatar Hi Brian, You can either use a username and password, or you can create a ssh2 key pair (public and private keys) and yes these have a passphrase associated with them. A copy of the public key is lodged on the server and you have the private key. I will send you a separate email with further details.
Mike T
# Posted By michael traher | 10/31/06 9:58 PM
Andy Clary's Gravatar I am trying to use this component and I keep getting the error message "invalid privatekey" while trying to authenticate using a public/private key pair. The key I am using works using WinSCP. It is an SSH-2 RSA 1024 bit key. I am using version 1.3.1 of the component and version jsch-0.1.29 of the Java object. Does anyone know what could be wrong?
# Posted By Andy Clary | 12/21/06 7:17 PM
Andy Clary's Gravatar Michael Traher helped me find a solution. My problem was that the private key I was using was not in the right format for JSch. It only like keys in the openssh format, but mine was made with puttygen. I used puttygen to convert it to the openssh format and it works beautifully now. Thanks Michael!
# Posted By Andy Clary | 1/4/07 4:22 PM
Scott Marion's Gravatar I have downloaded and installed this component and am using it to try to connect to one our our secure ftp servers - but I am getting an error:

CFSFTP.connect;java.net.ConnectException: Connection refused: connect

Any ideas would be appreciated?

Thanks in advance.
# Posted By Scott Marion | 3/27/07 9:05 PM
Marco Antonio's Gravatar cfftp tag doesn't have support to use proxy password to connect. Haves proxyserver instead. I can use sftp with proxy server and proxy password? Cheers
# Posted By Marco Antonio | 3/28/07 5:44 PM
Antoinette Stanley's Gravatar Can you tell me what character set encoding scheme you're using? I have a vendor who is using UTF-8 to interpret special characters and it isn't working for them.

Thanks for creating this. It has made my life SO much easier.

Best Regards
# Posted By Antoinette Stanley | 4/11/07 10:41 PM
michael traher's Gravatar Hi Antoinette, Your question has me a bit stumped. Its not something I consciously set to any particular value and I suspect it is set by the environment you are running in. This could be at the JVM level, but the server end could also be a factor. Sorry I cannot help more on that one. May be one to post to CFTALK where better experts than me can assist. Best Regards Mike
# Posted By michael traher | 4/12/07 9:34 AM
Jeremy Halliwell's Gravatar Thanks for this Michael, very useful indeed. There were a couple of things I needed to do but couldn't with the current version: one was to specify a different SSH port number to the default 22 - you've hard coded that; the other was to use a UNC path to the working directory for local files which might be on another local server, rather than making it relative to the current directory or webroot. I've got round this by modifying the CFC but might be worth considering for a future version.
Cheers.
# Posted By Jeremy Halliwell | 5/1/07 5:39 PM
Sally Lokas's Gravatar Hello Michael,

The code looks great. I have been testing it but I keep getting errors. Is there an email address I can use to send you the details of the errors? Till then I'll try fixing them. Cheers!! :)
# Posted By Sally Lokas | 6/7/07 12:05 AM
Rachit's Gravatar Hi,
Awesome work, Micheal.
I am having some trouble with using the code. I am trying to contact the youtube server. I am using the following code to connect:

<cfset mysftp = createobject("component","sftp").init()>
<cftry>

<cfset mysftp.connect(host="xxx.youtube.com",
username="abc",
passphrase="xxxxx",
    privatekey="xxxyoutubekey",
knownhostsfile="xxx\known_hosts.cfm")>
<cfcatch type="any">
<cfif #findnocase("unknownhostkey",cfcatch.message)#>
       <cfset mysftp.addhostkey(host="xxx.youtube.com",
username="abc",
passphrase="xxxxx",
       privatekey="xxxyoutubekey",
knownhostsfile="xxx\known_hosts.cfm")>
<cfoutput>Adding host key for yourhost<br/></cfoutput>
<cfset mysftp.connect(host="xxx.youtube.com",
username="abc",
passphrase="xxxxx",
       privatekey="xxxyoutubekey",
knownhostsfile="xxx\known_hosts.cfm")>
</cfif>   
</cfcatch>
</cftry>
<cfset dirarray = mysftp.dir()>
<cfdump var="#dirarray#">
<cfset mysftp.disconnect()>
<cfset mysftp = "">

Once I run the file on my server, I am getting the following error:
CFSFTP.mkdir;Failure

can you please help me with this.
Thanks
# Posted By Rachit | 7/2/07 11:50 PM
jared's Gravatar Hey, this is a way cool solution for sftp.

Is there a chance that anyone has used this to connect to a FTPES ftp site?

FTPES - FTP over explicit TLS/SSL?

Thanks
# Posted By jared | 10/23/07 11:10 PM
Eric Roberts's Gravatar Great tag...ran into one issue:

how do I move back a directory (aka cd ..) or go to the root (aka cd \)? Once that is answered...this will be working like a charm!

Eric
# Posted By Eric Roberts | 10/26/07 12:49 AM
michael traher's Gravatar Hi Eric, The cd command should quite happily go up and down directories. If it is not happening on your server I would suspect permissions related to you login. ftp/sftp connection user accounts are often limited to only be able to access a limited directory space down from the login. Best regards Mike
# Posted By michael traher | 10/26/07 8:41 AM
michael traher's Gravatar Hi, For anyone reading these comments, you may see a number of apparently unanswered questions in these comments. I tend to answer most questions directly to the questioners email. Not perhaps the best way since it means the answers are not shared here. This is really just through laziness really since I first see the comments in my email. Regards Mike
# Posted By michael traher | 10/26/07 8:44 AM
Eric Roberts's Gravatar Yeah...I figured it out late last night...you have to use "../" instead of ".."

Thsanks!
Eric
# Posted By Eric Roberts | 10/26/07 3:57 PM
Jason's Gravatar Hi Michael,

Did you by chance answer jared's question about FTPES?

"Is there a chance that anyone has used this to connect to a FTPES ftp site?

FTPES - FTP over explicit TLS/SSL?"
# Posted By Jason | 7/31/08 9:24 PM
Epy's Gravatar Hi Michael,

I can browser through the files and folder but having problem to use GET and PUT. Any ideas?

Below are the error observed.
--------------------------------------------------------------------------------
CFSFTP.put;Failure


The error occurred in C:\Inetpub\wwwroot\temp2\cfsftp\sftp.cfc: line 231
Called from C:\Inetpub\wwwroot\temp2\cfsftp\sftp.cfc: line 159
Called from C:\Inetpub\wwwroot\temp2\cfsftp\upload_file.cfm: line 100

229 : <cfif isDefined("result.id")>
230 : <cfset this_error = errormess[result.id]>
231 : <cfthrow message="CFSFTP.#arguments.who_failed#;#this_error#">
232 : </cfif>
233 : <cfif isDefined("result.message")>
# Posted By Epy | 8/14/08 2:27 AM
Karen Rochette's Gravatar Hi Michael,

I am getting an error "Variable mychannel is undefined" on this code:
<cfset dirarray = mysftp.dir()>
<cfdump var="#dirarray#">

and "OVERWRITE is undefined in MYCHANNEL" on this code <cfset mysftp.put("test.txt","test2.txt")>.

Thanks
Karen
# Posted By Karen Rochette | 10/22/08 2:09 PM
Mark Turner's Gravatar Hi Michael, I am getting the following error:

CFSFTP.dir;Variable MYCHANNEL is undefined.


The error occurred in D:\Production\cfmx\web_applications\freerecall\sftp\sftp.cfc: line 234
Called from D:\Production\cfmx\web_applications\freerecall\sftp\sftp.cfc: line 121
Called from D:\Production\cfmx\web_applications\freerecall\sftp\dsp_Batch_page0006.cfm: line 29

232 : </cfif>
233 : <cfif isDefined("result.message")>
234 : <cfthrow message="CFSFTP.#arguments.who_failed#;#result.message#">
235 : </cfif>
236 : <cfthrow message="Unknown error in cfsftp">

Can you help? Is his an error with the known_hosts file?
# Posted By Mark Turner | 11/7/08 11:46 AM
Ris's Gravatar Hi All,

Does anyone have any information on the following error:
Variable mychannel is undefined.
# Posted By Ris | 11/24/08 12:20 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.003. Contact Blog Owner