expect
seria usado para ssh
exatamente da mesma maneira que para sftp
, com o bit mais complicado sendo como extrair a soma de verificação da saída. Isso pode ser algo nos moldes de
#!/usr/bin/env expect
#
# remote host sftp and then checksum a file. assumes linux coreutils
# available on both ends
if {[llength $argv] == 0} {
puts stderr "Usage: $argv0 somefiletoxfer"
exit 64
}
set local_file [lindex $argv 0]
set local_sum [lindex [split [exec md5sum $local_file] " "] 0]
set file_basename [lindex [split $local_file "/"] end]
set match_max 9999 ;# in the event of much output spam from sftp or ssh
set timeout 60
# these could also be read from $argv
set EWS_USER todofixme
set EWS_HOST todofixme
set TGT_DIR todofixme
set password hunter2
spawn sftp ${EWS_USER}@${EWS_HOST}:${TGT_DIR}
expect -ex "assword:"
send "$password\r"
expect -ex "sftp>"
send "put $local_file\r"
expect -ex "sftp>"
send "bye\r"
expect EOF
spawn ssh ${EWS_USER}@${EWS_HOST}
expect -ex "assword:"
send "$password\r"
send "md5sum ${TGT_DIR}/$file_basename\r"
expect -re {md5sum [^\n]+\n([A-Za-z0-9=_-]+) }
set remote_sum $expect_out(1,string)
send "exit\r"
expect EOF
if {$local_sum ne $remote_sum} {
puts stderr "a failure prompts this dispatch"
puts stderr "for thy checksums do mismatch"
puts stderr "local >$local_sum<"
puts stderr "remote >$remote_sum<"
exit 1
}
puts $remote_sum
exit 0