How to send password using sftp batch file

前端 未结 6 2301
挽巷
挽巷 2020-12-08 19:05

I\'m trying to download a file from sftp site using batch script. I\'m getting the following error:

Permission denied (publickey,password,keyboard-interactiv         


        
相关标签:
6条回答
  • 2020-12-08 19:49

    I advise you to run sftp with -v option. It becomes much easier to fathom what is happening.

    The manual clearly states:

    The final usage format allows for automated sessions using the -b option. In such cases, it is necessary to configure non-interactive authentication to obviate the need to enter a password at connection time (see sshd(8) and ssh-keygen(1) for details).

    In other words you have to establish a publickey authentication. Then you'll be able to run a batch script.

    P.S. It is wrong to put your password in your batch file.

    0 讨论(0)
  • 2020-12-08 19:54
    PSFTP -b path/file_name.sftp user@IP_server -hostkey 1e:52:b1... -pw password
    

    the file content is:

    lcd "path_file for send"
    
    cd path_destination
    
    mput file_name_to_send
    
    quit
    

    to have the hostkey run:

    psftp  user@IP_SERVER
    
    0 讨论(0)
  • 2020-12-08 19:56

    You mention batch files, am I correct then assuming that you're talking about a Windows system? If so you cannot use sshpass, and you will have to switch to a different option.

    Two of such options, that follow diametrically opposite philosophies are:

    • psftp: command-line tool that you can call from within your batch scripts; psftp is part of the PuTTY package and you can find it here http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
    • Syncplify.me FTP Script: a scriptable FTP/S and SFTP client for Windows that allows you to store your password in encrypted "profile files"; check it out here http://www.syncplify.me/products/ftp-script/

    Either way, switching from password to PKI authentication is strongly recommended.

    0 讨论(0)
  • You'll want to install the sshpass program. Then:

    sshpass -p YOUR_PASSWORD sftp -oBatchMode=no -b YOUR_COMMAND_FILE_PATH USER@HOST

    Obviously, it's better to setup public key authentication. Only use this if that's impossible to do, for whatever reason.

    0 讨论(0)
  • 2020-12-08 20:08

    If you are generating a heap of commands to be run, then call that script from a terminal, you can try the following.

    sftp login@host < /path/to/command/list
    

    You will then be asked to enter your password (as per normal) however all the commands in the script run after that.

    This is clearly not a completely automated option that can be used in a cron job, but it can be used from a terminal.

    0 讨论(0)
  • 2020-12-08 20:09

    You need to use the command pscp and forcing it to pass through sftp protocol. pscp is automatically installed when you install PuttY, a software to connect to a linux server through ssh.

    When you have your pscp command here is the command line:

    pscp -sftp -pw <yourPassword> "<pathToYourFile(s)>" <username>@<serverIP>:<PathInTheServerFromTheHomeDirectory>
    

    These parameters (-sftp and -pw) are only available with pscp and not scp. You can also add -r if you want to upload everything in a folder in a recursive way.

    0 讨论(0)
提交回复
热议问题