Oct 26, 2018

How to sftp with expect

Install expect:
On Ubuntu install it with:
sudo apt-get install expect
On a CentOS Machine install it with:
yum install expect
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 [email protected].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