Ruby obtains the IP address of the WINXP Machine

Source: Internet
Author: User

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 ")

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.