media/vlc/ UsingExpectWithVlc


This is my basic expect script

#!/usr/bin/expect
set timeout 1

set host "[lindex $argv 0]"
set tport "[lindex $argv 1]"
puts "tport '$tport'"
if { $tport == "" } {
  set tport 0
}
set port "[expr 5000 + $tport]"
set password "password"

spawn gtelnet $host $port
expect {
  timeout { puts "timeout pw" ; exit 1 }
  "Password:"
}
send "$password\r"

expect {
  timeout { puts "timeout welcome" ; exit 1 }
  "Welcome, Master"
}

interact

and because I am extremely lazy, I wrote this python script with shorthands for the host name (I don't know if you can do a dictionary lookup in expect, so I do it in Python and then use os.execv to load the expect script above). In the following, tvlce is the name of the script above.

#!/usr/bin/env python
import sys
import os
args = sys.argv[1:]
d = {
  ".": "localhost",
  "lh": "localhost",
  "l": "landsend",
  "f": "flypaper",
  "b": "bumblebee",
  "g": "gandalf"
}
try:
  p = int(args[1])
  if p < 0 or p > 399:
    print(f"Relative port number out of range (should be 0..399)")
    raise ValueError
except (ValueError,IndexError):
  print(f"{sys.argv[0]} host port")
  exit(1)
if args[0] in d:
  args[0]=d[args[0]]
os.execv(os.path.expanduser("~/bin/tvlce"),["tvlce"]+args)

tvlcmd

This sends individual commands via telnet. Usage

tvlcmd bumblebee 20 pause

the code

#!/usr/bin/env python
from subprocess import run, Popen, PIPE

a = r"""set timeout 1

set host "[lindex $argv 0]"
set tport "[lindex $argv 1]"
puts "tport '$tport'"
if { $tport == "" } {
  set tport 0
}
set port "[expr 5000 + $tport]"
set password "optimus"

spawn gtelnet $host $port
expect {
  timeout { puts "timeout pw" ; exit 1 }
  "Password:"
}
send "$password\r"

expect {
  timeout { puts "timeout welcome" ; exit 1 }
  "Welcome, Master"
}

sleep 0.1
"""

import sys
args = sys.argv[1:]
try:
  host, port, *args = args
  if len(args) == 0:
    raise ValueError()
except ValueError:
  print(f"{sys.argv[0]} <host> <rport> [<cmds>...]")
  exit(1)

d = {
  ".": "localhost",
  "lh": "localhost",
  "l": "landsend",
  "f": "flypaper",
  "b": "bumblebee",
  "g": "gandalf"
}
if host in d:
  host = d[host]

b="sleep 0.1"

args = [arg.replace('"','\"') for arg in args]
args = [f"send \"{arg}\\r\"" for arg in args]
scr="\n".join([a]+args+[b])

proc = Popen(["expect","-f","-",host,port],stdin=PIPE)
proc.communicate(input=scr.encode())
exit(proc.returncode)