Created
March 8, 2014 04:34
-
-
Save nhoriguchi/9425402 to your computer and use it in GitHub Desktop.
virsh_exec_single_line.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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