There is a class named open3 in ruby that can be used to execute system commands. However, after Ruby is installed in WINXP, open3 supports Linux by default. If you run the code, the fork () cannot be found () method error.
Fortunately there is a Windows version of open3: win32-open3, the installation is very simple, execute the gem install win32-open3 can:
D:/Ruby/bin> gem install win32-open3
Successfully installed win32-open3-0.2.7-x86-mswin32-60
1 gem installed
Installing Ri documentation for win32-open3-0.2.7-x86-mswin32-60...
Installing rdoc documentation for win32-open3-0.2.7-x86-mswin32-60...
Open3 returns an array containing three Io handles: stdin, stdout, and stderr. They are "standard input", "standard output", and "error" respectively. You can get the result of running the system command by processing stdout.
The Code is as follows:
# Getip. Rb
Require 'rubygems'
Require 'win32/open3'
# Stdin, stdout, stderr = open3.popen3 ('ipconfig ')
# Stdout = open3.popen3 ('ipconfig ')
Io_in, io_out, io_err = open3.popen3 ("ipconfig", mode = 'T', show = false)
# Io_in <"ipconfig/N"
Io_out.each do | L |
P L
End
Run the file and the result is as follows:
"Windows IP configuration/R/N"
"/R/N"
"/R/N"
"Ethernet Adapter/316/336/317/337/315/370/302/347/301/254/275/323:/R/N"
"/R/N"
"Media State ......: Media disconnected/R/N"
"/R/N"
"Ethernet Adapter/261/276/265/330/301/254/275/323:/R/N"
"/R/N"
"Connection-specific DNS suffix.:/R/N"
"Ip address ......: 222.248.147.93/R/N"
"Subnet mask ............: 201710000224.0/R/N"
"Default Gateway...: 222.248.128.1/R/N"
The above is just a way to get an IP address. We can also use a code to do this:
System ("ipconfig ")