#!/usr/bin/expect -f # val_expect_sftp.txt by Iritscen # A script for driving "sftp" to upload the HTML report that is produced by the # Validate External Links script. # Check arguments if {$argc != 6} \ { puts "Error: You need to pass me the path to the file to upload, the SFTP user name, the SFTP password, the SFTP port, the upload destination, and the name of the uploaded file, in that order." exit 1 } \ else \ { set report_file "[lindex $argv 0]" set sftp_user "[lindex $argv 1]" set sftp_pw "[lindex $argv 2]" set sftp_port "[lindex $argv 3]" set upload_path "[lindex $argv 4]" set upload_name "[lindex $argv 5]" } # Check that file to be uploaded exists if {![info exists report_file]} \ { puts "Error: There isn't a file at path $report_file." exit 2 } \ # Turn off output except for 'puts' commands log_user 0 set timeout 10 # Spawn sftp with: # - a single password try, so that ssh is less likely to get hung up at login # - the custom port used by oni2.net for SFTP # - a disabled ssh_knownhosts file, so sftp doesn't ask us whether we want to # accept the certificate spawn sftp -oNumberOfPasswordPrompts=1 -oPort=$sftp_port -oStrictHostKeyChecking=no $sftp_user # Expect ssh password prompt expect \ { timeout {puts "Error: ssh failed to connect to server."; exit 3} "password: $" } # Log in send "$sftp_pw\r" # Expect sftp command prompt expect \ { timeout {puts "Error: ssh login failed."; exit 4} "sftp> $" } # Move to desired upload path send "cd $upload_path\r" # Expect sftp command prompt again expect \ { timeout {puts "Error: Could not enter directory of upload path $upload_path."; exit 5} "sftp> $" } # Send the desired file with the supplied new name for it send "put \"$report_file\" \"$upload_name\"\r" # Expect at least one progress report, e.g. "100%" expect \ { timeout {puts "Error: sftp 'put' failed to run."; exit 6} -re "\[0-9\]%" } # Expect possible additional progress reports and expect the sftp command # prompt when done uploading expect \ { # Continue waiting while progress reports are happening -re "\[0-9\]%" exp_continue # Otherwise, if nothing happens for three seconds, there must be a # problem timeout {puts "Error: sftp 'put' failed to complete."; exit 7} # We're done when we get the sftp command line again "sftp> $" } # Log out send "bye\r" # Expect EOF expect \ { timeout {puts "Error: sftp logout failed."; exit 8} eof } exit 0