bashexpectsystemdpbx

linux expect in background


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


Solution

  • 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