Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Help Required - Problem Automating SFTP

by Kraythorne (Sexton)
on Jan 28, 2009 at 10:59 UTC ( #739492=perlquestion: print w/replies, xml ) Need Help??

Kraythorne has asked for the wisdom of the Perl Monks concerning the following question:

OK, I guess you could call me an 'Intermediate Perl User' - not an expert by any stretch.

I'm trying to automate SFTP polling and donwnloading from one of our clients' site but have hit a stumbling block. Here's what I'm doing (using a windows xp OS):

The client has generated the secure encrypted key.
Installed openssh
Installed Putty
sshd service is running


use strict; use warnings; use Net::SFTP::Foreign; my $host = '*mycompany*@*client_ftp_IP*'; my %args = ('ssh_cmd' => 'C:\Program Files\PuTTY\plink', ); my $sftp = Net::SFTP::Foreign->new( $host, %args, more => [ -P => '*connection port*', -i => 'G:\FTP\id_cmf_mediatechid_cmf_mediatech.ppk', -ssh, '-v', ] ); $sftp->error and die "Unable to stablish SFTP connection: " . sftp->error; $sftp->get("foo", "bar") or die "get failed: " . sftp->error; $sftp->put("bar", "baz") or die "put failed: " . sftp->error;
Looking up host "*client_ftp_IP*" Connecting to *client_ftp_IP* port *connection port* Server version: SSH-2.0-OpenSSH_5.1 We claim version: SSH-2.0-PuTTY_Release_0.60 Using SSH protocol version 2 Doing Diffie-Hellman group exchange Doing Diffie-Hellman key exchange with hash SHA-256 Host key fingerprint is: ssh-rsa 2048 *FINGERPRINT KEY* Initialised AES-256 SDCTR client->server encryption Initialised HMAC-SHA1 client->server MAC algorithm Initialised AES-256 SDCTR server->client encryption Initialised HMAC-SHA1 server->client MAC algorithm Reading private key file "G:\FTP\id_cmf_mediatech\id_cmf_mediatech.ppk +" Using username "mycompany". Offered public key Offer of public key accepted Authenticating with public key "rsa-key-20081029" Access granted Opened channel for session Server refused to start a shell/command FATAL ERROR: Server refused to start a shell/command Unable to stablish SFTP connection: Connection to remote server is bro +ken at C:\PROGRA~1\OpenSSH\usr\sbin\ line 24. ==============================
use strict; use warnings; use Net::SFTP::Foreign; my $host = '*mycompany*@*client_ftp_IP*'; my %args = ('ssh_cmd' => 'C:\Program Files\OpenSSH\bin\ssh', ); my $sftp = Net::SFTP::Foreign->new( $host, %args, more => [ -p => '*connection port*', -i => 'C:\Documents and Settings\.ssh\id_cmf_mediatech', '-v', ] ); $sftp->error and die "Unable to stablish SFTP connection: " . $sftp->error;
OpenSSH_3.8.1p1, OpenSSL 0.9.7d 17 Mar 2004 debug1: Reading configuration data /etc/ssh_config debug1: Connecting to *client_ftp_IP* [*client_ftp_IP*] port <connecti +on port>. debug1: Connection established. debug1: identity file C:\\Documents and Settings\\.ssh\\id_cmf_mediate +ch type -1 debug1: Remote protocol version 2.0, remote software version OpenSSH_5 +.1 debug1: match: OpenSSH_5.1 pat OpenSSH* debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_3.8.1p1 debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: server->client aes128-cbc hmac-md5 none debug1: kex: client->server aes128-cbc hmac-md5 none debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP debug1: SSH2_MSG_KEX_DH_GEX_INIT sent debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY debug1: Host '<client_ftp_IP>' is known and matches the RSA host key. debug1: Found key in /home/.ssh/known_hosts:1 debug1: ssh_rsa_verify: signature correct debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: SSH2_MSG_SERVICE_REQUEST sent debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey,gssapi-with-mic,p +assword debug1: Next authentication method: publickey debug1: Trying private key: C:\\Documents and Settings\\.ssh\\id_cmf_m +ediatech debug1: read PEM private key done: type RSA debug1: Authentication succeeded (publickey). debug1: channel 0: new [client-session] debug1: Entering interactive session. debug1: Sending subsystem: sftp Request for subsystem 'sftp' failed on channel 0 Unable to stablish SFTP connection: Connection to remote server is bro +ken at C:\ PROGRA~1\OpenSSH\usr\sbin\ line 23.

I'm trying to figure out why this is falling down but the Putty and OpenSSH help is vague and other than checking that the Subsystem path in the sshd_config for OpenSSH is correct, I'm stuck!

If anyone can help suggest how I can either fix this, or an alternative method of initiating sftp which I can automate through Perl, I would greatly appreciate it.


Replies are listed 'Best First'.
Re: Help Required - Problem Automating SFTP
by derby (Abbot) on Jan 28, 2009 at 12:33 UTC

    I feel your pain ... SFTP is a rather overloaded word (some people construe it to be FTP over ssh, some people construe it to be SCP and some explicitly SFTP). Your debug statements lend me to believe that the server is not really configured (or properly configured) for SFTP. Can you SFTP outside of perl? (does putty include an SFTP client?). Can you SCP or SSH outside of perl?

    I have no experience with SFTP (or SSH) in windows, but for my *nix work, I prefer Net::SSH2 over all the other SSH/SFTP modules.

      Net::SSH2 for SFTP? you are a masochist! ;-)

      Seriously, try using (my) Net::SFTP::Foreign instead, it is far easier to use and more powerful!

      Check also my other module Net::OpenSSH, it is still a work in progress but I am sure you will find it interesting.

        Ha! Not really, when most of my clients say SFTP then configure their servers for SCP - and that's a breeze with Net::SSH2 ... but thanks I'll look into Net::OpenSSH if the need arises (and it probably will!)

      I can connect using filezilla without any problems - it's just command line that is giving me the problems.

      I don't know if the error I'm getting is from my sshd service or the clients' sftp server?

      If it's the clients server then what do I need to ask them to do to accept shell commands? If it's my end then what can I do?

      It seems that I can connect to the client and be verified using the key they supplied, but then loose connection when I try to communicate :-(

        In looking at those messages, it appears you are getting a connection and getting authenticated with SSH, but the remote host is not starting the session shell properly. Confirm all the settings with the remote host admin and have them check to make sure their server is up an responding (and allowing logins to start correctly). It could also be a permissions issue on the remote host if you don't have the appropriate permisions to the login directory, you might not be able to get in to the site even though you can authenticate properly.
        does command line scp work?

        Run a couple of tests using scp and sftp commands with the verbose flag (-v) and post here the output.

Re: Help Required - Problem Automating SFTP
by salva (Canon) on Jan 28, 2009 at 12:25 UTC
    Request for subsystem 'sftp' failed on channel 0

    It seems that the remote server is refusing SFTP connections.

    What happens when you try to connect to the remote host using the sftp command manually?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://739492]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-10-03 15:21 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (14 votes). Check out past polls.