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

Hello Monks,
I am trying to do sftp from command line. The problem is the session becomes interactive , and I want to make it non interactive to enter the password and drop a file.
Is there any way this can be done ?
Currently I am doing
my $result = `sftp $userName\@$url `;
But this prompts for user password. Is there any way this can be done in one go ?
Also I want to drop a file to the connected server once connectivity is established and then disconnect from sftp
I am not able to install Net::SFTP module...its throwing 'Can't locate Net/SSH/Perl/Buffer.pm' even though I tried installing it...
Any help will be appreciated

Replies are listed 'Best First'.
Re: Need help in doing SFTP via command line
by NetWallah (Canon) on Oct 12, 2016 at 23:47 UTC
    This is somewhat off-topic for perl, but you could look at "ssh-keygen" to generate a key pair, to allow for automated authentication keys. This should prevent sftp from asking for a password.

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

Re: Need help in doing SFTP via command line
by salva (Canon) on Oct 13, 2016 at 07:15 UTC
Re: Need help in doing SFTP via command line
by FreeBeerReekingMonk (Deacon) on Oct 13, 2016 at 07:59 UTC
    You can also try expect, for which a perl module also exist.

    But as another monk said earlier, look up these:

    ssh-keygen (generate a public and private key in .ssh_ DONT USE A PASSWORD when asked, just press enter)

    ssh-copy-id (copy your public key to the other machine)

Re: Need help in doing SFTP via command line
by punitpawar (Sexton) on Oct 13, 2016 at 21:27 UTC
    Thanks for the suggestions
    However the nature of my application is such that I have to connect to the SFTP server each time , with newly generated credentials to upload some files
    So I do require a way in which , I should be able to log on to the SFTP server with the new set of credentials, drop my files , and then close the connection
    Currently via command line when I do something like `sftp $userName\@$url ` , it actually prompts the user to put the password , which has been generated on the screen
    I wanted to get control over the user Interaction , by trying to add the password via my perl program itself
    In perl , is there any way to prevent the interactive user mode and to force the user entries via the program itself ?
    Thanks Once Again

      Have you looked at Net::SFTP::Foreign? It allows you to pass in the password, at least on unixish systems.

        Hello, Thanks for the suggestion. Yes I was able to instal Net::SFTP::Foreign and I was able to successfully connect to the server.
        The place where I am stuck is when I try to do a 'Put'. I get the following message
        put failed: the authenticity of the target host can't be established, +the remote host public key is probably not present on the '~/.ssh/kno +wn_hosts' file
        The reason being that there seems to be an offending key in the ssh/known_hosts file , which has to be overidden.
        I tried to do the same thing from the command line and I got the following result. From the command line , I can select the option "yes" for it to continue connecting to the server , but I do not know if I can do the same thing from the STP::Foreign module
        Here is my command line output
        sftp 2000550000506913@sftp.XXX.com@upload-itms6.XXX.com Warning: the ECDSA host key for 'upload-itms6.XXX.com' differs from th +e key for the IP address '152.125.77.44' Offending key for IP in /Users/punitpawar/.ssh/known_hosts:2 Matching host key in /Users/punitpawar/.ssh/known_hosts:7 Are you sure you want to continue connecting (yes/no)? yes 2000550000506913@sftp.XXX.com@upload-itms6.XXX.com's password:
Re: Need help in doing SFTP via command line
by Sathishkumar (Scribe) on Oct 18, 2016 at 12:55 UTC
    I used to solve this kind of problem using sftp.exe . Thanks, Sathish