Skip to content

Instantly share code, notes, and snippets.

@alext
Created January 9, 2017 10:36
Show Gist options
  • Save alext/8c724f0104e0459bdce684cfd00fb7f2 to your computer and use it in GitHub Desktop.
Save alext/8c724f0104e0459bdce684cfd00fb7f2 to your computer and use it in GitHub Desktop.
Add AWS IP ranges to vpn config file.
#!/usr/bin/env ruby
# This script outputs a series of lines that can be added to the [ipv4] section
# of a NetworkManager VPN config file (typically found in
# /etc/NetworkManager/system-connections/)
#
# ./aws_vpn_routes.rb | sudo tee -a /etc/NetworkManager/system-connections/<connection_name>
require 'net/http'
require 'json'
AWS_IPRANGES_ENDPOINT='https://ip-ranges.amazonaws.com/ip-ranges.json'
def get_ipranges_data
resp = Net::HTTP.get_response(URI.parse(AWS_IPRANGES_ENDPOINT))
unless resp.is_a?(Net::HTTPSuccess)
abort "Error downloading ipranges data: #{resp.code} #{resp.message}\n#{resp.body}"
end
JSON.parse(resp.body)
end
get_ipranges_data.fetch("prefixes").each_with_index do |prefix, i|
ip, masklen = prefix.fetch("ip_prefix").split('/', 2)
puts "routes#{i+1}=#{ip};#{masklen};0.0.0.0;0;"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment