0

I need a script to configure PDU (power distribution unit). The script will be called from another program. I created Expect script that gets parameters, connects to PDU by ssh and runs the command. The short commands, such as 'pwd' or 'lang' works correctly, but the long commands stuck waiting for user interaction. If I do nothing, the connection is closed after timeout, if I press Enter, the script continue and pass.

There is a script:

spawn ssh $user@$pdu
interact -o -nobuffer "assword:" return
send "$password\r"
interact -o -nobuffer "apc>" return
send "olStatus $host\r"
interact -o -nobuffer "apc>" return

Running it with debug flag gave the following output

apc>tty_set: raw = 0, echo = 1
send: sending "olStatus machine-123456\r" to { exp4 }
defining key apc>, action return
tty_raw_noecho: was raw = 0  echo = 1
spawn id exp4 sent <olStatus machine-123>
olStatus machine-123spawn id exp0 sent <\r>
spawn id exp4 sent <456\r\n23: machine-123456: On \r\nE000: Success\r\n\r\napc>\r\napc>>
456
23: machine-123456: On 
E000: Success

Here I added the comments Output

How can I solve the problem?

Nina
  • 11
  • 2
  • 1
    Any reason you're not using the more straightforward `expect "assword:"` and `expect "apc>"` ? – glenn jackman Mar 11 '21 at 14:25
  • @glennjackman, I tried `expect` too, but the script has stuck on password line. So I looked for solution and found it [here](https://stackoverflow.com/questions/4780893/use-expect-in-a-bash-script-to-provide-a-password-to-an-ssh-command?answertab=votes#tab-top) – Nina Mar 14 '21 at 09:23

2 Answers2

0

This is what I'd suggest:

spawn ssh $user@$pdu
expect "assword:"
send "$password\r"
expect -re {apc>$}
send "olStatus $host\r"
expect -re {apc>$}
send "exit\r"           # or "quit" or whatever
expect eof

But run it with debug enabled and let me know what you get.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thanks for reply. This is what I got: ``` send: sending "olStatus machine-123456\r" to { exp4 } Gate keeper glob pattern for 'apc>$' is 'apc>'. Activating booster. expect: does "" (spawn_id exp4) match regular expression "apc>$"? Gate "apc>"? gate=no olStatus machine-123 expect: does "olStatus machine-123" (spawn_id exp4) match regular expression "apc>$"? Gate "apc>"? gate=no expect: timed out usage: send [args] string while executing "send "exit\r" # or "quit" or whatever" (file "./pdu.sh" line 41) ``` – Nina Mar 14 '21 at 15:23
  • If you do this interactively, do you have to hit enter twice? Try sending `olStatus $host\r\r` – glenn jackman Mar 14 '21 at 22:28
  • @nina change `# or "quit" or whatever` to `; # or "quit" or whatever` – pynexj Mar 15 '21 at 04:01
  • Nothing helps :( It sends a maximum of 20 chars: `olStatus machine-123` and after this `456\r` Any more ideas, please? – Nina Mar 15 '21 at 09:16
  • My last suggestion: `autoexpect` -- run autoexpect, then manually perform the login and status commands. autoexpect will record the session. It's not a clean expect script, but there may be something in there you notice – glenn jackman Mar 15 '21 at 12:55
0

Thanks to @GlennJackman I ran autoexpect and this help me to understand how to solve the issue. Maybe it is a my system problem, but currently it is not matter.

What needed to be done was to send in two steps

send "olStatus "
expect "olStatus"
send "machine-123456\r"
expect -re {apc>$}
Nina
  • 11
  • 2