#!/usr/bin/expect -f
#
# This Expect script was hand generated by BobLfoot on Mon Ju1 12 20:53:00 2010
# Expect and autoexpect were both written by Don Libes, NIST.
#
# It's purpose is to connect to and update the Fedora386Outgoing KVM Domain
#
# Establish the Constants for this Script
# vmname is the Domains Name
set vmname "SomeDomainName"
# domusername is the Domains Standard Users Name
set domusername "User"
# passuer is the Domains Standard Users Password
set passuser "PaSsWoRdHeRe"
# passroot is the Domains Root Users Password
set passroot "RoOtPaSsWoRdHeRe"
# vmip is the Domains Fixed IP address in the DHCP Table
set vmip "ip.to.kvm.domain"
# vmcmd is the super user level command you want the VM to run
# examples are as follow:
# set vmcmd "yum update --enablerepo=updates-testing --skip-broken -y"
set vmcmd "yum install fedora-easy-karma yum-plugin-filter-data --enablerepo=updates-testing --skip-broken -y"
# set vmcmd "ls -laZR /""
#set vmcmd "yum --filter-groups=core,critical-path-\* update --enablerepo=updates-testing -y --skip-broken"

# Boilerplate from Don Libes for conservative mode
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
	}
}

# Real Working Script Starts Here
set timeout -1
spawn $env(SHELL)
sleep .1
# Start target KVM Domain
send -- "virsh start $vmname\r"
expect 	"#" 
sleep 15
# SSH to Started VM
send -- "ssh $domusername\@$vmip\r"
expect {
	"password: " {
	  send -- "$passuser\r"
	}
	"No route to host" {
	  send -- "echo Waiting 15 More Seconds for $vmname to Boot\r"
	  sleep 15
	  send -- "ssh $domusername\@$vmip\r"
	  exp_continue
	}
	"timed out" {
	  send -- "echo Waiting 15 More Seconds for $vmname to Boot\r"
	  sleep 15
	  send -- "ssh $domusername\@$vmip\r"
	  exp_continue
	}
	"refused" {
	  send -- "echo Waiting 15 More Seconds for $vmname to Boot\r"
	  sleep 15
	  send -- "ssh $domusername\@$vmip\r"
	  exp_continue
	}
	"yes/no" {
	  send -- "yes\r"
	  sleep .1
	  exp_continue
	}}
sleep .1
expect "$ "
send -- "su\r"
sleep .1
expect "Password: "
send -- "$passroot\r"
sleep 1
expect "#"
send -- "$vmcmd\r"
expect "#"
send -- "exit\r"
sleep .1
expect "$ "
send -- "exit\r"
sleep .1
expect "# "

# Stop the Target KVM Domain
send -- "virsh shutdown $vmname\r"
sleep 15
expect "# "



