Skip to content

Instantly share code, notes, and snippets.

/ping.rb Secret

Created November 23, 2015 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/36afed57eb2b45e339c5 to your computer and use it in GitHub Desktop.
Save anonymous/36afed57eb2b45e339c5 to your computer and use it in GitHub Desktop.
require 'socket'
include Socket::Constants
#
# check for root
#
abort("re-run with root priviledges") unless Process::Sys.geteuid.zero?
#
# create a raw socket
#
raw_socket = Socket.new(AF_INET, SOCK_RAW, IPPROTO_ICMP)
#
# bind to specific device
#
raw_socket.setsockopt(SOL_SOCKET, SO_BINDTODEVICE, "tun0")
#
# create ICMP echo request
#
icmp_ereq = [0x08, 0x00, 0x89, 0x98, 0x6e, 0x63, 0x00, 0x04, 0x00].pack('C*')
#
# create ICMP echo reply
#
#icmp_erep = [0x00, 0x00, 0x91, 0x98, 0x6e, 0x00, 0x04, 0x00].pack('C*')
#
# send message
#
raw_socket.send(icmp_ereq, 0, Socket.sockaddr_in(0, '8.8.8.8'))
#
# read reply
#
reply, ai = raw_socket.recvfrom(1024)
if reply
fqdn = ai.getnameinfo[0]
$stdout.puts reply
$stdout.puts "Received reply from: #{fqdn}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment