Skip to content

Instantly share code, notes, and snippets.

@nhoriguchi
Created March 8, 2014 04:34
Show Gist options
  • Save nhoriguchi/9425402 to your computer and use it in GitHub Desktop.
Save nhoriguchi/9425402 to your computer and use it in GitHub Desktop.
virsh_exec_single_line.sh
#!/bin/bash
usage() {
echo "Usage: `basename $0` target password logfile <command>"
exit 0
}
while getopts c: OPT
do
case $OPT in
"c" ) export LIBVIRT_DEFAULT_URI=$OPTARG ;;
esac
done
shift $[OPTIND - 1]
LANG=C
[ $# -lt 3 ] && usage
target=$1
pass=$2
logfile=$3
shift 3
cmd="$@"
#echo $cmd
expfile="/tmp/test-expect-script-$$.exp"
cat <<EOF > $expfile
#!/usr/bin/expect
set timeout 100
set target $target
set pass $pass
log_file -noappend $logfile
spawn virsh console --force $target
expect "Escape character is"
send "\n"
expect "login: " {
send "root\n"
expect "Password: "
send "$pass\n"
} "~]# " {
send "\n"
}
send "echo start_exec_given_command > /dev/null\n"
send "$cmd\n"
send "echo end_exec_given_command > /dev/null\n"
send "exit\n"
send "\n"
expect "login: "
send "\n"
expect "login: "
send -- "^]"
interact
EOF
#cat $expfile
expect $expfile > /dev/null
gawk '
BEGIN { window = 0 }
/end_exec_given_command/ { window = 0 }
{ if (window == 1) { print } }
/start_exec_given_command/ { window = 1 }
' $logfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment