Module: Ronin::Core::System

Defined in:
lib/ronin/core/system.rb

Overview

Utility methods for interacting with the system.

Since:

  • 0.2.0

Defined Under Namespace

Classes: DownloadFailed

Class Method Summary collapse

Class Method Details

.download(url, dest) ⇒ String

Downloads a file using either curl or wget.

Parameters:

  • url (URI::HTTP, String)

    The URL to download.

  • dest (String)

    The destination file or directory to download into.

Returns:

  • (String)

    The path to the downloaded file.

Raises:

  • (DownloadFailed)

    The download failed or curl/wget could not be found on the system.

Since:

  • 0.2.0



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
137
138
139
140
141
# File 'lib/ronin/core/system.rb', line 112

def self.download(url,dest)
  uri = URI(url)
  url = url.to_s

  if File.directory?(dest)
    dest = File.join(dest,File.basename(uri.path))
  end

  FileUtils.mkdir_p(File.dirname(dest))

  partial_dest = "#{dest}.part"

  case downloader
  when :wget
    unless system('wget','-c','-O',partial_dest,url)
      raise(DownloadFailed,"wget command failed: wget -c -O #{partial_dest} #{url}")
    end
  when :curl
    unless system('curl','-f','-L','-C','-','-o',partial_dest,url)
      raise(DownloadFailed,"curl command failed: curl -f -L -C - -o #{partial_dest} #{url}")
    end
  when nil
    raise(DownloadFailed,"could not find 'curl' or 'wget' on the system")
  else
    raise(NotImplementedError,"downloader not supported: #{downloader.inspect}")
  end

  FileUtils.mv(partial_dest,dest)
  return dest
end

.downloader'curl', ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The detected download command.

Returns:

  • ('curl', 'wget', nil)

Since:

  • 0.2.0



82
83
84
85
86
87
88
89
# File 'lib/ronin/core/system.rb', line 82

def self.downloader
  @downloader ||= if os == :macos then :curl
                  else
                    if    installed?('wget') then :wget
                    elsif installed?('curl') then :curl
                    end
                  end
end

.installed?(command) ⇒ Boolean

Determines if a command is installed.

Parameters:

  • command (String)

    The command name to search for.

Returns:

  • (Boolean)

    Indicates whether the command was found or not.

Since:

  • 0.2.0



69
70
71
72
73
# File 'lib/ronin/core/system.rb', line 69

def self.installed?(command)
  paths.any? do |bin_dir|
    File.executable?(File.join(bin_dir,command))
  end
end

.os:linux, ...

The detected Operating System type.

Returns:

  • (:linux, :macos, :freebsd, :openbsd, :netbsd, :windows, nil)

Since:

  • 0.2.0



37
38
39
40
41
42
43
44
45
# File 'lib/ronin/core/system.rb', line 37

def self.os
  @os ||= if    RUBY_PLATFORM.include?('linux')   then :linux
          elsif RUBY_PLATFORM.include?('darwin')  then :macos
          elsif RUBY_PLATFORM.include?('freebsd') then :freebsd
          elsif RUBY_PLATFORM.include?('openbsd') then :openbsd
          elsif RUBY_PLATFORM.include?('netbsd')  then :netbsd
          elsif Gem.win_platform?                 then :windows
          end
end

.pathsArray<String>

The list of bin/ directories containing executables.

Returns:

  • (Array<String>)

Since:

  • 0.2.0



52
53
54
55
56
57
58
# File 'lib/ronin/core/system.rb', line 52

def self.paths
  @paths ||= if ENV['PATH']
               ENV['PATH'].split(File::PATH_SEPARATOR)
             else
               []
             end
end