Skip to content

Instantly share code, notes, and snippets.

@xanthalas
Created May 27, 2014 13:38
Show Gist options
  • Select an option

  • Save xanthalas/03a9121e381a942d399f to your computer and use it in GitHub Desktop.

Select an option

Save xanthalas/03a9121e381a942d399f to your computer and use it in GitHub Desktop.
Get Internal IP Address and write to file
#!/usr/bin/ruby
#Retrieve the internal IP address (using IPCONFIG in Windows), display it to the console and write it to a file.
#binding.pry
def get_address
toFile = ARGV[0] + '\current_ip'
command = 'ipconfig'
output = `#{command}`
lines = output.split("\n")
ethernetConnection = false
ip = "<undefined>"
lines.each {|line|
ethernetConnection = true if /Ethernet adapter Local Area Connection/ =~ line
if ethernetConnection and /IPv4 Address/ =~ line
ip = line
puts line
break
end
}
time = Time.new
File.open(toFile, 'a') do |outfile|
outfile.puts "-----------------\n"
outfile.puts "IP Address: #{ip}\n"
outfile.puts "Retrieved on #{time.day}/#{time.month}/#{time.year} at #{time.hour}:#{time.min}"
end
end
#Mainline starts here
if ARGV.empty?
puts "Please enter path where file current_ip.txt will be written."
else
get_address
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment