Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require "pty"
- require "expect"
- require "json"
- require "timeout"
- @output = []
- @zdinfo = {}
- def harvest_info(buffers)
- buffers.each do |buffer|
- buffer.lines.each do |line|
- next if line.match(/.*= .*/) do
- a = line.chop.strip
- key, value = a.split "= ", 2
- @zdinfo[key] = value
- end
- end
- end
- @zdinfo
- end
- @zdinfo = { "zd_ip" => "10.57.169.2",device_ip: "10.57.169.2", access_port: 22, username: "support", password: "ihintl"}
- begin
- Timeout.timeout(10) do
- PTY.spawn("ssh -p #{@zdinfo[:access_port] == 0 ? 22 : @zdinfo[:access_port] || '22'} \
- -o UserKnownHostsFile=/dev/null \
- -o StrictHostKeyChecking=no \
- #{@zdinfo[:device_ip]}"
- ) do |ssh_out, ssh_in|
- begin
- ssh_out.expect(/ease login: /) { ssh_in.printf("#{@zdinfo[:username]}\n") }
- ssh_out.expect(/assword: /) { ssh_in.printf("#{@zdinfo[:password]}\n") }
- ssh_out.expect(/uckus> /) { ssh_in.printf("enable\n") }
- ssh_out.expect(/uckus# /) { ssh_in.printf("config\n") }
- ssh_out.expect(/uckus[(]config[)]# /) { ssh_in.printf("system\n") }
- ssh_out.expect(/uckus[(]config-sys[)]# /) { ssh_in.printf("snmpv3\n") }
- ssh_out.expect(/uckus[(]config-sys-snmpv3[)]# /) { ssh_in.printf("rw-user ihotelrw md5 ihotelrw des ihotelrw\n") }
- ssh_out.expect(/uckus[(]config-sys-snmpv3[)]# /) { ssh_in.printf("ro-user ihotelro md5 ihotelro des ihotelro\n") }
- ssh_out.expect(/uckus[(]config-sys-snmpv3[)]# /) { ssh_in.printf("end\n") }
- ssh_in.printf("quit\n\n")
- ssh_in.printf("quit\n\n")
- ssh_in.printf("quit\n\n")
- rescue Errno::EIO
- errormsg = { "error" => "ssh not available" }
- @output.push @zdinfo.merge(errormsg)
- end
- end
- end
- rescue Timeout::Error
- errormsg = { "error" => "bad credentials" }
- @output.push @zdinfo.merge(errormsg)
- end
- puts @output.to_a.empty? && "" || @output.to_a.to_json
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement