sys-host 0.6.2-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,126 @@
1
+ == 0.6.2 - 20-Mar-2010
2
+ * Changed license to Artistic 2.0.
3
+ * The Host.host_id method has been modified to return a hex value on MS
4
+ Windows in order to be consistent with other platforms.
5
+ * Some internal directory layout changes and corresponding changes to the
6
+ Rakefile.
7
+ * Additional Rakefile tasks that remove the need for some code that was
8
+ previously inlined into the gemspec directly.
9
+ * Updated the gemspec, including an updated description, the addition of a
10
+ license, and changing test-unit to a development dependency.
11
+
12
+ == 0.6.1 - 18-Nov-2008
13
+ * Added the host_id method for Windows, which just returns the MAC address.
14
+ * Fixed an issue in the Host.ip_addr method for MS Windows where multiple ip
15
+ addresses might not be handled properly. Thanks go to Murmansk Manny for the
16
+ spot and patch.
17
+ * Updated the gemspec to include all .c and .h files now so that it should
18
+ successfully build and install on all Unix platforms.
19
+ * Rakefile fix for MS Windows for the install task.
20
+ * The test file is now "test_sys_host.rb".
21
+
22
+ == 0.6.0 - 8-Jun-2007
23
+ * Now supports OS X!
24
+ * Added the Host.host_id class method.
25
+ * Bug/warning fix for OS X.
26
+ * HostError is now Host::Error.
27
+ * Added a Rakefile, including tasks for installation and testing.
28
+ * Fixed the gemspec (I hope).
29
+ * Lots of internal reorganization, including the removal of the install.rb
30
+ file. That's handled by the Rakefile.
31
+
32
+ == 0.5.2 - 27-Jun-2006
33
+ * Added the Host.info method for Linux.
34
+
35
+ == 0.5.1 - 3-Mar-2006
36
+ * The Host.info method is now supported on Solaris.
37
+ * Moved project to RubyForge.
38
+ * Replaced INSTALL with README.
39
+ * Made documentation rdoc friendly.
40
+
41
+ == 0.5.0 - 3-Jul-2004
42
+ * Replaced Windows C extension with pure Ruby version, using WMI + OLE.
43
+ * Added an additional class method called 'info' that returns a HostInfo
44
+ struct. Currently on Windows only.
45
+ * Removed the block form of Host.ip_addr. It now simply returns an Array.
46
+ * Renamed and revamped test suite.
47
+ * Moved test.rb to doc/examples
48
+ * Removed the host.html file.
49
+ * Replaced previous exception classes with a single exception class,
50
+ HostError, and moved it under the Sys module namespace.
51
+
52
+ == 0.4.0 - 15-Jun-2003
53
+ * Added MS Windows support
54
+ * Modified extconf.rb file - generic test script now in its own file
55
+ (test/test.rb) and the dynamic rd2 stuff was removed, in addition to proper
56
+ handling for MS Windows
57
+ * rd2, txt and html docs now included by default
58
+ * Removed VERSION() class method (use the constant instead)
59
+ * Added a universal constants.h file to store VERSION info, etc, that
60
+ is common to all source files.
61
+ * Added LICENSE info
62
+
63
+ == 0.3.4 - 3-Apr-2003
64
+ * Fixed a header include issue for solaris systems that don't
65
+ define inet_ntop().
66
+
67
+ == 0.3.3 * 24-Mar-2003
68
+ * If inet_ntop() is not defined, now falls back to inet_ntoa().
69
+
70
+ == 0.3.2 - 14-Mar-2003
71
+ * Added a VERSION constant
72
+ * Modified extconf.rb script, including a bugfix for solaris
73
+ * Added a test suite (for those with testunit installed)
74
+ * Removed the sys-uname requirement
75
+ * Changelog and Manifest are now CHANGES and MANIFEST, respectively
76
+ * Minor internal layout changes
77
+ * rd2 documentation removed from C source files and put into 'doc'
78
+ directory
79
+
80
+ == 0.3.1 - 6-Jan-2003
81
+ * Fix to make code C89 compliant so that older compilers don't choke.
82
+ * Added a VERSION class method
83
+ * Added a copyright notice
84
+ * Moved README into doc/host.txt
85
+ * Minor doc changes
86
+ * Fixed extconf.rb file to include socket lib for Solaris. This was
87
+ in 0.2.0 and was mistakenly left out of 0.3.0.
88
+
89
+ == 0.3.0 - 28-Oct-2002
90
+ * Reentrant (thread-safe) support added for ip_addr() method (for Linux,
91
+ FreeBSD and Solaris currently).
92
+ * The file layout changed so that each platform has its own source file.
93
+ This was done because different platforms have different implementations
94
+ of gethostbyname_r(), if they have them at all. Some platforms (e.g.
95
+ FreeBSD) use a different function altogether.
96
+ * The file generic.c is used to create the source if the platform is not
97
+ currently supported, or if gethostbyname_r() isn't found.
98
+ * The FreeBSD implementation uses getipnodebyname() instead of
99
+ gethostbyname(), if possible. According to the man page, it's thread
100
+ safe.
101
+ * Modifications to the extconf.rb file based on the above changes.
102
+ * Added a default value of 16 for INET_ADDRSTRLEN.
103
+ * Added a README file.
104
+
105
+ == 0.2.0 - 1-Sep-2002
106
+ * Added HostException and HostAddressException classes. These are raised
107
+ in the event the gethostname or gethostbyname call fails, respectively.
108
+ * Removed some unnecessary (and warning-causing) free calls.
109
+ * Added an INSTALL file (installation instructions removed from core doc).
110
+
111
+ == 0.1.2 - 9-Aug-2002
112
+ * Got the BSD support working properly this time. Thanks to Sean
113
+ Chittenden for providing BSD access.
114
+
115
+ == 0.1.1 - 21-Jun-2002
116
+ * Fixed a bug that would cause the ip_addr to segfault on some
117
+ operating systems. Apparently ,trying to free memory on a
118
+ null pointer is a bad thing. Hey, Solaris didn't complain!
119
+ Thanks go to Han Holl for the spot.
120
+ * On *BSD systems the 'netinet/in.h' file is now included (via
121
+ the makefile). Thanks go to Magnus Stahre for the spot and
122
+ the fix.
123
+ * Changed the version number to be more "ruby compliant"
124
+
125
+ == 0.01 * 11-Jun-2002
126
+ * First release!
data/MANIFEST ADDED
@@ -0,0 +1,17 @@
1
+ * CHANGES
2
+ * MANIFEST
3
+ * README
4
+ * Rakefile
5
+ * sys-host.gemspec
6
+ * doc/host.txt
7
+ * examples/example_sys_host.rb
8
+ * ext/bsd/extconf.rb
9
+ * ext/bsd/sys/host.c
10
+ * ext/generic/extconf.rb
11
+ * ext/generic/sys/host.c
12
+ * ext/linux/extconf.rb
13
+ * ext/linux/sys/host.c
14
+ * ext/sunos/extconf.rb
15
+ * ext/sunos/sys/host.c
16
+ * lib/windows/sys/host.rb
17
+ * test/test_sys_host.rb
data/README ADDED
@@ -0,0 +1,26 @@
1
+ = Prerequisites
2
+ Ruby 1.8.0 or later.
3
+ Ruby 1.8.2 or later preferred on Windows.
4
+
5
+ = Synopsis
6
+ require 'sys/host'
7
+ include Sys
8
+
9
+ p Host.hostname
10
+ p Host.ip_addr
11
+
12
+ Host.info{ |h|
13
+ p h
14
+ }
15
+
16
+ = Installation
17
+ == Standard (non-gem) installation
18
+ rake test (optional)
19
+ rake install
20
+
21
+ == Gem Installation
22
+ rake test (optional)
23
+ rake install_gem
24
+
25
+ = Documentation
26
+ See the doc/host.txt file for more information.
data/Rakefile ADDED
@@ -0,0 +1,169 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rbconfig'
4
+ include Config
5
+
6
+ desc "Clean the build files for the sys-host source for UNIX systems"
7
+ task :clean do
8
+ rm_rf('.test-result') if File.exists?('.test-result')
9
+ rm_rf 'conftest.dSYM' if File.exists?('conftest.dSYM') # OS X
10
+
11
+ Dir['*.gem'].each{ |f| File.delete(f) }
12
+
13
+ case Config::CONFIG['host_os']
14
+ when /bsd|darwin/i
15
+ dir = 'ext/bsd'
16
+ when /sunos|solaris/i
17
+ dir = 'ext/sunos'
18
+ when /linux/i
19
+ dir = 'ext/linux'
20
+ else
21
+ dir = 'ext/generic'
22
+ end
23
+
24
+ unless Config::CONFIG['host_os'] =~ /win32|mswin|dos|cygwin|mingw/i
25
+ Dir.chdir(dir) do
26
+ if Dir['*.o'].length > 0
27
+ sh 'make distclean'
28
+ Dir['sys/host.*'].each{ |f| rm(f) if File.extname(f) != '.c' }
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ desc "Build the sys-host library on UNIX systems (but don't install it)"
35
+ task :build => [:clean] do
36
+ case Config::CONFIG['host_os']
37
+ when /bsd|darwin/i
38
+ dir = 'ext/bsd'
39
+ when /sunos|solaris/i
40
+ dir = 'ext/sunos'
41
+ when /linux/i
42
+ dir = 'ext/linux'
43
+ else
44
+ dir = 'ext/generic'
45
+ end
46
+
47
+ unless Config::CONFIG['host_os'] =~ /win32|mswin|dos|cygwin|mingw/i
48
+ Dir.chdir(dir) do
49
+ ruby 'extconf.rb'
50
+ sh 'make'
51
+ cp 'host.' + Config::CONFIG['DLEXT'], 'sys'
52
+ end
53
+ end
54
+ end
55
+
56
+ desc 'Install the sys-host library'
57
+ task :install => [:build] do
58
+ file = nil
59
+ dir = File.join(Config::CONFIG['sitelibdir'], 'sys')
60
+
61
+ Dir.mkdir(dir) unless File.exists?(dir)
62
+
63
+ case Config::CONFIG['host_os']
64
+ when /mswin|win32|msdos|cygwin|mingw/i
65
+ file = 'lib/windows/sys/host.rb'
66
+ when /bsd|darwin/i
67
+ Dir.chdir('ext/bsd'){ sh 'make install' }
68
+ when /sunos|solaris/i
69
+ Dir.chdir('ext/sunos'){ sh 'make install' }
70
+ when /linux/i
71
+ Dir.chdir('ext/linux'){ sh 'make install' }
72
+ else
73
+ Dir.chdir('ext/generic'){ sh 'make install' }
74
+ end
75
+
76
+ cp(file, dir, :verbose => true) if file
77
+ end
78
+
79
+ desc 'Uninstall the sys-host library'
80
+ task :uninstall do
81
+ case Config::CONFIG['host_os']
82
+ when /win32|mswin|dos|cygwin|mingw/i
83
+ dir = File.join(Config::CONFIG['sitelibdir'], 'sys')
84
+ file = File.join(dir, 'host.rb')
85
+ else
86
+ dir = File.join(Config::CONFIG['sitearchdir'], 'sys')
87
+ file = File.join(dir, 'host.' + Config::CONFIG['DLEXT'])
88
+ end
89
+
90
+ rm(file)
91
+ end
92
+
93
+ desc "Run the example sys-host program"
94
+ task :example => [:build] do
95
+ Dir.mkdir('sys') unless File.exists?('sys')
96
+ if CONFIG['host_os'].match('mswin')
97
+ ruby '-Ilib/windows examples/example_sys_host.rb'
98
+ else
99
+ case Config::CONFIG['host_os']
100
+ when /bsd|darwin/i
101
+ dir = 'ext/bsd'
102
+ when /sunos|solaris/i
103
+ dir = 'ext/sunos'
104
+ when /linux/i
105
+ dir = 'ext/linux'
106
+ else
107
+ dir = 'ext/generic'
108
+ end
109
+ ruby "-I#{dir} examples/example_sys_host.rb"
110
+ end
111
+ end
112
+
113
+ desc 'Run the test suite'
114
+ Rake::TestTask.new do |t|
115
+ task :test => :build
116
+ t.libs << 'test'
117
+ t.test_files = FileList['test/test_sys_host.rb']
118
+
119
+ case Config::CONFIG['host_os']
120
+ when /mswin|msdos|cygwin|mingw/i
121
+ t.libs << 'lib/windows'
122
+ when /linux/i
123
+ t.libs << 'ext/linux'
124
+ when /sunos|solaris/i
125
+ t.libs << 'ext/sunos'
126
+ when /bsd|darwin/i
127
+ t.libs << 'ext/bsd'
128
+ else
129
+ t.libs << 'ext/generic'
130
+ end
131
+ end
132
+
133
+ namespace 'gem' do
134
+ desc 'Create the sys-host gem file'
135
+ task :create => [:clean] do
136
+ spec = eval(IO.read('sys-host.gemspec'))
137
+
138
+ case Config::CONFIG['host_os']
139
+ when /bsd|darwin/i
140
+ spec.files << 'ext/bsd/sys/host.c'
141
+ spec.extra_rdoc_files << 'ext/bsd/sys/host.c'
142
+ spec.extensions = ['ext/bsd/extconf.rb']
143
+ when /linux/i
144
+ spec.files << 'ext/linux/sys/host.c'
145
+ spec.extra_rdoc_files << 'ext/linux/sys/host.c'
146
+ spec.extensions = ['ext/linux/extconf.rb']
147
+ when /sunos|solaris/i
148
+ spec.files << 'ext/sunos/sys/host.c'
149
+ spec.extra_rdoc_files << 'ext/sunos/sys/host.c'
150
+ spec.extensions = ['ext/sunos/extconf.rb']
151
+ when /mswin|win32|dos|cygwin|mingw/i
152
+ spec.platform = Gem::Platform::CURRENT
153
+ spec.require_paths = ['lib', 'lib/windows']
154
+ spec.files += ['lib/windows/sys/host.rb']
155
+ else
156
+ spec.files << 'ext/generic/sys/host.c'
157
+ spec.extra_rdoc_files << 'ext/generic/sys/host.c'
158
+ spec.extensions = ['ext/generic/extconf.rb']
159
+ end
160
+
161
+ Gem::Builder.new(spec).build
162
+ end
163
+
164
+ desc 'Install the sys-host gem'
165
+ task :install => [:create] do
166
+ file = Dir["*.gem"].first
167
+ sh "gem install #{file}"
168
+ end
169
+ end
data/doc/host.txt ADDED
@@ -0,0 +1,88 @@
1
+ == Description
2
+ Provides hostname and ip address information for a given host.
3
+
4
+ == Synopsis
5
+ require 'sys/host'
6
+ include Sys
7
+
8
+ p Host.hostname
9
+ p Host.ip_addr
10
+
11
+ # Some platforms
12
+ Host.info{ |h|
13
+ p h
14
+ }
15
+
16
+ == Constants
17
+ VERSION
18
+ Returns the current version number of this library (as a string)
19
+
20
+ == Class Methods
21
+ Host.hostname
22
+ Returns the hostname of the current host. This may or not return
23
+ the FQDN, depending on your system.
24
+
25
+ Host.ip_addr
26
+ Returns a list of IP addresses for the current host (yes, it is
27
+ possible to have more than one).
28
+
29
+ Host.info(host=localhost)
30
+ Host.info(host=localhost){ |h| ... }
31
+ Yields a HostInfo struct for each network adapter on 'host', or an array
32
+ of HostInfo struct's in non-block form. The exact members of this struct
33
+ vary depending on your platform.
34
+
35
+ Host.host_id
36
+ Returns the unique hexadecimal identifier for the current host.
37
+
38
+ == Exception Classes
39
+ Host::Error < StandardError
40
+ Raised in the event of a failure for any of the class methods provided
41
+ with this library. Generally speaking, it means there was a failure in
42
+ the underlying gethostname() or gethostbyname() calls.
43
+
44
+ == Future Plans
45
+ Add support for IPV6. This will be difficult unless I have access to a
46
+ system on an IPV6 network. Any help on this front appreciated.
47
+
48
+ == Notes
49
+ The Windows version of this library uses OLE + WMI. The Host.info method
50
+ returns considerably more information than the *nix versions of this
51
+ library.
52
+
53
+ == Comments and Questions
54
+ Please direct all comments and/or questions to one of the Forums on the
55
+ project home page at http://www.rubyforge.org/projects/sysutils.
56
+
57
+ == Known Bugs
58
+ None that I'm aware of.
59
+
60
+ Please log any bugs on the project home page at
61
+ http://www.rubyforge.org/projects/sysutils
62
+
63
+ == Acknowledgements
64
+ Thanks go to Mike Hall, Guy Decoux, and Matz for their help with
65
+ C extensions in general.
66
+
67
+ Thanks also go to Richard Stevens for his excellent Unix programming books.
68
+
69
+ == License
70
+ Artistic 2.0
71
+
72
+ == Copyright
73
+ Copyright 2002-2010, Daniel J. Berger, djberg96 at gmail dot com
74
+
75
+ All Rights Reserved. This module is free software. It may be used,
76
+ redistributed and/or modified under the same terms as Ruby itself.
77
+
78
+ == Warranty
79
+ This library is provided "as is" and without any express or
80
+ implied warranties, including, without limitation, the implied
81
+ warranties of merchantability and fitness for a particular purpose.
82
+
83
+ == Author
84
+ Daniel J. Berger
85
+
86
+ == See Also
87
+ gethostbyname, gethostbyname_r, gethostname, hostname (Unix)
88
+ WMI, OLE (Windows)
@@ -0,0 +1,21 @@
1
+ ####################################################################
2
+ # example_sys_host.rb
3
+ #
4
+ # Generic test script for general futzing. Use the 'rake example'
5
+ # task to run this.
6
+ #
7
+ # Modify as you see fit.
8
+ ####################################################################
9
+ require 'sys/host'
10
+ include Sys
11
+
12
+ puts "VERSION: " + Host::VERSION
13
+ puts "Hostname: " + Host.hostname
14
+ puts "IP Addresses : " + Host.ip_addr.join(',')
15
+ puts "Host ID: " + Host.host_id.to_s
16
+
17
+ puts "Info: "
18
+
19
+ Host.info{ |i|
20
+ p i
21
+ }
@@ -0,0 +1,285 @@
1
+ require 'win32ole'
2
+ require 'socket'
3
+ require 'time'
4
+
5
+ # The Sys module serves as a namespace only.
6
+ module Sys
7
+ # The Host class encapsulates methods that return information about the local host.
8
+ class Host
9
+ # Error class typically raised if any of the Host methods fail.
10
+ class Error < StandardError; end
11
+
12
+ # The version of the sys-host library.
13
+ VERSION = '0.6.2'
14
+
15
+ # :stopdoc:
16
+
17
+ fields = %w[
18
+ arp_always_source_route?
19
+ arp_use_ether_snap?
20
+ caption
21
+ database_path
22
+ dead_gw_detect_enabled?
23
+ default_ip_gateway
24
+ default_tos
25
+ default_ttl
26
+ description
27
+ dhcp_enabled?
28
+ dhcp_lease_expires
29
+ dhcp_lease_obtained
30
+ dhcp_server
31
+ dns_domain
32
+ dns_domain_suffix_search_order
33
+ dns_enabled_for_wins_resolution?
34
+ dns_host_name
35
+ dns_server_search_order
36
+ domain_dns_registration_enabled?
37
+ forward_buffer_memory
38
+ full_dns_registration_enabled?
39
+ gateway_cost_metric
40
+ igmp_level
41
+ index
42
+ ip_address
43
+ ip_connection_metric
44
+ ip_enabled?
45
+ ip_filter_security_enabled?
46
+ ip_port_security_enabled?
47
+ ip_sec_permit_ip_protocols
48
+ ip_sec_permit_tcp_ports
49
+ ip_sec_permit_udp_ports
50
+ ip_subnet
51
+ ip_use_zero_broadcast
52
+ ipx_address
53
+ ipx_enabled?
54
+ ipx_frame_type
55
+ ipx_media_type
56
+ ipx_network_number
57
+ ipx_virtual_net_number
58
+ keep_alive_interval
59
+ keep_alive_time
60
+ mac_address
61
+ mtu
62
+ num_forward_packets
63
+ pmtubh_detect_enabled?
64
+ pmtu_discovery_enabled?
65
+ service_name
66
+ setting_id
67
+ tcpip_netbios_options
68
+ tcp_max_connect_retransmissions
69
+ tcp_max_data_retransmissions
70
+ tcp_num_connections
71
+ tcp_use_rfc1122_urgent_pointer?
72
+ tcp_window_size
73
+ wins_enable_lm_hosts_lookup?
74
+ wins_host_lookup_file
75
+ wins_primary_server
76
+ wins_scope_id
77
+ wins_secondary_server
78
+ ]
79
+
80
+ HostInfo = Struct.new("HostInfo", *fields)
81
+
82
+ # :startdoc:
83
+
84
+ # Yields a HostInfo struct for each network adapter on 'host', or an array
85
+ # of HostInfo struct's in non-block form. The exact members of this struct
86
+ # vary depending on your platform.
87
+ #
88
+ def self.info(host=Socket.gethostname)
89
+ array = [] unless block_given?
90
+ cs = "winmgmts://#{host}/root/cimv2"
91
+
92
+ begin
93
+ wmi = WIN32OLE.connect(cs)
94
+ rescue WIN32OLERuntimeError => err
95
+ raise Error, err
96
+ else
97
+ query = "
98
+ select * from Win32_NetworkAdapterConfiguration
99
+ where IPEnabled = True
100
+ "
101
+
102
+ wmi.ExecQuery(query).each{ |nic|
103
+ if nic.DHCPLeaseExpires
104
+ dhcp_expires = Time.parse(nic.DHCPLeaseExpires.split('.')[0])
105
+ else
106
+ dhcp_expires = nil
107
+ end
108
+
109
+ if nic.DHCPLeaseObtained
110
+ dhcp_lease_obtained = Time.parse(nic.DHCPLeaseObtained.split('.')[0])
111
+ else
112
+ dhcp_lease_obtained = nil
113
+ end
114
+
115
+ igmp_level = nil
116
+
117
+ case nic.IGMPLevel
118
+ when 0
119
+ igmp_level = "None"
120
+ when 1
121
+ igmp_level = "IP"
122
+ when 2
123
+ igmp_level = "IP & IGMP"
124
+ end
125
+
126
+ ipx_frame_type = nil
127
+ case nic.IPXFrameType
128
+ when 0
129
+ ipx_frame_type = "Ethernet II"
130
+ when 1
131
+ ipx_frame_type = "Ethernet 802.3"
132
+ when 2
133
+ ipx_frame_type = "Ethernet 802.2"
134
+ when 3
135
+ ipx_frame_type = "Ethernet SNAP"
136
+ when 255
137
+ ipx_frame_type = "AUTO"
138
+ end
139
+
140
+ ipx_media_type = nil
141
+ case nic.IPXMediaType
142
+ when 0
143
+ ipx_media_type = "Ethernet"
144
+ when 1
145
+ ipx_media_type = "Token Ring"
146
+ when 2
147
+ ipx_media_type = "FDDI"
148
+ when 8
149
+ ipx_media_type = "ARCNET"
150
+ end
151
+
152
+ tcpip_netbios_options = nil
153
+ case nic.TcpipNetbiosOptions
154
+ when 0
155
+ tcpip_netbios_options = "EnableNetbiosViaDhcp"
156
+ when 1
157
+ tcpip_netbios_options = "EnableNetbios"
158
+ when 2
159
+ tcpip_netbios_options = "DisableNetbios"
160
+ end
161
+
162
+ struct = HostInfo.new(
163
+ nic.ArpAlwaysSourceRoute,
164
+ nic.ArpUseEtherSNAP,
165
+ nic.Caption,
166
+ nic.DatabasePath,
167
+ nic.DeadGWDetectEnabled,
168
+ nic.DefaultIPGateway,
169
+ nic.DefaultTOS,
170
+ nic.DefaultTTL,
171
+ nic.Description,
172
+ nic.DHCPEnabled,
173
+ dhcp_expires,
174
+ dhcp_lease_obtained,
175
+ nic.DHCPServer,
176
+ nic.DNSDomain,
177
+ nic.DNSDomainSuffixSearchOrder,
178
+ nic.DNSEnabledForWINSResolution,
179
+ nic.DNSHostName,
180
+ nic.DNSServerSearchOrder,
181
+ nic.DomainDNSRegistrationEnabled,
182
+ nic.ForwardBufferMemory,
183
+ nic.FullDNSRegistrationEnabled,
184
+ nic.GatewayCostMetric,
185
+ igmp_level,
186
+ nic.Index,
187
+ nic.IpAddress,
188
+ nic.IPConnectionMetric,
189
+ nic.IPEnabled,
190
+ nic.IPFilterSecurityEnabled,
191
+ nic.IPPortSecurityEnabled,
192
+ nic.IPSecPermitIPProtocols,
193
+ nic.IPSecPermitTCPPorts,
194
+ nic.IPSecPermitUDPPorts,
195
+ nic.IPSubnet,
196
+ nic.IPUseZeroBroadcast,
197
+ nic.IPXAddress,
198
+ nic.IPXEnabled,
199
+ ipx_frame_type,
200
+ ipx_media_type,
201
+ nic.IPXNetworkNumber,
202
+ nic.IPXVirtualNetNumber,
203
+ nic.KeepAliveInterval,
204
+ nic.KeepAliveTime,
205
+ nic.MacAddress,
206
+ nic.MTU,
207
+ nic.NumForwardPackets,
208
+ nic.PMTUBHDetectEnabled,
209
+ nic.PMTUDiscoveryEnabled,
210
+ nic.ServiceName,
211
+ nic.SettingID,
212
+ tcpip_netbios_options,
213
+ nic.TcpMaxConnectRetransmissions,
214
+ nic.TcpMaxDataRetransmissions,
215
+ nic.TcpNumConnections,
216
+ nic.TcpUseRFC1122UrgentPointer,
217
+ nic.TcpWindowSize,
218
+ nic.WINSEnableLMHostsLookup,
219
+ nic.WINSHostLookupFile,
220
+ nic.WINSPrimaryServer,
221
+ nic.WINSScopeID,
222
+ nic.WINSSecondaryServer
223
+ )
224
+
225
+ if block_given?
226
+ yield struct
227
+ else
228
+ array << struct
229
+ end
230
+ }
231
+ end
232
+
233
+ block_given? ? nil : array
234
+ end
235
+
236
+ # Returns the hostname of the current host. This may or not return
237
+ # the FQDN, depending on your system.
238
+ #
239
+ def self.hostname
240
+ host = nil
241
+
242
+ begin
243
+ wmi = WIN32OLE.connect("winmgmts://")
244
+ rescue WIN32OLERuntimeError => err
245
+ raise Error, err
246
+ else
247
+ query = "
248
+ select * from Win32_NetworkAdapterConfiguration
249
+ where IPEnabled = True
250
+ "
251
+ wmi.ExecQuery(query).each{ |nic|
252
+ host = nic.DNSHostName
253
+ break
254
+ }
255
+ end
256
+
257
+ host
258
+ end
259
+
260
+ # Returns a list of unique IP addresses for the current host.
261
+ #
262
+ def self.ip_addr
263
+ ip_addrs = []
264
+ begin
265
+ wmi = WIN32OLE.connect("winmgmts://")
266
+ rescue WIN32OLERuntimeError => err
267
+ raise Error, err
268
+ else
269
+ query = "
270
+ select * from Win32_NetworkAdapterConfiguration
271
+ where IPEnabled = True
272
+ "
273
+ wmi.ExecQuery(query).each{ |nic| ip_addrs << nic.IPAddress }
274
+ end
275
+
276
+ ip_addrs.flatten.uniq
277
+ end
278
+
279
+ # Returns the host id (physical address) of the current host.
280
+ #
281
+ def self.host_id
282
+ info[0].mac_address.split(':').join.hex
283
+ end
284
+ end
285
+ end
data/sys-host.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'rbconfig'
3
+ include Config
4
+
5
+ # Some additional specification options are handled by the gem:create task.
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'sys-host'
9
+ spec.version = '0.6.2'
10
+ spec.author = 'Daniel J. Berger'
11
+ spec.license = 'Artistic 2.0'
12
+ spec.email = 'djberg96@gmail.com'
13
+ spec.homepage = 'http://www.rubyforge.org/projects/sysutils'
14
+ spec.platform = Gem::Platform::RUBY
15
+ spec.summary = 'Provides hostname and ip address info for a given host'
16
+ spec.test_file = 'test/test_sys_host.rb'
17
+ spec.has_rdoc = true
18
+
19
+ spec.files = %w[
20
+ CHANGES
21
+ MANIFEST
22
+ README
23
+ Rakefile
24
+ sys-host.gemspec
25
+ doc/host.txt
26
+ examples/example_sys_host.rb
27
+ test/test_sys_host.rb
28
+ ]
29
+
30
+ spec.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST']
31
+ spec.rubyforge_project = 'sysutils'
32
+
33
+ spec.add_development_dependency('test-unit', '>= 2.0.7')
34
+
35
+ spec.description = <<-EOF
36
+ The sys-host library provides information about the current machine that
37
+ your program is running on. Specifically, it returns hostname, IP
38
+ address information, aliases and so on.
39
+ EOF
40
+ end
@@ -0,0 +1,129 @@
1
+ ###########################################################################
2
+ # test_sys_host.rb
3
+ #
4
+ # Test suite for sys-host, all platforms. You should run these tests via
5
+ # the 'rake test' task.
6
+ ###########################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'sys/host'
11
+ require 'test/unit'
12
+ require 'rbconfig'
13
+ include Config
14
+ include Sys
15
+
16
+ class TC_Sys_Host < Test::Unit::TestCase
17
+ def self.startup
18
+ if CONFIG['host_os'] =~ /mswin|solaris|linux|bsd|mach|darwin|osx/i
19
+ @@info_supported = true
20
+ else
21
+ @@info_supported = false
22
+ end
23
+
24
+ @@windows = CONFIG['host_os'].match('mswin')
25
+ end
26
+
27
+ def test_version
28
+ assert_equal('0.6.2', Host::VERSION)
29
+ end
30
+
31
+ def test_hostname_basic
32
+ assert_respond_to(Host, :hostname)
33
+ assert_nothing_raised{ Host.hostname }
34
+ end
35
+
36
+ def test_hostname
37
+ assert_kind_of(String, Host.hostname)
38
+ assert_equal(`hostname`.chomp, Host.hostname) # sanity check
39
+ end
40
+
41
+ def test_hostname_expected_errors
42
+ assert_raise(ArgumentError){ Host.hostname(true) }
43
+ end
44
+
45
+ def test_ip_addr_basic
46
+ assert_respond_to(Host, :ip_addr)
47
+ assert_nothing_raised{ Host.ip_addr }
48
+ end
49
+
50
+ def test_ip_addr
51
+ assert_kind_of(Array, Host.ip_addr)
52
+ assert_kind_of(String, Host.ip_addr.first)
53
+ end
54
+
55
+ def test_ip_addr_expected_errors
56
+ assert_raise(ArgumentError){ Host.ip_addr(true) }
57
+ end
58
+
59
+ def test_host_id_basic
60
+ assert_respond_to(Host, :host_id)
61
+ assert_nothing_raised{ Host.host_id }
62
+ end
63
+
64
+ def test_host_id
65
+ type = @@windows ? String : Integer
66
+ assert_kind_of(type, Host.host_id)
67
+ end
68
+
69
+ def test_host_id_expected_errors
70
+ assert_raise(ArgumentError){ Host.host_id(true) }
71
+ end
72
+
73
+ def test_info_basic
74
+ omit_unless(@@info_supported, 'info test skipped on this platform')
75
+ assert_respond_to(Host, :info)
76
+ assert_nothing_raised{ Host.info }
77
+ end
78
+
79
+ def test_info
80
+ omit_unless(@@info_supported, 'info test skipped on this platform')
81
+ assert_kind_of(Array, Host.info)
82
+ assert_kind_of(Struct::HostInfo, Host.info.first)
83
+ assert_nil(Host.info{ })
84
+ end
85
+
86
+ def test_info_name_member
87
+ omit_unless(@@info_supported, 'info test skipped on this platform')
88
+ assert_true(Host.info.first.members.map{ |e| e.to_s }.include?('name'))
89
+ assert_kind_of(String, Host.info.first.name)
90
+ assert_true(Host.info.first.name.size > 0)
91
+ end
92
+
93
+ def test_info_addr_type_member
94
+ omit_unless(@@info_supported, 'info test skipped on this platform')
95
+ assert_true(Host.info.first.members.map{ |e| e.to_s }.include?('addr_type'))
96
+ assert_kind_of(Fixnum, Host.info.first.addr_type)
97
+ assert_true(Host.info.first.addr_type >= 0)
98
+ end
99
+
100
+ def test_info_aliases_member
101
+ omit_unless(@@info_supported, 'info test skipped on this platform')
102
+ assert_true(Host.info.first.members.map{ |e| e.to_s }.include?('aliases'))
103
+ assert_kind_of(Array, Host.info.first.aliases)
104
+ end
105
+
106
+ def test_info_length_member
107
+ omit_unless(@@info_supported, 'info test skipped on this platform')
108
+ assert_true(Host.info.first.members.map{ |e| e.to_s }.include?('length'))
109
+ assert_kind_of(Fixnum, Host.info.first.length)
110
+ assert_true(Host.info.first.length >= 4)
111
+ end
112
+
113
+ def test_info_addr_list_member
114
+ omit_unless(@@info_supported, 'info test skipped on this platform')
115
+ assert_true(Host.info.first.members.map{ |e| e.to_s }.include?('addr_list'))
116
+ assert_kind_of(Array, Host.info.first.addr_list)
117
+ assert_true(Host.info.first.addr_list.first.length > 0)
118
+ end
119
+
120
+ def test_info_high_iteration
121
+ omit_unless(@@info_supported, 'info test skipped on this platform')
122
+ assert_nothing_raised{ 100.times{ Host.info } }
123
+ end
124
+
125
+ def self.shutdown
126
+ @@info_supported = nil
127
+ @@windows = nil
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sys-host
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.2
5
+ platform: x86-mingw32
6
+ authors:
7
+ - Daniel J. Berger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-20 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: test-unit
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.0.7
24
+ version:
25
+ description: " The sys-host library provides information about the current machine that\n your program is running on. Specifically, it returns hostname, IP\n address information, aliases and so on.\n"
26
+ email: djberg96@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - CHANGES
33
+ - README
34
+ - MANIFEST
35
+ files:
36
+ - CHANGES
37
+ - MANIFEST
38
+ - README
39
+ - Rakefile
40
+ - sys-host.gemspec
41
+ - doc/host.txt
42
+ - examples/example_sys_host.rb
43
+ - test/test_sys_host.rb
44
+ - lib/windows/sys/host.rb
45
+ has_rdoc: true
46
+ homepage: http://www.rubyforge.org/projects/sysutils
47
+ licenses:
48
+ - Artistic 2.0
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ - lib/windows
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: sysutils
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Provides hostname and ip address info for a given host
74
+ test_files:
75
+ - test/test_sys_host.rb