How to run the sftp command with a password from Bash script?

后端 未结 9 590
礼貌的吻别
礼貌的吻别 2020-11-22 11:55

I need to transfer a log file to a remote host using sftp from a Linux host. I have been provided credentials for the same from my operations group. However, since I don\'t

9条回答
  •  北海茫月
    2020-11-22 12:33

    EXPECT is a great program to use.

    On Ubuntu install it with:

    sudo apt-get install expect
    

    On a CentOS Machine install it with:

    yum install expect
    

    Lets say you want to make a connection to a sftp server and then upload a local file from your local machine to the remote sftp server

    #!/usr/bin/expect
    
    spawn sftp username@hostname.com
    expect "password:"
    send "yourpasswordhere\n"
    expect "sftp>"
    send "cd logdirectory\n"
    expect "sftp>"
    send "put /var/log/file.log\n"
    expect "sftp>"
    send "exit\n"
    interact
    

    This opens a sftp connection with your password to the server.

    Then it goes to the directory where you want to upload your file, in this case "logdirectory"

    This uploads a log file from the local directory found at /var/log/ with the files name being file.log to the "logdirectory" on the remote server

提交回复
热议问题