0

I have an expect script that logs into my Beaglebone:

    #!/usr/bin/expect -f
    spawn ssh debian@192.168.7.2
    expect "debian@192.168.7.2's password:"
    send "temppwd\r"
    interact &&
    mkdir emma &&
    cd emma

This works and it logs into the debian account. However, it stops after interact the other two commands are not being executed. What do I have to do to do that ?

EDIT

ok so thanks andy256 I figured that interact is wrong here, however, I get

invalid command name "mkdir" while executing

How can I combine an expect script with normal shell script?

Thanks in advance !

  • 1
    I guess because the interact command gives control of the current process to the user. – andy256 Nov 29 '15 at 4:46
  • ah okk, I took it out but then I get a command unknown error for mkdir – Paul Bernhard Wagner Nov 29 '15 at 4:51
  • 1
    Why not adding your public key to the server to have password less login and run your command with ssh debian@192.168.7.2 <command>? Would be lot easier than fiddling with expect – Germar Nov 29 '15 at 5:21
  • I tried that with debian@192.168.7.2 ' "temppwd\r" ' but didn't work – Paul Bernhard Wagner Nov 29 '15 at 5:45
  • it stops at the pw question – Paul Bernhard Wagner Nov 29 '15 at 5:45
4

The main thrust of expect programming is send and expect pairs: you send some text to the spawned process and expect a response. In this case, you send the mkdir command, and expect to see your prompt to know that the command has completed. Prompts are best matched as regular expressions to match the end of it. Since prompts are so configurable, you might want to edit the prompt expression: this one matches a literal dollar sign and a space at the end of the string.

#!/usr/bin/expect -f
spawn ssh debian@192.168.7.2
expect "debian@192.168.7.2's password:"
send "temppwd\r"
set prompt_re {\$ $}
expect -re $prompt_re
send "mkdir -p emma && cd emma\r"
expect -re $prompt_re
interact

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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