The results is not printing out as expected, clearly there are 2 hosts alive.#!/user/bin/ruby
require 'socket'
s = UDPSocket.new
254.times do |i|
next if i == 0
s.send("test", 0, "192.168.10." + i.to_s, 53)
end
f = File.open("/proc/net/arp",'r')
data = f.read.split("\n")
up_hosts = []
data.each do |line|
entry = line.split(/\s+/)
next if entry [3] == "00:00:00:00:00:00"
next if entry [0] == "IP"
up_hosts << {:ip => entry[0], :mac => entry[3]}
end
print "Active Network hosts\n"
#print out in a table
print "%-12s\t%s\n" % ["IP Addr", "MAC Address"]
#print out in a list of host
up_hosts.each do |host|
print "%-12s\t%s\n" % ["host[:ip]", "host[:mac]"]
end
Side question, I am already using:Active Network hosts
IP Addr MAC Address
host[:ip] host[:mac]
host[:ip] host[:mac]
#!/user/bin/ruby, why still not printing out.
Another side questions, i am trying to print out all the variable at one shot:bash: ./ruby2.rb: /user/bin/ruby: bad interpreter: No such file or directory
#$ irb
#load 'ruby2.rb'
But I am getting the following error:char = 'A'
char.unpack('C')
char.unpack('C').first
char.unpack('C').first.to_s(16)
char.unpack('C').first.to_s(2)
char.unpack('C').first.to_s(2).to_i
'%08d' % char.unpack('C').first.to_s(2).to_i
Traceback (most recent call last):
19: from /usr/local/bin/irb:25:in `<main>'
18: from /usr/local/bin/irb:25:in `load'
17: from /var/lib/gems/2.7.0/gems/irb-1.3.7/exe/irb:11:in `<top (required)>'
(irb):1:in `<main>': undefined local variable or method `ruby2' for main:Object (NameError)
Edit (7/12/2021) found the issues, just have to remove the double quote:
#print out in a list of host
up_hosts.each do |host|
print "%-12s\t%s\n" % [host[:ip], host[:mac]]
end
Also I have change from #!/user/bin/ruby to #!/usr/bin/ruby, now I am able to run successfully.
Last edited: