Ruby · RubyonRails

Console commands in Ruby(Windows)

Hi All,

Sorry for a big gap…

Command Prompt commands in Ruby:(Windows Only)

Goto Command prompt:

c:> ipconfig

It will gives the details about ur ip address.How can i get the same from Ruby code.

system(‘ipconfig’)

Output:

Windows IP Configuration

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :

IP Address. . . . . . . . . . . . : 192.168.1.25

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.168.1.1

Program 2:

commandprompt.rb

system(‘ping 192.168.1.1’)
puts “———————”
system(‘ipconfig’)

>ruby commandprompt.rb

Pinging 192.168.1.1 with 32 bytes of data:

Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.1.1:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

Minimum = 0ms, Maximum = 0ms, Average = 0ms

———————

Windows IP Configuration

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :

IP Address. . . . . . . . . . . . : 192.168.1.25

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.168.1.1

>Exit code: 0

Program3:

Shutdown,restart,logoff your windows machine using Ruby codes.

shutdown.rb

system(‘shutdown.exe’)

Output:

Usage: shutdown.exe [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-
c “comment”] [-d up:xx:yy]

No args Display this message (same as -?)
-i Display GUI interface, must be the first option
-l Log off (cannot be used with -m option)
-s Shutdown the computer
-r Shutdown and restart the computer
-a Abort a system shutdown
-m \\computername Remote computer to shutdown/restart/abort
-t xx Set timeout for shutdown to xx seconds
-c “comment” Shutdown comment (maximum of 127 characters)
-f Forces running applications to close without war
ning
-d [u][p]:xx:yy The reason code for the shutdown
u is the user code
p is a planned shutdown code
xx is the major reason code (positive integer le
ss than 256)
yy is the minor reason code (positive integer le
ss than 65536)

shutdown.rb

system(‘shutdown.exe -r -f -t 0’)

Run it…

Output:

System shutdown in default.

Reference:

http://rubyonwindows.blogspot.com/

http://www.ruby-forum.com/topic/150301