Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'net/telnet'
- require 'mysql2'
- def connect(a,log,pas)
- i = Net::Telnet::new("Host" => a,
- "Port" => 23,
- "Output_log" => "output.log",
- "Dump_log" => "dump.log",
- "Prompt" => /#/ ,
- "Telnetmode" => true,
- "Timeout" => 5,
- )
- i.print(log)
- i.print("\n")
- i.print(pas)
- i.print("\n")
- i.waitfor(/#/)
- i.cmd("dis cli")
- std = i.cmd("show switch")
- i.cmd("enable cli")
- #$out = $std.split(/.*:.*/)
- i.close
- return std
- end
- def get_inf(addr, log, pas)
- print (addr + " connect\n")
- client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "123", :database => "snmp")
- ip=addr
- notcon = 0
- # print("\n")
- begin
- info = connect(ip, log, pas)
- mac1 = (info.scan(/MAC Address[\s,\S]*?\n\r/))
- mac = mac1[0].split.last(5)[3]
- name1 = (info.scan(/Device Type[\s,\S]*?\n\r/))
- name = name1[0].split.last(5)[1]
- if name==":"
- name = name1[0].split.last(5)[2]
- end
- firm1=(info.scan(/Firmware Version[\s,\S]*?\n\r/))
- firm=firm1[0].split.last(5)[4]
- rescue
- notcon=1
- print("not available... ")
- print(addr)
- print("\n")
- $repsw << addr
- end
- if notcon==0 then
- begin
- #print name
- #print ("\n")
- #print mac
- #print ("\n")
- #print firm
- #print ("\n")
- client.query("insert into switch (ip,model,mac,firmware,time) values ('#{ip}', '#{name}', '#{mac}', '#{firm}', NOW())")
- rescue
- begin
- client.query("UPDATE switch SET switch.model='#{name}', switch.mac='#{mac}', switch.firmware='#{firm}', switch.time=NOW() WHERE switch.ip='#{ip}';")
- #print ("запись обновлена")
- #print ("\n")
- end
- end
- end
- if notcon==1 then
- begin
- #print ("\n")
- #print ("Не удалось выполнить подключение ")
- #print ip
- #print ("\n")
- end
- end
- end
- =begin
- $iplist = Array.new
- results = $client.query("select * from switch")
- results.each do |row|
- puts row["ip"]
- if row["mac"]
- puts row["dne"]
- end
- end
- =end
- $ip = Array.new
- sqlcon = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "123", :database => "snmp")
- sqlcon.query("select ip.ip from ip where ip.check=1;").each do |row|
- $ip << row["ip"]
- end
- $repsw = Array.new
- start=(Time.new)
- cur_th=$ip.size
- a1=0
- while a1<cur_th do
- # print("thread 1 ")
- # print a1
- # print("\n")
- get_inf($ip[a1],"login","password")
- a1+=1
- end
- print("connection error for: \n")
- print($repsw)
- if $repsw.size!=0
- begin
- print("\ntry to reconnect... ")
- cur_th=$repsw.size
- a1=0
- while a1<cur_th do
- # print("thread 1 ")
- # print a1
- # print("\n")
- get_inf($repsw[a1],"login","password")
- get_inf($repsw[a1],"login","password")
- a1+=1
- end
- end
- end
- #thread4 = Thread.new{get_inf($ip[3])}
- #thread1.join
- #print(i)
- print(start)
- print("\n")
- print(Time.new)
- print("\n")
- print(Time.new-start)
- print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement