Internal/OpenFlow/OrbitSwitches/scripts: pingmapper.rb

File pingmapper.rb, 1.7 KB (added by akoshibe, 12 years ago)

script that returns a list of [node:MAC] pairs and populates switch MAC tables for a sandbox.

Line 
1# Logger Example
2
3require 'logger'
4require 'thread'
5
6LOG = Logger.new(STDOUT)
7LOG.level = Logger::INFO
8flag = nil
9
10if ARGV.length < 4
11 puts "Usage: ruby pingmapper.rb [XMIN] [XMAX] [YMIN] [YMAX] [-p|m]"
12 puts "\tNo flag to bring up/config eth0 and to run ping and map"
13 puts "\t-p to just ping"
14 puts "\t-m to only map(interfaces must be configured)"
15 exit
16end
17
18XMIN = ARGV[0].to_i
19XMAX = ARGV[1].to_i
20YMIN = ARGV[2].to_i
21YMAX = ARGV[3].to_i
22flag = ARGV[4]
23
24LOCK = Mutex.new()
25buffer = Array.new()
26sucess = Array.new()
27threads = Array.new()
28
29begin
30 Range.new(XMIN,XMAX).map do |x|
31 Range.new(YMIN,YMAX).map do |y|
32 t = Thread.new do
33 ssh_args = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
34 user = 'root'
35 host="node#{x}-#{y}"
36
37 ifconfig = "ifconfig eth0 inet 192.168.#{x}.#{y} up"
38 cmd = "ping -c 1 -I eth0 -b 192.168.0.1"
39
40 #grab just eth0 HW address
41 hwaddr = "ifconfig | grep HWaddr | awk '{split($0,a,\" \"); print a[5]}' | head -1"
42
43 if flag.nil?
44 `ssh #{ssh_args} #{user}@node#{x}-#{y} #{ifconfig} 1>/dev/null 2>/dev/null`
45 end
46
47 unless flag == "-m"
48 res = `ssh #{ssh_args} #{user}@node#{x}-#{y} #{cmd} 1>/dev/null 2>/dev/null`
49 end
50
51 unless flag == "-p"
52 res = `ssh #{ssh_args} #{user}@node#{x}-#{y} #{hwaddr} 2>/dev/null`
53 end
54
55 buffer.push("#{x},#{y} = #{res}")
56
57 LOG.debug("Results was: #{res.chomp} for #{x},#{y}")
58 LOCK.synchronize {sucess.push([x,y]) unless res.nil?}
59
60 end
61 threads.push(t)
62 end
63 end
64 threads.map{|t| t.join}
65rescue => err
66 LOG.fatal("Caught exception; exiting")
67 LOG.fatal(err)
68ensure
69 LOG.debug("Sucessfull nodes:[#{sucess.map{|b|"[#{b[0]},#{b[1]}]"}.join(",")}]")
70 LOG.debug("Number of sucesses: #{sucess.length}")
71 puts buffer.sort
72end
73