Trying to troubleshoot an SFTP script that uses Expect

I'm having trouble with an Expect script on Red Hat Enterprise Server 6.10.  I would think that the script below would spawn SFTP, then send the password.  I'm getting a Password error, which I am guessing is due to lots of escapable characters.

The script below gets me a "permission denied" error.  My password ( I changed some characters) is something like  r10@4g@$^#a&r*a^^$%  .  I have tried escaping the 'special' characters with \ and also tried escaping every character (so it looks like \r\1\0\@\4\g\@\$\^\#\a\&\r\*\a\^\^\$\%).  When I manually connect to the site and manually enter a password it works fine.

Can someone tell me what I might be missing?

Thanks for any advice you can provide,

-Dan

#!/usr/bin/expect
spawn sftp username@ftpprd.host.com
expect "username\@ftpprd.host.com\'s password: "
send "r10@4g@$^#a&r*a^^$%\n"


T
Dan PackerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
You best try to run the command and see whether the expect should be in the form that you are looking which is explicit and exact where a simple
expect "password: " 

note the response is commonly ended with a space also adding unnecessary ....

use single quotes or you have to escape the & another option is to assign these to a variable first
username='username@ftpprd.host.com'
password='r10@4g@$^#a&r*a^^$%'

spawn sftp $username
expect 'password: '
send "$password"
expect 'sftp> '
#connection established do stuff.
0
Robert LemMrCommented:
it you can use an ip address it would be better.
on expect, you can use only the last characters, not the entire prompt.
on the password i use \r instead of \n

#!/usr/bin/expect
spawn sftp username@ipaddress
expect "assword: "
send "r10@4g@$^#a&r*a^^$%\r"
0
Duncan RoeSoftware DeveloperCommented:
In Expect (or Tcl in general) text in braces ({}) does not require escaping, whereas text in double-quotes does. So in https:#a42676112, substitute send {r10@4g@$^#a&r*a^^$%}; send \r
While debugging, you also might like to insert exp_internal 1 near the start of your script. This will confirm to you what is being sent, and also shows what comes back.
1
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Dan PackerAuthor Commented:
Thanks - I'm having a weird, different issue:  The script seems to be sending the right data, but the commands aren't getting executed in SFTP.  At the end of the script I'm trying to do an 'ls' and a 'put', but neither command is getting executed.  Can I get another hint?
 
Sample output below (I set exp_internal 1 to show me the inputs and outputs, and changed the server names / passwords)

danp@qvarzpcldb03:~$ ./sftp2.sh
spawn sftp 0725@ftpprd.bank.com
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {116608}

expect: does "" (spawn_id exp5) match glob pattern "assword: "? no
Connecting to ftpprd.bank.com...

expect: does "Connecting to ftpprd.bank.com...\r\n" (spawn_id exp5) match glob pattern "assword: "? no
Access to this site is entirely prohibited unless permission has been granted.

expect: does "Connecting to ftpprd.bank.com...\r\nAccess to this site is entirely prohibited unless permission has been granted.\r\n" (spawn_id exp5) match glob pattern "assword: "? no
Authenticated with partial success.

expect: does "Connecting to ftpprd.bank.com...\r\nAccess to this site is entirely prohibited unless permission has been granted.\r\nAuthenticated with partial success.\r\r\n" (spawn_id exp5) match glob pattern "assword: "? no
0725@ftpprd.bank.com's password:
expect: does "Connecting to ftpprd.bank.com...\r\nAccess to this site is entirely prohibited unless permission has been granted.\r\nAuthenticated with partial success.\r\r\0725@ftpprd.bank.com's password: " (spawn_id exp5) match glob pattern "assword: "? yes
expect: set expect_out(0,string) "assword: "
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) "Connecting to ftpprd.bank.com...\r\nAccess to this site is entirely prohibited unless permission has been granted.\r\nAuthenticated with partial success.\r\r\n0725@ftpprd.bank.com's password: "
send: sending "some@$#p&h*apassword" to { exp5 }
send: sending "ls" to { exp5 }
send: sending "bye" to { exp5 }


Script is here:
#!/usr/bin/expect
exp_internal 1
spawn sftp 0725@ftpprd.bank.com
expect "assword: "
send "some@$#p&h*assword\r"
send "put /home/danp/test1.txt"
send "ls"
send "bye"
0
arnoldCommented:
You should always check what response you get to send commands. Because your sequential commands presume the other side is listening when sent.
I.e. Put nay take time
While you presume the remote will buffer the four commands
Use
Expect '>'
I.e wait for prompt before sending the next command.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dan PackerAuthor Commented:
Thank you for your help!

-Dan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.