Class: Vmopt::NetWork
- Inherits:
-
Object
- Object
- Vmopt::NetWork
- Defined in:
- lib/vmopt/network.rb
Class Method Summary collapse
-
.adapter(interface) ⇒ Object
功能:检查接口是否存在,返回接口名对应的适配器.
-
.adapter_discript ⇒ Object
功能:返回网卡描述信息,用来判断网卡型号.
-
.adapter_opt(interface, opt) ⇒ Object
功能:给定一个网卡接口名,卸载网卡 例子:adapter_opt(“本地连接 2”,“禁用”).
-
.adapter_rescan ⇒ Object
功能:扫描硬件改动.
-
.adapter_set_dhcp(interface) ⇒ Object
功能:设置网卡的配置模式为dhcp.
-
.adapter_set_static(interface, opt = {}) ⇒ Object
功能:设置网卡为配置的参数.
-
.devcon_exec(cmd) ⇒ Object
功能:执行devcon命令.
-
.show_interface_stat(interface) ⇒ Object
功能:检查网络接口的连接状态 返回:返回状态字串.
-
.show_netinterface ⇒ Object
功能:显示可用网络接口 返回:返回接口名称数组.
Class Method Details
.adapter(interface) ⇒ Object
功能:检查接口是否存在,返回接口名对应的适配器
23 24 25 26 27 28 29 30 31 |
# File 'lib/vmopt/network.rb', line 23 def self.adapter(interface) interface = interface.to_utf8 raise WinNetError::NoInterfaceError "Give me no NetWorkinterface" if interface.nil? or interface.empty? adapter =WinNet.network_adapter.select do |inf| inf.NetConnectionId == interface ; end raise WinNetError::NoInterfaceError "Find no interface #{interface}" if adapter.empty? adapter[0] end |
.adapter_discript ⇒ Object
功能:返回网卡描述信息,用来判断网卡型号
67 68 69 70 71 72 73 |
# File 'lib/vmopt/network.rb', line 67 def self.adapter_discript() description=[] WinNet.network_adapter_configurations.each do |interface| description << interface.description end description end |
.adapter_opt(interface, opt) ⇒ Object
功能:给定一个网卡接口名,卸载网卡 例子:adapter_opt(“本地连接 2”,“禁用”)
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/vmopt/network.rb', line 41 def self.adapter_opt(interface, opt) pci = adapter(interface).PNPDeviceID; optt=case opt.to_utf8 when "卸载" then "remove" when "禁用" then "disable" when "启用" then "enable" else raise WinNetError::InvalidParamError "Unkown option for adapter.-#{opt}" end cmd = "devcon.exe /r #{optt} \"@#{pci}\" " devcon_exec(cmd) end |
.adapter_rescan ⇒ Object
功能:扫描硬件改动
61 62 63 64 |
# File 'lib/vmopt/network.rb', line 61 def self.adapter_rescan() cmd = "devcon.exe rescan" devcon_exec(cmd) end |
.adapter_set_dhcp(interface) ⇒ Object
功能:设置网卡的配置模式为dhcp
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/vmopt/network.rb', line 76 def self.adapter_set_dhcp(interface) adapter(interface); interface = "\""+interface.to_gbk+"\"" ipset_cmd = "netsh interface ip set address name=#{interface} source=dhcp".to_gbk dnsset_cmd = "netsh interface ip set dns name=#{interface} source=dhcp".to_gbk ipset_status = system("#{ipset_cmd} >null 2>&1"); dnsset_status = system("#{dnsset_cmd} >null 2>&1"); ipset_status & dnsset_status end |
.adapter_set_static(interface, opt = {}) ⇒ Object
功能:设置网卡为配置的参数
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/vmopt/network.rb', line 89 def self.adapter_set_static(interface, opt={}) adapter(interface); interface = "\""+interface.to_gbk+"\"" ipset_cmd = "netsh interface ip set address name=#{interface} source=static addr=#{opt[:addr]} mask=#{opt[:mask]}".to_gbk gwset_cmd = "netsh interface ip set address name=#{interface} gateway=#{opt[:gateway]} gwmetric=0".to_gbk dnsset_cmd = "netsh interface ip set dns name=#{interface} source=static addr=202.106.128.86 register=primary".to_gbk dns2set_cmd = "netsh interface ip add dns name=#{interface} addr=8.8.8.8 index=2".to_gbk ipset_status = system(ipset_cmd) gwset_status = system(gwset_cmd) dns_status = system(dnsset_cmd) dns2_staus = system(dns2set_cmd) ipset_status & dns_status & dns2_staus & gwset_status end |
.devcon_exec(cmd) ⇒ Object
功能:执行devcon命令
34 35 36 37 |
# File 'lib/vmopt/network.rb', line 34 def self.devcon_exec(cmd) status = system("#{cmd} 1>&2"); status end |
.show_interface_stat(interface) ⇒ Object
功能:检查网络接口的连接状态 返回:返回状态字串
17 18 19 20 |
# File 'lib/vmopt/network.rb', line 17 def self.show_interface_stat(interface) adapter(interface.to_utf8); WinNet.netconnstatus[interface.to_utf8] end |
.show_netinterface ⇒ Object
功能:显示可用网络接口 返回:返回接口名称数组
11 12 13 |
# File 'lib/vmopt/network.rb', line 11 def self.show_netinterface() WinNet.interfaces end |