os/linux/ ExpectUtility


See here (summarised below)

VLC example

mute audio

This sets the volume to 0 using volume 0.

set timeout 5

set host "HOST"
set port "7010"
set password "mrflibble"

spawn telnet $host $port
expect "Password:"
send "$password\r"

expect "Welcome, Master"
send "volume 0\r"
sleep 0.01

and this is a pure script to login with my default password

#!/usr/bin/expect
set timeout 5

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

spawn telnet $host $port
expect "Password:"
send "$password\r"

expect "Welcome, Master"

interact

vlccmd

This assumes that vlc has been started with --telnet-port=7000 --telnet-password=mrflibble and uses a python script to assemble the script which is passed to expect via its stdin

#!/usr/bin/env python3
import sys
import os
from subprocess import run, PIPE, Popen
import time
scr = """
set timeout 5

set host "HOST"
set port "PORT"
set password "PASSWORD"

spawn telnet $host $port
expect "Password:"
send "$password\\r"

expect "Welcome, Master"
"""
args = sys.argv[1:]
try:
  host,port,*xs = args
except ValueError:
  print(f"vlccmd <host> <port-offset> [<cmd> ...]")
  exit(1)
port = int(port)
if port < 0 or port >= 1000:
  print(f"Invalid port: {port}")
  exit(1)
port = str(port+7000)
s = scr.replace("HOST",host).replace("PORT",port).replace("PASSWORD","mrflibble")
for x in args:
  s += f'\nsend "{x}\\r"\nsleep 0.01\n'
with Popen(["expect"],stdin=PIPE) as p:
  p.communicate(input=s.encode())

Geek Stuff article

From this Geek Stuff article by Balakrishnan Mariyappan on January 18, 2011

The Expect scripting language is easy to learn. It expects a specific string, and sends (or responds) strings accordingly.

Define Expect Variables – set command

In expect, you can set the variable values using set command as shown below,

# To set the variable with numeric values
set var1 10

# To set the variable with string literal
set name "john"

# To set the variable value which includes output of another command
set proc_id "process id : [pid]"

Note: For expect command line arguments, read 6 Expect Script Command Line Argument Examples.

Expect Expressions – expr command

To evaluate the expressions, use the expr command, which executes the given expression and returns the result. Expect expressions are similar to the C expressions. Some of the valid expressions are listed below.

# To add two simple numerical values
set sum "[expr 1 + 1]"

# To multiple the value of variables
set mul "[expr $sum * $sum]"

# To evaluate conditions and returns 1 or 0 to indicate success or failure accordingly.
set ret "[expr (1+1) == 2]"

# Conditions may contain the command return values.
set ret [expr [pid] == 0]

Expect Conditional Expressions – If command

If command is used for conditional flow of execution of statements as shown in the example below.

if { $count < 0} {
    puts "True : $count\n";
} else {
    puts "False : $count\n";
}

Just like any other programming language, you can use the elseif command in expect as shown below.

if { $count < 0} {
    puts "Success Condition1 : $count\n";
} elseif { $count == 0 } {
    puts "Success Condition2 : $count\n";
} else {
    puts "False : $count\n";
}

Expect Looping Constructs

Expect For Loop Examples:

As we know, for loop is used to do repeated execution of expression until certain condition.

General for loop construct :


for {initialization} {conditions} {incrementation or decrementation} {
...
}

Expect for loop example :

for {set i 1} {$i < $no} {incr i 1} {
 set $total [expr $total * $i ]
}
puts "$total";

Note: You should place the loop open brace in the same line as it contains “for” keyword.

Expect While Loop Examples:

set count 5;
while {$count > 0 } {
puts "count : $count\n";
set count [expr $count-1];
}