Why Does Automating Sftp with Expect Hang After Sending The Password

Why does automating SFTP with Expect hang after sending the password?

Try like this:

#!/bin/bash
expect -c "
spawn sftp remoteuser@XX.XX.XX.XX
expect \"password\"
send \"PASSWORD\r\"
interact "

Example : http://www.techtrunch.com/scripting/lazy-admins-part-2

Linux Expect SFTP + put file

I understand you need some help, but you should at least try doing some reading. When you say expect -c 'some stuff ...', you are specifying the entire expect script as the parameter to the -c option. The Expect session will not exist afterward.

Also, try to understand what the interact command does instead of blindly applying what someone advises.

Anyway, the solution:

expect  <<'END_EXPECT'
set timeout -1
spawn sftp remoteuser@1.2.3.4
expect "[Pp]assword:"
send "passwrd\r"
expect "whatever the sftp prompt looks like"
send "put output/data.xml\r"
expect "whatever the sftp prompt looks like"
send "quit\r"
expect eof
END_EXPECT

Note that the here-doc delimiter is quoted when you first see it. That way, you will not be subjected to any shell expansions. It's like the entire here-doc is single-quoted.

Expect Script for SFTP not working in bash

Okay, Apparently i was missing expect "sftp>" {send "bye\n"} , before EOF (EOFEXPECT1).

However, i would still be interested in knowing the significance of bye in expect script.

Here is the updated and working code :

#!/bin/ksh

FTPREMOTEPATH=/Inbox
FTPREMOTEFILENAME=test.CSV

/usr/bin/expect -f - <<EOFEXPECT1
set timeout 60
spawn sftp -oPort=1002 username@test.server.com
expect {
default { exit 1}
-re "failed|invalid password|Permission denied" {exit 2}
"Connection closed" {exit 1}
timeout {exit 1}
}

expect "Password:"
send "password\r"

expect {
default {exit 1}
-re "password|Enter password for " {puts "Incorrect Password"; exit 2}
"sftp>" {send "cd $FTPREMOTEPATH \r"}
}

expect "sftp>"
send "pwd\r"

send "get $FTPREMOTEFILENAME \r";

expect "sftp>" {send "bye\n"}

EOFEXPECT1

why the password is visible for a split second even after i am doing stty -echo?

I tried your code and it works fine for me. Which version of the Expect you're using?

By the way, I usually write like this:

stty -echo
send_user "Password: "
expect_user -timeout 3600 -re "(.*)\n"
stty echo
send_user "\r\n"
set passwd "$expect_out(1,string)"

Using expect to pass a password to ssh

I always used the "proper" solution, but I used expect in other situations.

Here I found following suggestion:

#!/usr/local/bin/expect
spawn sftp -b cmdFile user@yourserver.com
expect "password:"
send "shhh!\n";
interact

Use Expect in a Bash script to provide a password to an SSH command

Mixing Bash and Expect is not a good way to achieve the desired effect. I'd try to use only Expect:

#!/usr/bin/expect
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com

# Use the correct prompt
set prompt ":|#|\\\$"
interact -o -nobuffer -re $prompt return
send "my_password\r"
interact -o -nobuffer -re $prompt return
send "my_command1\r"
interact -o -nobuffer -re $prompt return
send "my_command2\r"
interact

Sample solution for bash could be:

#!/bin/bash
/usr/bin/expect -c 'expect "\n" { eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com; interact }'

This will wait for Enter and then return to (for a moment) the interactive session.

SFTP using Expect

Thank you for your attempts to solve the problem above. I found the solution. Here's what I did:

/usr/local/bin/expect <<EOF
spawn sftp PG1@dev1.dummy.com
expect "Password:"
send "abc123\r"
expect "sftp>"
send "cd /tmp\r"
expect "sftp>"
send "get Data.dat\r"
expect "sftp>"
send "get List.dat\r"
expect "sftp>"
send "bye\r"
EOF


Related Topics



Leave a reply



Submit