封装一个sendkey函数向虚拟机一次性发送字符串

QEMU 提供一个 sendkey的 Monitor 命令,用来向虚拟机发送单个字符,或者组合键。之所以只支持单个字符,是因为这里需要对空格、ctrl,回车等进行转换,也需要支持keycode的输入。
libvir的virsh调用QEMU的sendkey命令为上层提供类似的功能。但有一个新的需求就是一次输入连续的字符串。我们可以从三个不同层面来实现这个需求,分别是QEMU、libvirt,还有用户自己的脚本、程序里。

这里我跟倾向从用户程序封装出一个转换函数,实现一次连续输入,这样也更加灵活一些,方便扩展。因为QEMU、libvirt接受的多种类型的KEY,如果再把字符转换加进去,会导致接口复杂,语义表达带来冲突。

[amos@amosk qemu]$ cat sendkey.sh 
DOM=rhel6u5_x64

# 封装一下sendkey()函数,调用virsh send-key 命令想虚拟机发送字符串
function sendkey() {
    str=$@
    length=`expr length "$str"`
    for ((i=1; i<=$length; i++)); do
        char=`expr substr "$str" $i 1`
        if [ "$char" = " " ];then
            char="spc"
        fi
        echo virsh send-key $DOM "$char"
    done
}

sendkey "root"
echo virsh send-key $DOM kp_enter
sendkey "shutdown -h now"
echo virsh send-key $DOM kp_enter
[amos@amosk qemu]$ bash sendkey.sh 
virsh send-key rhel6u5_x64 r
virsh send-key rhel6u5_x64 o
virsh send-key rhel6u5_x64 o
virsh send-key rhel6u5_x64 t
virsh send-key rhel6u5_x64 kp_enter
virsh send-key rhel6u5_x64 s
virsh send-key rhel6u5_x64 h
virsh send-key rhel6u5_x64 u
virsh send-key rhel6u5_x64 t
virsh send-key rhel6u5_x64 d
virsh send-key rhel6u5_x64 o
virsh send-key rhel6u5_x64 w
virsh send-key rhel6u5_x64 n
virsh send-key rhel6u5_x64 spc
virsh send-key rhel6u5_x64 -
virsh send-key rhel6u5_x64 h
virsh send-key rhel6u5_x64 spc
virsh send-key rhel6u5_x64 n
virsh send-key rhel6u5_x64 o
virsh send-key rhel6u5_x64 w
virsh send-key rhel6u5_x64 kp_enter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.