Error connecting to SFTP site via R

Hello,

We are using the latest Rstudio-Connect release on our RHEL8 VM with the R 4.2.1 version.
We are trying to connect to our SFTP site ;however, we are receiving an error message as

"Error in function (type, msg, asError = True)" 
Error establishing ssh connection

I checked the ports and we are able to telnet to the remote sftp site.
Below is the code snippet that we are using, are there any other ways we can connect to sftp site?

sftp_con <- sftp_connect(server = 'sftp.example.com', 
                          username = user, 
                          password = *** 
                          folder = 'Folder')
sftp_files <- sftp_listfiles(sftp_con)

Hi there, are you using the {sftp} package? Depending on the sftp host, there could be a number of config changes in curl that need to be made at the time of request. Clients like Filezilla are good at doing that automatically. First, I would try to see where the connection is failing by doing sftp_listfiles(sftp_connection = sftp_con, curlPerformVerbose = TRUE)

1 Like

Hi Michael,

I tried this and received error as below, its saying Failure establishing ssh session, however, I am able to connect with this same creds via WinSCP or other clients. Any suggestions? Thankyou!

SFTP url: sftp://sftp.example.com/Folder/

  • Trying 10.2.xxx.xx...
  • TCP_NODELAY set
  • Connected to sftp.example.com (10.2.xxx.xx) port 22 (#0)
  • User: user
  • Failure establishing ssh session
  • Closing connection 0
    Error in function (type, msg, asError = TRUE) :
    Failure establishing ssh session

Hi @abhi04d ,

I think this is a similar issue that I had with an FTP server connection. I was getting a 451 access denied error though, but maybe this Error establishing ssh connection is similar?

Anyways what solved it for me was adding ftp.ssl = TRUE to my connection. Maybe try adding that to your arguments?

1 Like

Hi jonesey,

I tried that already but I believe that parameter is not present for the sftp function I am using.

I agree with @jonesey441, that is a good first attempt. If that doesn't work, you may need to make the connection in WinSCP again and enable a logging or debug mode. I have done the same with FileZilla in the past, and it gives me hints of what is being under the hood to make a successful connection. At that point, once you get an idea of additional parameters/things to check, reference https://everything.curl.dev/ to see what available parameters to include inside of the curl_options argument in sftp_list. To see exactly which options you can use, execute RCurl::listCurlOptions() to get a list. For example, for @jonesey441 recommendation, type sftp_listfiles(sftp_connection = sftp_con, curl_options = list(ftp.ssl = TRUE))

Did you try sftp_listfiles(sftp_connection = sftp_con, curl_options = list(ftp.ssl = TRUE)) ?

Hi Michael, I tried that as well, but still no luck. Can you share me the syntax to use SSH private key & public key

I tried using below:
sftp_files <- sftp_listfiles(sftp_connection = sftp_con,
verbose = TRUE,
curlPerformVerbose = TRUE,
curl_options = list('ssh.private.keyfile = /home/.ssh/id_rsa_sftp','ssh.public.keyfile = /home/.ssh/id_rsa_sftp.pub')
)
But I am still getting the same error. :expressionless:

Sorry I never had to use that option, but the quotes you have look off. Try:

sftp_files <- sftp_listfiles(sftp_connection = sftp_con,
verbose = TRUE,
curlPerformVerbose = TRUE,
curl_options = list(ssh.private.keyfile = "/home/.ssh/id_rsa_sftp",ssh.public.keyfile = "/home/.ssh/id_rsa_sftp.pub")
)
1 Like

Its still giving me the same error. I don't know what else to check now. :confused:

At this point I am just taking shots in the dark. This stackoverflow post seems promising. This is suggesting that the format of your ssh keys are not right. Check the format of your ssh key files per that post to make sure it is compatible with the {RCurl} package, which {sftp} uses internally.

Hopefully this works or provides for paths to explore.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.