Class: NetworkTools::VpnTools

Inherits:
Object
  • Object
show all
Defined in:
lib/it_tools/network_tools.rb

Instance Method Summary collapse

Instance Method Details

#is_port_open?(ip, port) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/it_tools/network_tools.rb', line 7

def is_port_open?(ip, port)
  begin
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new(ip, port)
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    end
  rescue Timeout::Error
  end
  return false
end

#login_to_url2(url, port, username, password, limit = 35) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/it_tools/network_tools.rb', line 33

def (url, port, username, password, limit = 35)
  puts "[URL]: " + url
  puts "[PORT]: #{port}"
  puts "[USERNAME]: " + username
  puts "[PASSWORD]: " + password

  # You should choose a better exception.
  raise ArgumentError, 'too many HTTP redirects' if limit == 0

  uri = URI(url)

  req = Net::HTTP::Get.new(uri.request_uri)
  req.basic_auth username, password

  res = Net::HTTP.start(uri.host, port, :use_ssl => uri.scheme == 'https') {|http|
    http.request(req)
  }

  case res
  when Net::HTTPSuccess then
    puts "Successfully logged in." # res.body
  when Net::HTTPRedirection then
    location = res['location']
    warn "redirected to #{location}"
    (location, port, username, password, limit - 1)
  else
    response.value
  end
#    File.open('out.html', 'w'){|f| f.write res.body}
  puts res.body.to_s
end

#on_vpnObject

  • Returns :

    • true if you are on the vpn or false if you are not on the vpn



25
26
27
28
29
30
31
# File 'lib/it_tools/network_tools.rb', line 25

def on_vpn
  if is_port_open?("spicevan.com", 22)
    return false
  else
    return true
  end
end