-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevice.rb
39 lines (33 loc) · 884 Bytes
/
Device.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'net/telnet'
module Device
class DeviceCLI
def initialize ip, port, prompt
prompt_reg = Regexp.new prompt
@telnet = Net::Telnet.new("Host"=>ip, "Port"=>port, "Prompt"=>prompt_reg, "FailEOF"=>true)
end
def login login_prompt, user, password_prompt, password
login_reg = Regexp.new login_prompt
password_reg = Regexp.new password_prompt
@telnet.login("LoginPrompt"=>login_reg, "Name"=>user, "PasswordPrompt"=>password_reg, "Password"=>password) #{|c| print c}
end
def execute cmd_str
cmd = cmd_str.rstrip
str = ''
#block below is for E7-2 who need a "RETURN" to continue after "--MORE--"
str << @telnet.cmd("String"=>cmd, "Timeout"=>60) do |c|
if c =~ /--MORE--/
@telnet.write "\015"
end
end
str
end
def exit
begin
@telnet.cmd "exit"
rescue =>e
puts e.to_s
end
@telnet.close
end
end
end