Ruby not printing out the comments

twinbaby

Supremacy Member
Joined
Jul 8, 2014
Messages
5,417
Reaction score
1,499
#!/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
The results is not printing out as expected, clearly there are 2 hosts alive.
Active Network hosts
IP Addr MAC Address
host[:ip] host[:mac]
host[:ip] host[:mac]
Side question, I am already using:
#!/user/bin/ruby, why still not printing out.
bash: ./ruby2.rb: /user/bin/ruby: bad interpreter: No such file or directory
Another side questions, i am trying to print out all the variable at one shot:
#$ irb
#load 'ruby2.rb'
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
But I am getting the following error:
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:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
There is no prefix path starting with /user/bin in normal linux distribution, only /usr/.

Bash:
$ nmap -sP -PR 172.31.XXX.0/24 >/dev/null; sleep 1; arp -n | grep -v incomplete | awk '{print $1,$3}'
Address HWaddress
172.31.XXX.1 XX:7e:06:c6:YY:ZZ
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top