vmopt 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,120 @@
1
+ #encoding: utf-8
2
+ require "vmopt/utils/wmi"
3
+ require "au3"
4
+ require "Win32API"
5
+ module Vmopt
6
+ class DVDOperation
7
+ =begin
8
+ 参数:无
9
+ 功能:获取光驱的基本信息
10
+ 返回值:默认
11
+ =end
12
+ def get_dvd_information()
13
+ data_value={}
14
+ colItems = WMI.execquery ("select * from Win32_CDROMDrive")
15
+ for colItem in colItems do
16
+ str={"介质类型"=>colItem.MediaType,
17
+ "盘符"=> colItem.Drive,
18
+ "描述信息"=>colItem.Description
19
+ }
20
+ data_value["#{colItem.Drive}"]=str
21
+ end
22
+ return data_value
23
+ end
24
+
25
+ =begin
26
+ 参数:无
27
+ 功能:弹出光驱
28
+ 返回值:默认
29
+ =end
30
+ def eject_dvd(str1="set cdauto door open",str2="",num1=0,num2=0)
31
+ mci = Win32API.new("winmm.dll","mciSendString",%w(p p i i),"i")
32
+ mci.call(str1,str2,num1,num2)
33
+ end
34
+
35
+ =begin
36
+ 参数:无
37
+ 功能:关闭光驱与弹出光驱对应
38
+ 返回值:默认
39
+ =end
40
+ def close_dvd(str1="set cdauto door closed",str2="",num1=0,num2=0)
41
+ mci = Win32API.new("winmm.dll","mciSendString",%w(p p i i),"i")
42
+ mci.call(str1,str2,num1,num2)
43
+ end
44
+
45
+ =begin
46
+ 参数:无
47
+ 功能:禁用系统所有的光驱设备
48
+ 返回值:默认
49
+ =end
50
+ def disable_all_dvd
51
+ system("devcon.exe disable *DVD* >null 2>&1")
52
+ end
53
+
54
+ =begin
55
+ 参数:盘符号例如D:
56
+ 功能:禁用指定盘符的光驱设备
57
+ 返回值:默认
58
+ =end
59
+ def disable_index_dvd(str)
60
+ colItems = WMI.execquery ("select * from Win32_CDROMDrive where Drive='#{str}'")
61
+ for colItem in colItems do
62
+ strID = colItem.DeviceId
63
+ system("devcon.exe disable @#{strID} >null 2>&1")
64
+ end
65
+ end
66
+
67
+ =begin
68
+ 参数:无
69
+ 功能:启用系统所有的光驱设备
70
+ 返回值:默认
71
+ =end
72
+ def enable_all_dvd
73
+ system("devcon.exe enable *DVD* >null 2>&1 ")
74
+ end
75
+
76
+
77
+ =begin
78
+ 参数:无
79
+ 功能:删除或卸载统所有的光驱设备
80
+ 返回值:默认
81
+ =end
82
+ def remove_all_dvd
83
+ system("devcon.exe remove *DVD* >null 2>&1")
84
+ end
85
+
86
+ =begin
87
+ 参数:盘符号例如D:
88
+ 功能:删除或卸载指定盘符的光驱设备
89
+ 返回值:默认
90
+ =end
91
+ def remove_index_dvd(str)
92
+ colItems = WMI.execquery ("select * from Win32_CDROMDrive where Drive='#{str}'")
93
+ for colItem in colItems do
94
+ strID = colItem.DeviceId
95
+ system("devcon.exe remove @#{strID} >null 2>&1")
96
+ end
97
+ end
98
+ =begin
99
+ 参数:无
100
+ 功能:自动添加或者挂载所有光驱设备
101
+ 返回值:默认
102
+ =end
103
+ def mount_all_dvd
104
+ system("devcon.exe rescan >null 2>&1")
105
+ end
106
+
107
+ =begin
108
+ 参数:盘符号例如D:
109
+ 功能:打开光盘里的内容
110
+ 返回值:默认
111
+ =end
112
+ def read_dvd(str)
113
+ #AutoItX3.run("explorer.exe #{str}")
114
+ system("dir #{str} >null 2>&1")
115
+ end
116
+ end
117
+
118
+ end #module vmopt
119
+
120
+
@@ -0,0 +1,104 @@
1
+ # encoding: utf-8
2
+ require 'iconv' if RUBY_VERSION.to_f < 1.9
3
+ class String # :nodoc: all
4
+ #判断是否为utf8
5
+ def utf8?
6
+ if String.method_defined?(:encode)
7
+ return true if self.force_encoding("UTF-8").valid_encoding?
8
+ return false
9
+ else
10
+ unpack('U*') rescue return false
11
+ return true
12
+ end
13
+ end
14
+
15
+ #TODO:参数功能目前没有用
16
+ def to_utf8(enc='GBK')
17
+ if self.respond_to?(:encode)
18
+ return self if utf8?
19
+ self.encode('UTF-8',enc)
20
+ else
21
+ require 'iconv' if RUBY_VERSION.to_f < 1.9
22
+ return self if utf8?
23
+ begin
24
+ Iconv.iconv('UTF-8',enc,self).at(0).to_s
25
+ rescue
26
+ self
27
+ end
28
+ end
29
+ end
30
+
31
+ #TODO:参数功能目前没有用
32
+ def to_gbk(enc='UTF-8')
33
+ if self.respond_to?(:encode)
34
+ return self unless utf8?
35
+ self.encode('GBK', enc)
36
+ else
37
+ return self unless utf8?
38
+ require 'iconv' if RUBY_VERSION.to_f < 1.9
39
+ begin
40
+ Iconv.iconv('GBK',enc,self).at(0).to_s
41
+ rescue
42
+ self
43
+ end
44
+ end
45
+ end
46
+
47
+ def to_gb2312(enc = 'UTF-8')
48
+ # require 'iconv'
49
+ # Iconv.iconv('GB2312','UTF-8',self).at(0).to_s
50
+ if self.respond_to?(:encode)
51
+ self.encode('GB2312', enc)
52
+ else
53
+ begin
54
+ self.unpack("U*")
55
+ require 'iconv' if RUBY_VERSION.to_f < 1.9
56
+ Iconv.iconv('GB2312',enc,self).at(0).to_s
57
+ rescue ::ArgumentError
58
+ self
59
+ end
60
+ end
61
+ end
62
+ def to_arr()
63
+ self.split('&') #暂时未考虑&在\之后的问题,fix this bug
64
+ end
65
+
66
+ def __camelize(first_letter_in_uppercase=true)
67
+ if first_letter_in_uppercase
68
+ self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
69
+ else
70
+ self.to_s[0].chr.downcase + camelize(self)[1..-1]
71
+ end
72
+ end
73
+ alias camelize __camelize
74
+
75
+ def __underscore
76
+ word = self.to_s.dup
77
+ word.gsub!(/::/, '/')
78
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
79
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
80
+ word.tr!("-", "_")
81
+ word.downcase!
82
+ word
83
+ end
84
+ alias underscore __underscore
85
+
86
+ end
87
+
88
+ class << nil
89
+ def to_utf8
90
+ ""
91
+ end
92
+
93
+ end
94
+
95
+
96
+ if $0 == __FILE__
97
+ # require 'string'
98
+ puts "中文123中文".to_gbk.dump
99
+ puts "中文123中文".to_gb2312.dump
100
+ puts File.join("中文abc/abc","中文").to_gbk.dump
101
+ puts File.join("中文abc/abc","中文").to_gb2312.dump
102
+ puts 'ab_c'.camelize
103
+ end
104
+
@@ -0,0 +1,128 @@
1
+ #encoding: utf-8
2
+ #$:.unshift File.join(__FILE__,"..","..")
3
+ require "vmopt/windows/win_net"
4
+ require "vmopt/ext/string_ext"
5
+
6
+ module Vmopt
7
+ class NetWork
8
+
9
+ #功能:显示可用网络接口
10
+ #返回:返回接口名称数组
11
+ def self.show_netinterface()
12
+ WinNet.interfaces
13
+ end
14
+
15
+ #功能:检查网络接口的连接状态
16
+ #返回:返回状态字串
17
+ def self.show_interface_stat(interface)
18
+ adapter(interface.to_utf8);
19
+ WinNet.netconnstatus[interface.to_utf8]
20
+ end
21
+
22
+ #功能:检查接口是否存在,返回接口名对应的适配器
23
+ def self.adapter(interface)
24
+ interface = interface.to_utf8
25
+ raise WinNetError::NoInterfaceError "Give me no NetWorkinterface" if interface.nil? or interface.empty?
26
+ adapter =WinNet.network_adapter.select do |inf|
27
+ inf.NetConnectionId == interface ;
28
+ end
29
+ raise WinNetError::NoInterfaceError "Find no interface #{interface}" if adapter.empty?
30
+ adapter[0]
31
+ end
32
+
33
+ #功能:执行devcon命令
34
+ def self.devcon_exec(cmd)
35
+ status = system("#{cmd} 1>&2");
36
+ status
37
+ end
38
+
39
+ #功能:给定一个网卡接口名,卸载网卡
40
+ #例子:adapter_opt("本地连接 2","禁用")
41
+ def self.adapter_opt(interface, opt)
42
+ pci = adapter(interface).PNPDeviceID;
43
+ optt=case opt.to_utf8
44
+ when "卸载" then "remove"
45
+ when "禁用" then "disable"
46
+ when "启用" then "enable"
47
+ else raise WinNetError::InvalidParamError "Unkown option for adapter.-#{opt}"
48
+ end
49
+
50
+ cmd = "devcon.exe /r #{optt} \"@#{pci}\" "
51
+ devcon_exec(cmd)
52
+ end
53
+
54
+ # #功能:安装一个网卡,暂时不做了,用户需要获取网卡的描述
55
+ # def self.adapter_install()
56
+ # devcon /r install C:\windows\system32\drivers\rtl8139.sys 硬件ID
57
+ # end
58
+
59
+ #功能:扫描硬件改动
60
+ #
61
+ def self.adapter_rescan()
62
+ cmd = "devcon.exe rescan"
63
+ devcon_exec(cmd)
64
+ end
65
+
66
+ #功能:返回网卡描述信息,用来判断网卡型号
67
+ def self.adapter_discript()
68
+ description=[]
69
+ WinNet.network_adapter_configurations.each do |interface|
70
+ description << interface.description
71
+ end
72
+ description
73
+ end
74
+
75
+ #功能:设置网卡的配置模式为dhcp
76
+ def self.adapter_set_dhcp(interface)
77
+ adapter(interface);
78
+ interface = "\""+interface.to_gbk+"\""
79
+ ipset_cmd = "netsh interface ip set address name=#{interface} source=dhcp".to_gbk
80
+ dnsset_cmd = "netsh interface ip set dns name=#{interface} source=dhcp".to_gbk
81
+ ipset_status = system("#{ipset_cmd} >null 2>&1");
82
+ dnsset_status = system("#{dnsset_cmd} >null 2>&1");
83
+
84
+ ipset_status & dnsset_status
85
+
86
+ end
87
+
88
+ #功能:设置网卡为配置的参数
89
+ def self.adapter_set_static(interface, opt={})
90
+ adapter(interface);
91
+ interface = "\""+interface.to_gbk+"\""
92
+ ipset_cmd = "netsh interface ip set address name=#{interface} source=static addr=#{opt[:addr]} mask=#{opt[:mask]}".to_gbk
93
+ gwset_cmd = "netsh interface ip set address name=#{interface} gateway=#{opt[:gateway]} gwmetric=0".to_gbk
94
+ dnsset_cmd = "netsh interface ip set dns name=#{interface} source=static addr=202.106.128.86 register=primary".to_gbk
95
+ dns2set_cmd = "netsh interface ip add dns name=#{interface} addr=8.8.8.8 index=2".to_gbk
96
+
97
+ ipset_status = system(ipset_cmd)
98
+ gwset_status = system(gwset_cmd)
99
+ dns_status = system(dnsset_cmd)
100
+ dns2_staus = system(dns2set_cmd)
101
+
102
+ ipset_status & dns_status & dns2_staus & gwset_status
103
+
104
+ end
105
+
106
+ end
107
+ end
108
+
109
+ if __FILE__ == $0
110
+
111
+ Vmopt::NetWork.show_netinterface.each { |e| puts (e) }
112
+ Vmopt::NetWork.show_netinterface.each do |inf|
113
+ puts "#{inf} :" + Vmopt::NetWork.show_interface_stat(inf)
114
+ end
115
+ Vmopt::NetWork.adapter_opt("本地连接 4", "禁用")
116
+ Vmopt::NetWork.adapter_opt("本地连接 4", "启用")
117
+ #Vmopt::NetWork.adapter_opt("本地连接 3", "卸载")
118
+ # Vmopt::NetWork.adapter_rescan
119
+ # sleep 10
120
+ Vmopt::NetWork.adapter_discript.each{|d| puts (d) }
121
+
122
+ Vmopt::NetWork.adapter_set_static("本地连接 4", {:addr => "192.168.12.10", :mask => "255.255.252.0", :gateway => "192.168.12.1"})
123
+ Vmopt::NetWork.adapter_set_dhcp("本地连接 4")
124
+
125
+ end
126
+
127
+
128
+
@@ -0,0 +1,145 @@
1
+ # encoding: utf-8
2
+ #$:.unshift File.join(__FILE__,"..","..")
3
+ require "vmopt/windows/win_winutils"
4
+
5
+ module Vmopt
6
+
7
+ class NotePad
8
+
9
+ # 功能:
10
+ # 1.初始化一个notepad程序,如果给定路径,则打开特定路径下的文件,同时激活该窗口
11
+ # 2.如果不给定路径,则打开notepad,同时激活该窗口。
12
+ # 参数:
13
+ # txt_path: 文件路径
14
+ # open_window: 表示需不需要创建该notepad窗口,默认创建,不设置表示获取该窗口
15
+ #
16
+
17
+ def initialize(opt)
18
+
19
+ if opt[:txt_path].nil?
20
+ @title = "记事本"
21
+ else
22
+ basename = File.basename(opt[:txt_path]).to_utf8
23
+ @title = "#{basename} - 记事本"
24
+ @path = opt[:txt_path]
25
+ end
26
+
27
+ if opt[:open_window]
28
+ winexe = Win32API.new('kernel32', 'WinExec', 'PI', 'I')
29
+ opt[:txt_path].nil? ? winexe.call("notepad.exe", 1) : winexe.call("notepad.exe #{opt[:txt_path]}", 1)
30
+ sleep 1
31
+ end
32
+
33
+ @window = WinUtils::find_window(/#{@title}/)
34
+ RAutomation::Window.wait_timeout=40
35
+
36
+ end
37
+
38
+ # 功能:
39
+ # 1.对一个激活的窗口设置文本内容
40
+ # 参数:
41
+ # msg:表示即将填充的信息
42
+ def set_text(msg)
43
+ @window.text_field(:class => "Edit", :id => 15).set(msg)
44
+ end
45
+
46
+ # 功能:读取一个已经打开的notepad的里面的内容
47
+
48
+ def read_text
49
+ @window.text_field(:class => "Edit", :id => 15).value
50
+ end
51
+
52
+ #功能:点击菜单中的保存按钮来保存文件
53
+ #参数:
54
+ # filepath:
55
+ # 如果指定了filepath 就表示要另外保存在filepath所在文件
56
+ # 如果没有指定filepath 而且 初始化时指定了path,那就只按保存键
57
+ # 如果初始化时没有指定path,要保存时又不提供路径,报异常
58
+
59
+ def save(filepath=nil)
60
+ if !filepath.nil?
61
+ @window.WinMenuSelectItem("[CLASS:Notepad]", "", "文件", "另存为")
62
+
63
+ savewindow = WinUtils::find_window(/另存为/)
64
+ savewindow.text_field(:class => "Edit").set(filepath)
65
+ #窗口关闭,遍历button会导致卡死,
66
+ # savewindow.buttons.each do|button|
67
+ # if button.value == "保存(&S)";
68
+ # button.click;
69
+ # end
70
+ # end
71
+
72
+ savewindow.send_keys("{ENTER}")
73
+ sleep 1
74
+ elsif !@path.nil? and filepath.nil?
75
+ @window.WinMenuSelectItem("[CLASS:Notepad]", "", "文件", "保存")
76
+ elsif @path.nil? and filepath.nil?
77
+ raise WinUtils::NoSavePathError,"Not found the save path ."
78
+ end
79
+ end #end of save
80
+
81
+ #功能:关闭记事本
82
+ def close
83
+ @window.WinClose("[CLASS:Notepad]", "")
84
+ end #end of close
85
+
86
+ end #end of class NotePad
87
+
88
+ end #end of module vmopt
89
+
90
+ if __FILE__ == $0
91
+
92
+ #File.delete("C:\\1.txt") if FileTest::exist?("C:\\1.txt")
93
+ notepad = Vmopt::NotePad.new({:open_window => true, :txt_path => "C:\\1.txt"})
94
+ #notepad.set_text("hello world")
95
+ puts notepad.read_text
96
+ #notepad.save("C:\\1.txt")
97
+ #notepad.close
98
+
99
+ end
100
+
101
+
102
+ #------使用 au3 的实现----先放着后续可能复用---
103
+ #
104
+ # module Vmopt
105
+ # class NotePad
106
+ # def initialize()
107
+ # AutoItX3.run("notepad.exe")
108
+ # AutoItX3::Window.wait("无标题 - 记事本")
109
+ # @window = AutoItX3::Window.new("无标题 - 记事本")
110
+ # end
111
+
112
+ # def close
113
+ # @window.close
114
+ # end
115
+
116
+ # def set_text(msg)
117
+ # @window.activate
118
+ # edit = @window.focused_control
119
+ # edit.text = msg
120
+ # end
121
+
122
+ # def read_text
123
+ # @window.activate
124
+ # edit = @window.focused_control
125
+ # edit.text
126
+ # end
127
+
128
+ # def save_to(filepath)
129
+ # @window.activate
130
+ # @window.select_menu_item("文件", "另存为")
131
+ # window_save = AutoItX3::Window.new("另存为")
132
+ # window_save.activate
133
+ # window_save.focused_control.text = filepath
134
+ # AutoItX3::send_keys("{enter}")
135
+
136
+ # end
137
+
138
+ # end
139
+ # end
140
+
141
+ # notepad = Vmopt::NotePad.new()
142
+ # notepad.set_text("adfhj")
143
+ # puts notepad.read_text
144
+ # notepad.save_to("C:\\1.txt")
145
+ # notepad.close