Class: Windows

Inherits:
Object
  • Object
show all
Defined in:
lib/guess_os/type/windows.rb

Class Method Summary collapse

Class Method Details

.guess(host) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/guess_os/type/windows.rb', line 2

def self.guess(host)
  conn = GuessOS::Conn.new(host)

  os = try_with_regedit(conn)
  return os unless os.type == :unkown

  os = try_with_ver(conn)
  return os unless os.type == :unkown

  try_with_folder(conn)
end

.try_with_folder(conn) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/guess_os/type/windows.rb', line 60

def self.try_with_folder(conn)
  # command = 'echo %windir%' => Windows
  command = "cd c:\windows"
  conn.exec(command)

  identified = conn.ok && conn.last_output.include?("Windows")
  return GuessOS::OS.unkown unless identified

  output = conn.last_output
  begin
    lines = output.split("\n")
    filter = lines.filter { _1.include? "Volume" }
    items = filter[0].split
    type = :windows
    name = items[-2, 2].join(" ").to_s
    desc = filter[0]
    GuessOS::OS.new(type, name, desc)
  rescue
    GuessOS::OS.unkown
  end
end

.try_with_regedit(conn) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/guess_os/type/windows.rb', line 14

def self.try_with_regedit(conn)
  command = "reg query \"HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\" /t REG_SZ"
  conn.exec(command)

  identified = conn.ok && conn.last_output.include?("Windows")
  return GuessOS::OS.unkown unless identified

  output = conn.last_output
  begin
    lines = output.split("\n")
    filter = lines.filter { _1.include? "ProductName" }
    filter[0].gsub!("ProductName", "")
    filter[0].gsub!("REG_SZ", "")

    desc = filter[0].strip
    items = desc.split
    name = "#{items[0]} #{items[1]}"

    GuessOS::OS.new(:windows, name, desc)
  rescue
    GuessOS::OS.unkown
  end
end

.try_with_ver(conn) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/guess_os/type/windows.rb', line 38

def self.try_with_ver(conn)
  # ver => Microsoft Windows [Version 10.0.20348.469]
  command = "ver"
  conn.exec(command)

  identified = conn.ok && conn.last_output.include?("Windows")
  return GuessOS::OS.unkown unless identified

  output = conn.last_output
  begin
    output.tr!("\r", "")
    output.tr!("\n", "")
    items = output.split
    type = :windows
    name = "windows #{items[3].split(".").first}"
    desc = output
    GuessOS::OS.new(type, name, desc)
  rescue
    GuessOS::OS.unkown
  end
end