私たちのLinux設定には、パスワードなしの認証用に設定されたキー生成はありません。したがって、EXPECT
シェルスクリプトではパスワードのない認証のみを使用する必要があります。
/usr/bin/expect<<EOF >> ${LOG_FILE}
set timeout 60
spawn sftp ${EWS_USER}@${EWS_HOST}:${TGT_DIR}
expect "*?assword:"
send "$password\r"
expect "sftp>"
send "put $local_dir/$line\r"
expect "sftp>"
send "bye\r"
expect EOF
EOF
filename=$(basename "$line")
# echo "File Name: $filename"
#Calculate the MD5Sum locally.
local_md5sum=$(md5sum "$line")
#echo "Local MD5Sum: ${local_md5sum}"
#Calculate the MD5sum in remote machine
remote_md5sum=$(ssh ${EWS_USER}@${EWS_HOST} "cd '$TGT_DIR' ; find -name '$filename' -exec md5sum {} \;" < /dev/null)
#echo "Remote MD5Sum: ${remote_md5sum}"
LOCAL_SUM=`echo ${local_md5sum} | awk {'print $1'}`
REMOTE_SUM=`echo ${remote_md5sum} | awk {'print $1'}`
echo $LOCAL_SUM
echo $REMOTE_SUM
if [ "${LOCAL_SUM}" != "${REMOTE_SUM}" ]
then
echo "SFTP Successfull"
else
echo "SFTP Unsuccessfull"
fi
EXPECT
次のシナリオで使用する方法を知っています。
sftp ${EWS_USER}@${EWS_HOST} << EOF >> ${LOG_NAME}
put ${LOCAL_DIR}/${line} ${TGT_DIR}/${line}
EOF
しかし、次のシナリオでEXPECTを使用して接続をパスワードなしで作成する方法を知っていますか?
remote_md5sum=$(ssh ${EWS_USER}@${EWS_HOST} "cd '$TGT_DIR' ; find -name '$filename' -exec md5sum {} \;" < /dev/null)
答え1
expect
ssh
forはforとまったく同じ方法で使用されますsftp
。最も複雑な部分は、出力からチェックサムを抽出する方法です。これは、次の行に沿って実行できます。
#!/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