#!/usr/bin/expect if { $argc != 2 } { send_user "Usage: $argv0 guestname cmd\n"; exit 1; } set guestname [lindex $argv 0] set cmd [lindex $argv 1] set prompt "\n(.*?(%|#|\\\$)) $" set user "root" set pass "rhts" set timeout 30 set i 0 log_user 0 #exp_internal 1 spawn virsh console $guestname sleep 3 send "\n" send "\n" expect { -exact "login: " { send "$user\n"; exp_continue; } -re "(P|p)assword:" { send "$pass\n"; sleep 10; send  } timeout { send  } eof { } } spawn virsh console $guestname send "\n" send "\n" sleep 5 expect { -re $prompt { set thelist [split $expect_out(0,string) "\n" ] set theprompt [string trim [lindex $thelist end]] send "$cmd\n"; send ; } timeout { if { $i < 3 } { send "\n" incr i exp_continue } else { send_user "didn't get prompt in $timeout seconds \n"; exit 1; } } eof { send_user "Unexpected EOF ..\n"; exit 1; } } spawn virsh console $guestname expect { -re $prompt { set idx [string first "$theprompt" $expect_out(1,string)] set out [string range $expect_out(1,string) 0 [expr $idx - 1]] send_user "${out}" } timeout { send_user "timeout waiting for the prompt\n" } eof { send_user "eof waiting for the prompt\n" } } exit 0