2

I need to create a shell that makes choices on an interactive menu. In the manual version I execute:

- ssh connection to the host
- I select the menu item 4 (console start)
- I run the "show vpn connection" command (it returns a number)
- I type "exit" to close the console
- I select the menu item 0 (exit)

I tried to create a shell of this type:

#!/bin/bash
HOST=172.19.0.9
USER="admin"
PASSWORD="xxxxxxxxxx"
COMMAND="show vpn connection status"
SSH="/usr/bin/ssh"
$SSH -tt $USER@$HOST << EOF
4
$COMMAND
exit
0
EOF

It asks me for the password but no output is returned to me. I only get the message "Connection to $HOST closed." Can anyone help me? Thanks in advance

AlAn It
  • 21
  • 2

2 Answers2

0

So I think this can help you:

Put all of your commands in file, then run

cat file | sshpass -p $yourpassword ssh -T IP_address

this will read the file and pass it as STDIN for the ssh command. sshpass is to go past the login, and ssh -T disables pseudo-tty allocation (Pseudo-terminal will not be allocated because stdin is not a terminal)

Alice
  • 972
  • 1
  • 7
  • 17
  • Hi @Sami and thank, I was already trying this kind of solution, but another problem is generated: **stty: standard input: Inappropriate ioctl for device** Do you have any idea? Many thanks! – AlAn It Oct 10 '19 at 15:43
  • Sorry I have no idea why it does that, it worked fine when i tested that on my computer. Do you own the distant host ? – Alice Oct 11 '19 at 07:14
0

I solved the problem using "expect". Example:

#!/usr/bin/expect -f
spawn ssh <HOST IP> -l admin
expect "password:"
send "<Admin Password>\r"
expect "Select Menu Number \[0-7\]:"
send "7\r"
expect "Shutdown(S/s) or Reboot(R/r) Device  (S/s/R/r):  No (Enter) >"
send "r\r"
expect eof
exit

Thank you all. AlAn

AlAn It
  • 21
  • 2