Expect isn't working properly - it's not sending commands.

I’ve tried quite a few expect scripts to remotely work on our Mikrotiks, and they seem to be avoiding sending data.

#!/usr/bin/expect
#
spawn /usr/bin/ssh 1.2.3.4 -l admin
expect -re "password:"
send "password\r"
#
expect ">"
send  "system resource print\n"
sleep 2
#
expect -re "] > "
send "print"

I’ve also tried several other scripts, including YazzY’s script. They all show the exact same thing: They log into the Mikrotik properly, and then accomplish nothing. I see no commands or responses sent, and the Mikrotik logs out.

I’ve tried pointing the script at a local Linux server, and it works properly - I see the commands & responses.

Ideas?

-=Russ=-

You might need to manually login once first from the box running the script via ssh as the user your script uses in order to accept the cert…it will prompt for a yes/no the first time you login.

I’ve already taken care of that - it logs in successfully, just… doesn’t do anything after that. I get the full banner and prompt, but that’s it.

-=Russ=-

Try to end the commands that you send with \r instead of \n just like you do when you send the password.

–Tom

Eh, yea. Should have specified above. I’ve tried \n, \r, \r\n, \n\r, pretty much everything I can think of.

Just to confirm, I should be able to see the output coming back when I run the commands, correct? I see the login prompts & banner, so it would make sense to me that I see the output from the commands I’m running.

-=Russ=-

My old script still isn’t working, but I was able to create something that worked with autoexpect and then trim it down to work.

#!/usr/bin/expect -f

spawn ssh 1.2.3.4 -l admin
expect "admin@1.2.3.4's password: "
send -- "password\r"
expect " > "
send -- "system resource print\r"
expect " > "
send -- "queue simple print\r"
expect " > "
send -- "quit\r"
expect eof

This works properly for some reason, and my other one didn’t.

Anyway, apparently, if you’re having trouble, try using autoexpect to log a session & then tweak it’s output.

-=Russ=-