Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know questions like this have been asked before, and I have read them all (that I found).

The problem I have is that we have an embedded linux device that we can log into for debug purposes by doing the following:

ssh root@name

Then the password is anything (I usually just hit enter). I don't want to change anything on the embedded device/linux. But I do want to be able to (as part of my script) ssh into it.

My research has shown me that the following can be used (this is copied from another post):

#!/usr/bin/expect -f
spawn ssh user@my.server.com
expect "assword:"
send "mypassword\r"
interact

However with my version of Ubuntu (10.04) I don't have spawn or send commands. I was able to install the "expect" command with sudo apt-get install expect.

Does anyone have a solution? is it possible to install these commands, or is there another way to ssh and get past the "password" step?

Thanks :)

------------- EDIT -----------------

I think I am starting to understand the issue I am having. I have a script that starts with:

#!/bin/bash

Then later I am trying to do:

spawn ssh user@my.server.com
expect "assword:"
send "mypassword\r"
interact

I did not realise that you needed explicitly the:

#!/usr/bin/expect -f

At the start of the script. so I have had to move my code into a seperate file which contains only and exactly:

#!/usr/bin/expect -f
spawn ssh user@my.server.com
expect "assword:"
send "mypassword\r"
interact

... sorry for the mis-understanding on my part, but people answers helped me to twig what I was doing. This is not quite ideal for me as I wanted to pass parameters in to open different ssh connections and I can't pass in parameters to this script starting with "expect -f" :(

share|improve this question

migrated from stackoverflow.com Sep 18 '13 at 11:25

This question came from our site for professional and enthusiast programmers.

2  
Is there any reason you can not use a ssh key? Such as not being able to write to /root? If not please read this. –  Hennes Sep 18 '13 at 11:27
    
and the spawn and send are internal cmds to expect. Once you have it installed correctly, you should be able to above script. Good luck. –  shellter Sep 18 '13 at 11:28
    
@Hennes Actually in this case "yes" there is a reason I can't do that. The embedded linux file system is read only (proceted). Though I can unlock this everything will be over-written when we install the next version (which is very often :( ... but the solution looks good for a future use :) –  code_fodder Sep 18 '13 at 11:50
    
I expect as much when I read "embedded". Otherwise I would just have commented "Upgrade it to something more modern and use ssh keys". (Q: Overwritten as read from flash and unpacked to RAM or union FS?) –  Hennes Sep 18 '13 at 11:52
    
@shellter Hmm... as I mentioned I did sudo apt-get install expect. But send still does not work. I get error like program 'send' can be found in the following packages: mailutils-mh, nmh. What am I doing wrong there? –  code_fodder Sep 18 '13 at 11:53

1 Answer 1

up vote 2 down vote accepted

I think you're trying to run your script as a shell script. After installing expect, run your script like this:

expect -f scrip.exp

If you can't install expect as a normal user, try installing its binaries in one of your directories in $HOME e.g. $HOME/opt/ and call expect from there, although it would depend on your system on how you could manipulate LD_LIBRARY_PATH to add another directory for searching dynamic libraries.

An example of embedded expect:

#!/bin/bash

USER=$1
SERVER=$2
PASSWORD=$3

expect <<EOF
spawn ssh "${USER}@${SERVER}"
expect "assword:"
send "${PASSWORD}\r"
interact
EOF

Run with

bash script.sh user server password
share|improve this answer
    
Thanks for that, your reply helped my to understand what I was doing wrong. Please see my edit. –  code_fodder Sep 18 '13 at 13:07
    
@code_fodder Please see my update. –  konsolebox Sep 18 '13 at 13:13
    
Yeah, thats a nice touch, thanks for that! –  code_fodder Sep 18 '13 at 14:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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