create method to create file for live openwrt routers

This commit is contained in:
Torsten 2022-03-28 00:52:49 +03:00
parent b0542808cb
commit 435229680d

View File

@ -4,8 +4,8 @@ require "net/ssh"
class Routers class Routers
@@start_address = "10.20.20." @@start_address = "10.20.20."
@@start_at = 30 @@start_at = 20
@@stop_at = 80 @@stop_at = 85
def initialize def initialize
@at = @@start_at @at = @@start_at
@ -16,16 +16,13 @@ class Routers
end end
# print info for each ip # print info for each ip
def print( hosts = false) def print( info = nil)
info = "host_name" unless info
ips(false) do |ip , connection| ips(false) do |ip , connection|
if connection.is_a?(String) if connection.is_a?(String)
message = connection message = connection
else else
if(hosts) message = send(info.to_sym , connection)
message = admin_name(connection)
else
message = "ok"
end
end end
puts "#{ip} = #{message}" puts "#{ip} = #{message}"
end end
@ -50,18 +47,57 @@ class Routers
end end
end end
def scan
info = {}
ips(false) do |ip , connection|
if connection.is_a?(String)
message = connection
else
info[ip] = get_info(connection)
message = info[ip][:machine]
end
puts "#{ip} = #{message}"
end
r_file = File.open("#{Rails.root}/config/routers.yml" , "w")
r_file.write(info.to_yaml)
puts "Routers file written , #{info.length} live"
end
def each_router
routers = YAML::load(File.open("#{Rails.root}/config/routers.yml"))
routers.each do |ip ,info|
connection = Net::SSH.start(at , "root" ,
password: Rails.application.credentials.sala,
non_interactive: true ,
timeout: 4)
yield( ip , connection)
connection.close
end
end
private private
# get the hostname from the connection # get the hostname from the connection
# (as opposed to the admin network name) # (as opposed to the admin network name)
def host_name(connection) def host_name(connection)
connection.exec! "uci get system.@system[0].hostname" connection.exec!( "uci get system.@system[0].hostname").strip
end end
# get the admin name, ie the dhcp name of the admin interface # get the admin name, ie the dhcp name of the admin interface
def admin_name(connection) def admin_name(connection)
connection.exec! "uci get network.admin.hostname" connection.exec!( "uci get network.admin.hostname").strip
end end
def cpuinfo(connection)
connection.exec! "cat /proc/cpuinfo"
end
def machine(connection)
cpuinfo(connection).lines[1].split(":")[1].strip
end
def get_info(connection)
{machine: machine(connection) , host_name: admin_name(connection)}
end
# takes a block and yields ip and connection to # takes a block and yields ip and connection to
# every succesful connection # every succesful connection
def ips(live_only = true) def ips(live_only = true)