Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require "pty"
  4. require "expect"
  5. require "json"
  6. require "timeout"
  7.  
  8. @output = []
  9. @zdinfo = {}
  10.  
  11. def harvest_info(buffers)
  12. buffers.each do |buffer|
  13. buffer.lines.each do |line|
  14. next if line.match(/.*= .*/) do
  15. a = line.chop.strip
  16. key, value = a.split "= ", 2
  17. @zdinfo[key] = value
  18. end
  19. end
  20. end
  21. @zdinfo
  22. end
  23.  
  24. @zdinfo = { "zd_ip" => "10.57.169.2",device_ip: "10.57.169.2", access_port: 22, username: "support", password: "ihintl"}
  25. begin
  26. Timeout.timeout(10) do
  27. PTY.spawn("ssh -p #{@zdinfo[:access_port] == 0 ? 22 : @zdinfo[:access_port] || '22'} \
  28. -o UserKnownHostsFile=/dev/null \
  29. -o StrictHostKeyChecking=no \
  30. #{@zdinfo[:device_ip]}"
  31. ) do |ssh_out, ssh_in|
  32. begin
  33. ssh_out.expect(/ease login: /) { ssh_in.printf("#{@zdinfo[:username]}\n") }
  34. ssh_out.expect(/assword: /) { ssh_in.printf("#{@zdinfo[:password]}\n") }
  35. ssh_out.expect(/uckus> /) { ssh_in.printf("enable\n") }
  36. ssh_out.expect(/uckus# /) { ssh_in.printf("config\n") }
  37. ssh_out.expect(/uckus[(]config[)]# /) { ssh_in.printf("system\n") }
  38. ssh_out.expect(/uckus[(]config-sys[)]# /) { ssh_in.printf("snmpv3\n") }
  39. ssh_out.expect(/uckus[(]config-sys-snmpv3[)]# /) { ssh_in.printf("rw-user ihotelrw md5 ihotelrw des ihotelrw\n") }
  40. ssh_out.expect(/uckus[(]config-sys-snmpv3[)]# /) { ssh_in.printf("ro-user ihotelro md5 ihotelro des ihotelro\n") }
  41. ssh_out.expect(/uckus[(]config-sys-snmpv3[)]# /) { ssh_in.printf("end\n") }
  42.  
  43. ssh_in.printf("quit\n\n")
  44. ssh_in.printf("quit\n\n")
  45. ssh_in.printf("quit\n\n")
  46.  
  47. rescue Errno::EIO
  48. errormsg = { "error" => "ssh not available" }
  49. @output.push @zdinfo.merge(errormsg)
  50. end
  51. end
  52. end
  53. rescue Timeout::Error
  54. errormsg = { "error" => "bad credentials" }
  55. @output.push @zdinfo.merge(errormsg)
  56. end
  57.  
  58. puts @output.to_a.empty? && "" || @output.to_a.to_json
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement