0

I login our company server by ssh, but the server ask my password and otp token. Since i know my otp secret, so i can generate my otp in my script.

the prompt looks like this:

$ ssh mike@relay.office.com
Your password: 
Your token:

let's assume my password is "123", and token is "456" I wrote a expect script like this, which is supposed to work

#!/usr/bin/expect

spawn ssh mike@relay.office.com
expect "password:"
send "123\n"
send "456\n"
interact

However the prompt just like this:

$ ./expect.sh
Your [EMAIL] password: 
Your [VPN] token: 
^[[?65;1;9cInvalid credentials

You see, the ssh server return some massy code followed by "Invalid credentials" I don't understand where the messy code came from. In normal situation. Even i type the wrong password or token, it will just prompt "Invalid credentials" without any messy code. Is it some sort of anti-script login method from our ssh server?

Some Supplyment

Per answer below, i need to clarify, i forget to paste expect line in my script before. But even with it, the problem stands still.

And another thing i didn't mention before, but maybe related. when i type ssh mike@xxx.com. the server returned a QR code.(yeah, QR code in terminal.) it contains color escape as well as some unicode characters. I'm not sure if expect has any bug on handling unicode characters

demonguy
  • 1,977
  • 5
  • 22
  • 34

1 Answers1

0

The "messy code" is a terminal escape sequence, probably intended to change the colour or font of the following error message.

But the main problem here is that as written, your script will launch the ssh command and then immediately send the following lines, without waiting for ssh to connect to the remote machine. You should use expect commands to wait for each prompt before sending the response, e.g.

spawn ssh mike@relay.office.com
expect "password:"
send "123\n"
expect "token:"
send "456\n"
interact
Colin Macleod
  • 4,222
  • 18
  • 21
  • well, i forget to paste the `expect` line. but the problem stands still – demonguy Feb 07 '22 at 02:13
  • 1
    On the question, you only have expect password. Check here because there's another expect for token, and that's the key. I can tell you expect works OK with SSH. – Sakura Kinomoto Feb 07 '22 at 09:13