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

Hello, I have my application running on one server(A). I have planned to move this on new server(B). I have another application(using perl) running on server(C). My application on server C is trying to make sftp connection with new server B but it gives me "BAD MESSAGE RECEIVED" error. But its working properly while making sftp connection with server A. I have installed sftp and also created new user for sftp on new server B. I have also checked sftp protocol version on server A & server B both are same(version 3). From command prompt I can make sftp connection easily from server C to server B but not from application. I am getting following error:
#25089 1363077870.00000 new: This is Net::SFTP::Foreign 1.73 #25089 1363077870.00000 new: Loaded from /usr/local/lib/perl5/site_per +l/5.16.1/Net/SFTP/Foreign.pm #25089 1363077870.00000 new: Running on Perl v5.16.1 for linux #25089 1363077870.00000 new: debug set to -1 #25089 1363077870.00000 new: ~0 is 18446744073709551615 #25089 1363077870.00000 new: Using backend Net::SFTP::Foreign::Backend +::Unix 1.73 #25089 1363077870.00000 _queue_msg: queueing msg len: 5, code:1, id:3 +... [1] 00 00 00 05 01 00 00 00 03 + | ......... #25089 1363077870.00000 _get_msg: waiting for message... [1] #25089 1363077870.00000 _do_io: _do_io connected: 1 #25089 1363077870.00000 _do_io: _do_io select(-,-,-, undef) #25089 1363077870.00000 _do_io: _do_io write queue: 9, syswrite: 9, ma +x: 65536, $!: 00 00 00 05 01 00 00 00 03 + | ......... #25089 1363077870.00000 _do_io: _do_io read sysread: 1, total read: 1, + $!: 0a + | . #25089 1363077870.00000 _do_io: _do_io select(-,-,-, undef) #25089 1363077870.00000 _do_io: _do_io read sysread: 27, total read: 2 +8, $!: 50 6c 65 61 73 65 20 74 79 70 65 20 27 79 65 73 27 20 6f 72 20 27 6e 6 +f 27 3a 20 | Please type 'yes' or 'no': #25089 1363077870.00000 _set_status: _set_status code: 5, str: Bad mes +sage #25089 1363077870.00000 _set_error: _set_err code: 13, str: bad remote + message received #25089 1363077870.00000 _conn_lost: _conn_lost bad remote message receivedunable to establish SSH connection: bad rem +ote message received at SftpProcess.pm line 205. #25089 1363077870.00000 DESTROY: Net::SFTP::Foreign=HASH(0x41088f0)->D +ESTROY called (current pid: 25089, disconnect_by_pid: ) #25089 1363077870.00000 disconnect: Net::SFTP::Foreign=HASH(0x41088f0) +->disconnect called (ssh pid: ) #25089 1363077870.00000 _conn_lost: _conn_lost
Kindly help me out. Thanks

Replies are listed 'Best First'.
Re: sftp connection through perl
by salva (Canon) on Mar 12, 2013 at 10:07 UTC
    It seems the slave SSH client process is asking for some confirmation on STDIN (that's wrong, it should be using /dev/tty). Probably, the remote host key is unknown to it and it is asking you about its validity.

    Which SSH software and version are you using (ssh -V)? what is your OS?

    The easiest solution to that problem would probably be to install OpenSSH and tell Net::SFTP::Foreign to use it.

      Thanks for repply. Below are the details: On server C ssh : OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008 OS: CentOS release 5.8 (Final) On server B ssh: OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008 OS: CentOS release 5.9 (Final)
        Aha, I see what is happening! You are using a custom transport!!!

        Probably, your script is quite old, from the days when Net::SFTP::Foreign didn't support password authentication directly and Expect was used to establish the connection and log into the server.

        Nowadays, you can pass the user and password to Net::SFTP::Foreign constructor and let it handle everything for you.

Re: sftp connection through perl
by blue_cowdawg (Monsignor) on Mar 12, 2013 at 12:53 UTC
        #25089 1363077870.00000 _do_io: _do_io select(-,-,-, undef) #25089 1363077870.00000 _do_io: _do_io read sysread: 27, total read: 2 +8, $!: 50 6c 65 61 73 65 20 74 79 70 65 20 27 79 65 73 27 20 6f 72 20 27 6e 6 +f 27 3a 20 | Please type 'yes' or 'no': #25089 1363077870.00000 _set_status: _set_status code: 5, str: Bad mes +sage

    First off, you haven't shown us any Perl code for us to see what you are attempting to do.

    That said: I have quoted some of the trace that you supplied and in there is the smoking gun so to speak. It would appear that serverB is not in the known_hosts file for the userid that is running the script. Very simple fix:

    1. Log in as the userid that is running the script
    2. SSH from that userid to hostB as the userid SFTP is logging in as.
    3. When SSH asks you to accept the key say "yes."
    That will cause the known_hosts file to be populated with the public key of the remote host

    Works fine... lasta a long time...


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Thanks blue_cowdawg. I tried the same way and it worked.