-
Notifications
You must be signed in to change notification settings - Fork 14.2k
/
Copy pathplatform.rb
136 lines (134 loc) · 5.74 KB
/
platform.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
module Metasploit
module Framework
module Ssh
module Platform
def self.get_platform(ssh_socket)
info = get_platform_info(ssh_socket, timeout: 10)
get_platform_from_info(info)
end
def self.get_platform_info(ssh_socket, timeout: 10)
info = ''
begin
Timeout.timeout(timeout) do
info = ssh_socket.exec!("id\n").to_s
if (info =~ /id=/)
info << ssh_socket.exec!("uname -a\n").to_s
if (info =~ /JUNOS /)
# We're in the SSH shell for a Juniper JunOS, we can pull the version from the cli
# line 2 is hostname, 3 is model, 4 is the Base OS version
info = ssh_socket.exec!("cli show version\n").split("\n")[2..4].join(', ').to_s
elsif (info =~ /Linux USG /)
# Ubiquiti Unifi USG
info << ssh_socket.exec!("cat /etc/version\n").to_s.rstrip
end
temp_proof = ssh_socket.exec!("grep unifi.version /tmp/system.cfg\n").to_s.rstrip
if (temp_proof =~ /unifi\.version/)
info << temp_proof
# Ubiquiti Unifi device (non-USG), possibly a switch. Tested on US-24, UAP-nanoHD
# The /tmp/*.cfg files don't give us device info, however the info command does
# we dont call it originally since it doesnt say unifi/ubiquiti in it and info
# is a linux command as well
info << ssh_socket.exec!("grep board.name /etc/board.info\n").to_s.rstrip
end
elsif info =~ /Unknown command or computer name/
# Cisco IOS
info = ssh_socket.exec!("ver\n").to_s
# Juniper ScreenOS
elsif info =~ /unknown keyword/
info = ssh_socket.exec!("get chassis\n").to_s
# Juniper JunOS CLI
elsif info =~ /unknown command: id/
info = ssh_socket.exec!("show version\n").split("\n")[2..4].join(', ').to_s
# Brocade CLI
elsif info =~ /Invalid input -> id/ || info =~ /Protocol error, doesn't start with scp!/
info = ssh_socket.exec!("show version\n").to_s
if info =~ /Version:(?<os_version>.+).+HW: (?<hardware>)/mi
info = "Model: #{hardware}, OS: #{os_version}"
end
# Arista
elsif info =~ /% Invalid input at line 1/
info = ssh_socket.exec!("show version\n").split("\n")[0..1]
info = info.map { |item| item.strip }
info = info.join(', ').to_s
# Windows
elsif info =~ /command not found|is not recognized as an internal or external command/
info = ssh_socket.exec!("systeminfo\n").to_s
/OS Name:\s+(?<os_name>.+)$/ =~ info
/OS Version:\s+(?<os_num>.+)$/ =~ info
if os_num.present? && os_name.present?
info = "#{os_name.strip} #{os_num.strip}"
else
info = ssh_socket.exec!("ver\n").to_s.strip
end
# mikrotik
elsif info =~ /bad command name id \(line 1 column 1\)/
info = ssh_socket.exec!("/ system resource print\n").to_s
/platform:\s+(?<platform>.+)$/ =~ info
/board-name:\s+(?<board>.+)$/ =~ info
/version:\s+(?<version>.+)$/ =~ info
if version && platform && board
info = "#{platform.strip} #{board.strip} #{version.strip}"
end
# esxi 6.7
elsif info =~ /sh: id: not found/
info = ssh_socket.exec!("vmware -v\n").to_s
# vcenter 6.7 (photon)
# VMware vCenter Server 8.0.0.10000
# VMware VirtualCenter 6.7.0 build-19299595
elsif info =~ /Unknown command: `id'/
# eventually we'll want to try to shell in via 'shell'. On failure you see: "User 'user_operator' is not authorized to run this command"
# on succeess: "Shell access is granted to <username>"
info = ssh_socket.exec!("api com.vmware.appliance.version1.system.version.get\n\n").to_s
/Product:\s+(?<product>.+)$/ =~ info
/Version:\s+(?<version>[\d\.]+)$/ =~ info
if version && product
info = "#{product.strip} #{version.strip}"
end
else
info << ssh_socket.exec!("help\n?\n\n\n").to_s
end
end
rescue Timeout::Error
end
info
end
def self.get_platform_from_info(info)
case info
when /unifi\.version|UniFiSecurityGateway/i # Ubiquiti Unifi. uname -a is left in, so we got to pull before Linux
'unifi'
when /Linux/i
'linux'
when /VMware ESXi/i
'linux'
when /Darwin/i
'osx'
when /SunOS/i
'solaris'
when /BSD/i
'bsd'
when /HP-UX/i
'hpux'
when /AIX/i
'aix'
when /MSYS_NT|cygwin|Win32|Windows|Microsoft/i
'windows'
when /Unknown command or computer name|Line has invalid autocommand/i
'cisco-ios'
when /unknown keyword/i # ScreenOS
'juniper'
when /JUNOS Base OS/i # JunOS
'juniper'
when /MikroTik/i
'mikrotik'
when /Arista/i
'arista'
when /VMware vCenter Server/i
'vcenter'
else
'unknown'
end
end
end
end
end
end