Shell Script: SFTP a File

Sometimes instead of executing a built in command on a remote system you need to upload a script that you will need to execute.  Again a simple expect script comes to our rescue to upload a file to a remote system.

#!/usr/bin/expect -f
set user [lindex $argv 0]
set host [lindex $argv 1]
set password [lindex $argv 2]
set file [lindex $argv 3]
 
spawn sftp $user@$host
expect "password:*"
send -- "$password\r"
expect "sftp>"
send -- "put $file\r"
expect "sftp>"
send -- "exit\r"
interact

Usage: sftp_file <username> <hostname> <password> <file to upload>

  • Ghostprogrammer
  • I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: