功能:

  • 在ubuntu上面,通过shell脚本打开 VirtualBox(windows xp),并调用XP里面的python脚本,来打开一些windows上面的软件,并做模拟操作,完成之后关闭VirtualBox(windows xp)。

环境:

  • ubuntu 10.0.4 
  • Oracle VM VirtualBox(windows xp)
  • windows xp 上安装Python 2.6.5+pywinauto+freesshd+GnuWin32

pywinauto 下载地址:http://sourceforge.net/projects/pywinauto/

GnuWin32-coreutils 下载地址:http://gnuwin32.sourceforge.net/packages/coreutils.htm

 

准备条件:

  • windows xp 上安装freesshd 确保ubuntu能够ssh到windows。

以下提供两种方法设置来实现ubuntu ssh到windows。

  1. 如果VirtualBox虚拟机使用nat模式则可以使用端口转发功能
 
  
  1. VBoxManage modifyvm "VM name" --natpf1 "guestssh,tcp,,2222,,22" 

     2. 使用桥接网络。

  • 使用key认证登录模式,不需要使用密码。

通过key认证ssh windows实现后 则准备工作完成。

贴脚本。

 
  
  1. #--调用入口脚本
  2. function test(){ 
  3.     #use windows  
  4.     echo 'Start VM ...' 
  5.     #VBoxManage startvm "XP" 
  6.     sleep 60 
  7.     VBoxManage list runningvms | grep XP && echo "VM is running" ||  exit 1 
  8.  
  9.     echo 'use windows tools ...' 
  10.     ssh -p 2201 Administrator@localhost "c:\\\\runTools.bat" "${name}" "${pass}" #经测试直接调用python脚本不太稳定,故先调用bat脚本,在有bat调用python脚本。
  11.     echo "wait....use tool for windows VM" 
  12.     sleep 600  
  13.  
  14.     #echo 'Shut Down VM...' 
  15.     VBoxManage controlvm "shreck" acpipowerbutton 
  16.     sleep 60 

windows 上的脚本可以提前拷贝过去,或者使用脚本自动拷贝。

 
  
  1. #--runTools.bat 
  2. python c:\TestAuto.py %1 %2  

主要python实现脚本:

 

 
  
  1. #!/usr/bin/env python 
  2. # -*- coding: utf-8 -*- 
  3.  
  4. from pywinauto import application 
  5. import time 
  6. import os 
  7. import sys 
  8. import glob 
  9.  
  10.  
  11. def main(): 
  12.     if len(sys.argv) != 3: 
  13.         print '%s name pass' % sys.argv[0] 
  14.         sys.exit(1) 
  15.     name = sys.argv[1] 
  16.     pass = sys.argv[2] 
  17.     os.chdir('c:\\'
  18.     os.system('rm -rvf test'
  19.     os.system('md test'
  20.     os.chdir('test'
  21.     os.system('wget -r -nd -np --no-proxy -l1 http://xxxxx') 
  22.  
  23.     os.system('ls -l'
  24.     print 'Start Tools...' 
  25.     app = application.Application() 
  26.     app.start_("C:\Program Files\TestTools\Test.exe") #打开软件
  27.     app.Test.Edit1.SetText('xxxx') #用户名
  28.     app.Test.Edit2.SetText('xxxx') #密码
  29.     app.Test.Button1.Click() #点击登录
  30.     time.sleep(3) 
  31.     app.connect_(title_re='Test.*'
  32.     dlg = app.window_(title_re='Test.*'
  33.     if dlg.CheckBox1.GetCheckState() == 1: #验证是否打开成功
  34.         dlg.CheckBox1.Click() 
  35.     dlg.ComboBox2.Select(name) #打开后进行选择下拉框
  36.     time.sleep(3) 
  37.     dlg['Choose'].Click() #Choose按钮
  38.     time.sleep(3) 
  39.     opendlg = app.top_window_() 
  40.     opendlg.Edit.SetText('c:\\test\file.xxxx') #输入地址路径
  41.     opendlg.Button1.Click() 
  42.     dlg['Start'].Wait('enabled', timeout=600) 
  43.     dlg['Start'].Click() 
  44.     time.sleep(450) 
  45.     os.system('Shutdown -s -t 2') #关机
  46.  
  47.  
  48. if __name__ == '__main__'
  49.     main() 

至此整个windows操作过程结束,可进行后面的shell脚本。