How to pass password to scp in linux?

How to pass password to scp in linux?

Asked on October 23, 2018 in Linux.
Add Comment


  • 5 Answer(s)

    To use this expect  tool is used for scripting this ,the code of expect tool is

    sub copyover
    {
      $scp=Expect->spawn("/usr/bin/scp ${srcpath}/$file $who:${destpath}
    +/$file");
      $scp->expect(30,"ssword: ") || die "Never got password prompt from
    + $dest:$!\n";
    print $scp 'password' . "\n";
      $scp->expect(30,"-re",'$\s') || die "Never got prompt from parent 
    +system:$!\n";
      $scp->soft_close();
    return;
    }
    
    Answered on October 23, 2018.
    Add Comment

    You can use the ‘expect’ script on unix/terminal

    ex.

    #!/usr/bin/expect
    
    spawn scp /usr/bin/file.txt [email protected]<ServerLocation>:/home
    
    set pass "Your_Password"
    
    expect
    {
    
    password: {send "$pass\r"; exp_continue}
    
    }
    
    

    run the script

    expect test.exp
    
    

     

    Answered on October 23, 2018.
    Add Comment
    You can use curl as a alternative to scp to copy a file and it supports a password on the commandline.
    curl --insecure --user username:password -T /path/to/sourcefile sftp://desthost/path/
    
    
    Answered on October 23, 2018.
    Add Comment

    You should not use password-less ssh-keys for security reasons.

    The tool keychain can help you re-use the ssh-agent and run rsync over ssh in scripts without being prompted for passwords.

    To install it on a Ubuntu machine just run the following command.

    sudo apt-get install keychain
    Answered on January 16, 2019.
    Add Comment

    USE CODE:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    sub copyover
    {
      $scp=Expect->spawn("/usr/bin/scp ${srcpath}/$file $who:${destpath}
    +/$file");
      $scp->expect(30,"ssword: ") || die "Never got password prompt from
    + $dest:$!\n";
    print $scp 'password' . "\n";
      $scp->expect(30,"-re",'$\s') || die "Never got prompt from parent
    +system:$!\n";
      $scp->soft_close();
    return;
    }
    Answered on January 16, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.