1

I use the following bash script to connect to pbx using telnet:

expect.sh:

#!/usr/bin/expect 
    spawn telnet [ip] 2300
    expect -exact "-"
    send "SMDR\r";
    expect "Enter Password:"
    send "PASSWORD\r";
    interact

and created another script to redirect the result to a file:

#!/bin/bash
    ./expect.sh | tee pbx.log

I'm trying to run expect.sh at boot time so I added it to systemd. When I add it as service in /etc/systemd/system it runs but I can't get the results in the log file as if I run both scripts manually any idea about how can I run it at boot time? TIA

Dominique
  • 16,450
  • 15
  • 56
  • 112
Hisham
  • 65
  • 1
  • 2
  • 11
  • The Expect script ends with `interact`. That means it should be run from a terminal so you can start typing commands manually to send to the remote device. – Barmar Jul 05 '19 at 20:00
  • If you run it from `systemd`, it won't be able to read any input, so `telnet` will immediately disconnect after sending the password and there won't be any output. – Barmar Jul 05 '19 at 20:03
  • so, how to connect to the device at boot? – Hisham Jul 05 '19 at 20:30
  • Connect to it for what purpose? – Barmar Jul 05 '19 at 20:34
  • to print SMDR to log file – Hisham Jul 05 '19 at 20:36
  • So put a `send` command that sends the command to print the SMDR in `expect.sh`, in place of `interact`. Then wait for the next prompt with an `expect` command. – Barmar Jul 05 '19 at 20:39
  • well, when I send "PASSWORD\r"; SMDR data start print in terminal, so no command needed – Hisham Jul 05 '19 at 20:44
  • Then maybe you just need an `expect` command to wait for it to finish. Otherwise, the script will end because there's no user input available. – Barmar Jul 05 '19 at 20:46
  • exactlly, I basically just need to wait for the prompt, but never happen because it will run forever waiting for new SMDR data, even I can't kill the connection, I have to close the terminal window : ) – Hisham Jul 05 '19 at 20:52
  • Ahh, it's not an interactive device, it just prints continuous output. You just need Expect to print all the output without closing the session. There must be a way to do it, but I don't know Expect well enough. – Barmar Jul 05 '19 at 21:05
  • @Barmar Thank you any way – Hisham Jul 05 '19 at 21:34
  • Instead of teeing the output, you can use expect's builtin `log_file` command. – glenn jackman Jul 06 '19 at 14:19
  • take a look at [sexpect (Expect for Shells)](https://github.com/clarkwang/sexpect) which you can use to write *Expect* scripts with **shell code only**. –  Jul 07 '19 at 03:04

1 Answers1

2

If you just want to permanently output everything received after providing your password, simply replace your interactive with expect eof, i.e. wait for end-of file which will happen when the connection is closed by the other end. You will probably also want to change the default timeout of 10 seconds with no data that will stop the command:

set timeout -1 
expect eof
meuh
  • 11,500
  • 2
  • 29
  • 45