Advertisement
marioq

myOutIP

Oct 17th, 2016
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.87 KB | None | 0 0
  1. #!/bin/bash
  2. # https://en.wikipedia.org/wiki/IPv6_address
  3.  
  4. NET_iface="`ip route | grep -n default | sort -b -k 9 | awk 'NR==1 {print $5}'`"
  5. if [ -z $NET_iface ]; then
  6.     echo " Non c'è connessione di rete locale ...??"
  7.     exit 1
  8. else
  9.     PING_to="gnu.org"
  10.     # PING_to="freegeoip.net"
  11.     # PING_to="ae-1-3101.edge3.Dusseldorf1.Level3.net"
  12.     # nc -zv freegeoip.net 80
  13.     # echo -e "GET /xml/ipv6.google.com HTTP/1.0\r\n\r\n" | nc -i1 freegeoip.net 80
  14.  
  15.     ping -c 1 $PING_to 2>/dev/null 1>/dev/null
  16.     PING_EXIT=$?
  17.     if [ $PING_EXIT -gt 0 ]; then
  18.         echo " \"ping -c 1 $PING_to\" exit $PING_EXIT"
  19.         echo " Non c'è connettivita su Internet"
  20.         exit $PING_EXIT
  21.     fi
  22. fi
  23.  
  24. # from a terminal try this
  25. # echo "this is not ip4 999.999.999.999" | grep -Eo "([0-9]{1,3}.){3}[[0-9]{1,3}" ; echo grep error $?
  26. # echo "this is ip4 1.1.1.1" | grep -Eo "([0-9]{1,3}.){3}[[0-9]{1,3}" ; echo grep error $?
  27. # echo "this is not ip4 999.999.999.999" | grep -Eo "`rgxg cidr 0.0.0.0/0`|`rgxg cidr ::/0`" ; echo grep error $?
  28. # echo "this is ip4 1.1.1.1/16 this is ip6 fe80::7627:eaff:fe21:37e ip6 ::/64 no ip4 256.1.1.256" | grep -Eo "`rgxg cidr 0.0.0.0/0`|`rgxg cidr ::/0`" ; echo grep error $?
  29. # ifconfig | grep -Eo "`rgxg cidr 0.0.0.0/0`|`rgxg cidr ::/0`"
  30.  
  31. RGXG="`which rgxg`"
  32. if [ -z $RGXG ]; then
  33.     # this is the output of "rgxg cidr 0.0.0.0/0", all ip4 address
  34.     REGEX_ALL_ip4="(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}"
  35.     # this is the output of "rgxg cidr ::/0", all ip6 address
  36.     REGEX_ALL_ip6="((:(:[0-9A-Fa-f]{1,4}){1,7}|::|[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4}){1,6}|::|:[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4}){1,5}|::|:[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4}){1,4}|::|:[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4}){1,3}|::|:[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4}){1,2}|::|:[0-9A-Fa-f]{1,4}(::[0-9A-Fa-f]{1,4}|::|:[0-9A-Fa-f]{1,4}(::|:[0-9A-Fa-f]{1,4}))))))))|(:(:[0-9A-Fa-f]{1,4}){0,5}|[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4}){0,4}|:[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4}){0,3}|:[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4}){0,2}|:[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4})?|:[0-9A-Fa-f]{1,4}(:|:[0-9A-Fa-f]{1,4})))))):(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3})"
  37. else
  38.     REGEX_ALL_ip4="`$RGXG cidr 0.0.0.0/0`" # all ip4
  39.     REGEX_ALL_ip6="`$RGXG cidr ::/0`" # all ip6
  40. fi
  41.  
  42. # GEOIP_service="geoip.ubuntu.com/lookup/"  # only your IP
  43. # GEOIP_service="freegeoip.net/xml/"   # all domain (it was open source, now https://ipstack.com )
  44. GEOIP_service="freegeoip.app/xml/"   # all domain (fork of freegeoip.net)
  45. # GEOIP_service="freegeoip.app/json/"
  46. # GEOIP_service="freegeoip.app/csv/"
  47.  
  48.  # curl
  49.  # -L, --location      Follow redirects (H)
  50.  # -s, --silent        Silent mode (don't output anything)
  51.  # -A, --user-agent STRING  Send User-Agent STRING to server (H)
  52. curl -s -L -A "`basename $0`" $GEOIP_service$1 | grep -Eo "$REGEX_ALL_ip6|$REGEX_ALL_ip4" ; exit $?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement