[mythtv-users] help control mythtv via telnet

Florin Andrei florin at andrei.myip.org
Wed Jan 23 00:18:32 UTC 2008

Steve Peters - Priority Electronics wrote:
> ------------------------------------------
> #!/bin/bash
> telnet mythtv-frontend 6546
> jump mythgallery
> exit
> exit 0
> ------------------------------------------
> The idea with that above script is that it just changes my mythfrontend 
> to open the gallery then exit telnet and then exit the script...it 
> didn't work.

What you're looking for is a package called expect. It comes with all 
Linux distros I'm aware of. It can be used to record and "play back" any 
interactive session - telnet, ftp, ssh, you name it. The recording part 
is done by a utility called autoexpect, which creates a .exp script. 
Then you just replay the .exp script with expect.

For some weird reason, in Ubuntu autoexpect is part of the expect-dev 
package, and even then it's not in the $PATH, but it's in 
/usr/share/doc/expect-dev/examples/autoexpect.gz - somebody was smoking 
a lot of bad stuff when they put together the package.
On Fedora / Red Hat / CentOS autoexpect is part of the normal expect 
package and is in $PATH.

After you generate the .exp script, feel free to edit the "expect" 
statements, especially if they're "expect -exact" - usually you only 
need the tail end of the whole string - e.g. do not expect for 25 lines 
of output, but only for the return of the prompt at the end.
That's the only bit of advice that you really need with autoexpect.

Here's a .exp file that records "telnet google.com 80":

#!/usr/bin/expect -f
# This Expect script was generated by autoexpect on Tue Jan 22 16:09:33 2008
# Expect and autoexpect were both written by Don Libes, NIST.
# Note that autoexpect does not guarantee a working script.  It
# necessarily has to guess about certain things.  Two reasons a script
# might fail are:
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts.  If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character.  This
# pacifies every program I know of.  The -c flag makes the script do
# this in the first place.  The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0  ;# set to 1 to force conservative mode even if
                           ;# script wasn't run conservatively originally
if {$force_conservative} {
         set send_slow {1 .1}
         proc send {ignore arg} {
                 sleep .1
                 exp_send -s -- $arg

# 2) differing output - Some programs produce different output each time
# they run.  The "date" command is an obvious example.  Another is
# ftp, if it produces throughput statistics at the end of a file
# transfer.  If this causes a problem, delete these patterns or replace
# them with wildcards.  An alternative is to use the -p flag (for
# "prompt") which makes Expect only look for the last line of output
# (i.e., the prompt).  The -P flag allows you to define a character to
# toggle this mode off and on.
# Read the man page for more info.
# -Don

set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "]0;florin at scout: ~florin at scout:~\$ "
send -- "telnet google.com 80\r"
expect -exact "telnet google.com 80\r
Connected to google.com.\r
Escape character is '^\]'.\r
send -- "HEAD / HTTP/1.1\r"
expect -exact "HEAD / HTTP/1.1\r
send -- "\r"
expect -exact "\r
HTTP/1.1 200 OK\r
Cache-Control: private\r
Content-Type: text/html; charset=ISO-8859-1\r
expires=Fri, 22-Jan-2010 00:09:53 GMT; path=/; domain=.google.com\r
Server: gws\r
Content-Length: 0\r
Date: Wed, 23 Jan 2008 00:09:53 GMT\r
send -- ""
expect -exact "^\]\r
telnet> "
send -- "QUIT\r"
expect -exact "QUIT\r
Connection closed.\r
]0;florin at scout: ~florin at scout:~\$ "
send -- "exit\r"
expect eof

Florin Andrei


More information about the mythtv-users mailing list