Advertisement
Guest User

Untitled

a guest
May 18th, 2016
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. require 'net/telnet'
  2. require 'mysql2'
  3. def connect(a,log,pas)
  4. i = Net::Telnet::new("Host" => a,
  5. "Port" => 23,
  6. "Output_log" => "output.log",
  7. "Dump_log" => "dump.log",
  8. "Prompt" => /#/ ,
  9. "Telnetmode" => true,
  10. "Timeout" => 5,
  11. )
  12.  
  13.  
  14. i.print(log)
  15. i.print("\n")
  16.  
  17. i.print(pas)
  18. i.print("\n")
  19. i.waitfor(/#/)
  20. i.cmd("dis cli")
  21. std = i.cmd("show switch")
  22. i.cmd("enable cli")
  23. #$out = $std.split(/.*:.*/)
  24.  
  25. i.close
  26. return std
  27. end
  28.  
  29. def get_inf(addr, log, pas)
  30. print (addr + " connect\n")
  31. client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "123", :database => "snmp")
  32. ip=addr
  33. notcon = 0
  34. # print("\n")
  35. begin
  36. info = connect(ip, log, pas)
  37. mac1 = (info.scan(/MAC Address[\s,\S]*?\n\r/))
  38. mac = mac1[0].split.last(5)[3]
  39. name1 = (info.scan(/Device Type[\s,\S]*?\n\r/))
  40. name = name1[0].split.last(5)[1]
  41. if name==":"
  42. name = name1[0].split.last(5)[2]
  43. end
  44. firm1=(info.scan(/Firmware Version[\s,\S]*?\n\r/))
  45. firm=firm1[0].split.last(5)[4]
  46. rescue
  47. notcon=1
  48. print("not available... ")
  49. print(addr)
  50. print("\n")
  51. $repsw << addr
  52. end
  53. if notcon==0 then
  54. begin
  55.  
  56.  
  57. #print name
  58. #print ("\n")
  59. #print mac
  60. #print ("\n")
  61. #print firm
  62. #print ("\n")
  63. client.query("insert into switch (ip,model,mac,firmware,time) values ('#{ip}', '#{name}', '#{mac}', '#{firm}', NOW())")
  64. rescue
  65. begin
  66. client.query("UPDATE switch SET switch.model='#{name}', switch.mac='#{mac}', switch.firmware='#{firm}', switch.time=NOW() WHERE switch.ip='#{ip}';")
  67. #print ("запись обновлена")
  68. #print ("\n")
  69. end
  70. end
  71. end
  72. if notcon==1 then
  73. begin
  74. #print ("\n")
  75. #print ("Не удалось выполнить подключение ")
  76. #print ip
  77. #print ("\n")
  78. end
  79. end
  80. end
  81.  
  82.  
  83.  
  84.  
  85. =begin
  86. $iplist = Array.new
  87. results = $client.query("select * from switch")
  88. results.each do |row|
  89. puts row["ip"]
  90. if row["mac"]
  91. puts row["dne"]
  92. end
  93. end
  94. =end
  95. $ip = Array.new
  96. sqlcon = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "123", :database => "snmp")
  97. sqlcon.query("select ip.ip from ip where ip.check=1;").each do |row|
  98. $ip << row["ip"]
  99. end
  100. $repsw = Array.new
  101. start=(Time.new)
  102.  
  103. cur_th=$ip.size
  104. a1=0
  105. while a1<cur_th do
  106. # print("thread 1 ")
  107. # print a1
  108. # print("\n")
  109.  
  110. get_inf($ip[a1],"login","password")
  111. a1+=1
  112. end
  113.  
  114. print("connection error for: \n")
  115. print($repsw)
  116. if $repsw.size!=0
  117. begin
  118. print("\ntry to reconnect... ")
  119. cur_th=$repsw.size
  120. a1=0
  121. while a1<cur_th do
  122. # print("thread 1 ")
  123. # print a1
  124. # print("\n")
  125.  
  126. get_inf($repsw[a1],"login","password")
  127. get_inf($repsw[a1],"login","password")
  128. a1+=1
  129. end
  130. end
  131. end
  132.  
  133.  
  134.  
  135. #thread4 = Thread.new{get_inf($ip[3])}
  136.  
  137. #thread1.join
  138.  
  139.  
  140. #print(i)
  141. print(start)
  142. print("\n")
  143. print(Time.new)
  144. print("\n")
  145. print(Time.new-start)
  146. print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement