Our Linux setup doesn't have keygen configured for password less authentication. Hence I am supposed to use only EXPECT for password less authentication in a shell script.

   /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

        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'}`
if [ "${LOCAL_SUM}" != "${REMOTE_SUM}" ]
        echo "SFTP Successfull"
        echo "SFTP Unsuccessfull"

I know how to use EXPECT in the below place

sftp ${EWS_USER}@${EWS_HOST} << EOF >> ${LOG_NAME}
put ${LOCAL_DIR}/${line} ${TGT_DIR}/${line}

But any idea how to use EXPECT here in the below scenario to make the connection password less?

remote_md5sum=$(ssh ${EWS_USER}@${EWS_HOST} "cd '$TGT_DIR' ; find -name '$filename'  -exec md5sum {} \;" < /dev/null)
  • Typically in expect via spawn ssh ssh-args-here and then suitable statements to match and respond to the password prompt. – thrig Oct 26 '16 at 14:14
  • Have you tried sshpass software? It might be easier to use it in this example. – Kalavan Oct 26 '16 at 14:22
  • I cant use SSHPASS. I am expected to make it work with EXPECT @Kalavan – sabarish jackson Oct 26 '16 at 14:28
  • @thrig I know how to use EXPECT normally for SFTP. But I am confused of how to use EXPECT for the below line. remote_md5sum=$(ssh ${EWS_USER}@${EWS_HOST} "cd '$TGT_DIR' ; find -name '$filename' -exec md5sum {} \;" < /dev/null Could you please elaborate how to use EXPECT in the above line? – sabarish jackson Oct 26 '16 at 14:31
  • What have you written for expect? All I see is shell code, not expect. – thrig Oct 26 '16 at 15:07

expect would be used for ssh in precisely the same way as for sftp, with the most complicated bit being how to extract the checksum from the output. This might run something along the lines of

#!/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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.