Advertisement
bobdodds

.ifaces - myip ifps ifstr

Nov 25th, 2020 (edited)
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. # ~/bin/.bash/.ifaces https://pastebin.com/DiPUdqPJ
  2.  
  3. myip() {
  4. [[ "$1" =~ --FoolMeOnce ]] && s='' || s='https://'
  5. ( for site in ipaddr.pub/cli ipecho.net/plain icanhazip.com ifconfig.me \
  6. ipconfig.in/ip ident.me bot.whatismyipaddress.com diagnostic.opendns.com/myip \
  7. checkip.amazonaws.com trackip.net/ip api.ipify.org tnx.nl/ip ip.tyk.nu \
  8. l2.io/ip wgetip.com
  9. do echo "$site "
  10. wget -qO- ${s}$site
  11. echo
  12. done
  13. wget -qO- ${s}checkip.dyndns.org |\
  14. sed -n -E '/IP Address/s/^.*:[\t ]+([^<]+).*$/checkip.dyndns.org \n\1/p' ) |\
  15. sed -n -E '/^$/d;H;${g;s/^[\n]+//;s/( )\n/ /g;p;}'
  16. wget -qO- ${s}ipinfo.io | sed '1s/.*/ipinfo.io:/;$d;'
  17. }
  18.  
  19. ifps() {
  20. oneline() {
  21. sed -n -E '
  22. s/.* //;
  23. G;
  24. /^([^\n]+).*\n\1(\n|$)/!{
  25. s/^([^\n]+)\n(.*)/\2\n\1/;
  26. h;
  27. };
  28. ${
  29. g;
  30. s/([^\n])(\n)([^\n])/\1,\3/g;
  31. s/^\n/:/;
  32. p;
  33. }'
  34. }
  35. sp='[\t ]*($|\n)' ; W='[\t ]+' ; w='[^\t ]+'
  36. for ip in $* `[ -z "$*" ] && \
  37. ss -tupla40H |\
  38. sed -n -E "
  39. s/%$w//;
  40. s/^${w}${W}${w}${W}${w}${W}${w}${W}($w):.*/\1/;
  41. tb;
  42. d;
  43. :b;
  44. /^[1-9]/{
  45. G;
  46. /^([^\n]+).*\n(\1)($|\n)/bo;
  47. s/\n.*//;
  48. P;
  49. H;
  50. :o;
  51. };"`
  52. do sudo echo -n $ip
  53. sudo ss -tupla40H |\
  54. sed -n -E "
  55. /${ip}:/{
  56. s/%$w//;
  57. /pid=/b pid;
  58. s/^($w)$W$w$W$w$W$w$W($w).*$/\2 \1/;
  59. b out;
  60. :pid;
  61. s/^($w)$W$w$W$w$W$w$W($w)$W$w[^\"]+.([^\"]+).[^=]+.([0-9]+).*${sp}/\2 \1 \3\/\4\5/;
  62. :out;
  63. /^0/d;
  64. s/^([^:]+).([^ ]+).[^ ]+.(.*)/\1 \2=\3/p;
  65. }" | oneline
  66. done
  67. }
  68.  
  69. # ifstate
  70. # UP enx001c2314614d=00:1c:23:14:61:4d,192.168.0.1
  71. # DOWN wlx001cbfb4f948=00:1c:bf:b4:f9:48
  72. # UP enx0c5b8f279a64=0c:5b:8f:27:9a:64,192.168.8.100
  73. # ifstate -v
  74. # DOWN() wlp12s0=00:1c:bf:b4:f9:48
  75. # UNKNOWN(UP,LOWER_UP) enp0s29f7u4=0c:5b:8f:27:9a:64,192.168.8.100
  76. # UP(UP,LOWER_UP) enx001c2314614d=00:1c:23:14:61:4d
  77. # ifstate -v enx001c2314614d
  78. # UP enx001c2314614d=00:1c:23:14:61:4d,192.168.0.1
  79. # ifstate -v UP
  80. # UP enx001c2314614d=00:1c:23:14:61:4d,192.168.0.1
  81. # UP enx0c5b8f279a64=0c:5b:8f:27:9a:64,192.168.8.100
  82. #
  83. # ifstr
  84. # enx001c2314614d 00:1c:23:14:61:4d 192.168.0.1
  85. # wlx001cbfb4f948 00:1c:bf:b4:f9:48
  86. # enx0c5b8f279a64 0c:5b:8f:27:9a:64 192.168.8.100
  87. # ifstr -m enx001c2314614d
  88. # 00:1c:23:14:61:4d
  89. # ifstr -i 192.168.8.100
  90. # enx0c5b8f279a64
  91. # ifstr -a enx001c2314614d
  92. # 192.168.0.1
  93. # ifstr 00:1c:23:14:61:4d
  94. # enx001c2314614d 00:1c:23:14:61:4d 192.168.0.1
  95. #
  96. # rt_tables, making additional routing tables for two or more interfaces
  97.  
  98. if [ -n "`which nmcli`" ]
  99. then ifinfo() {
  100. local l='' u=''
  101. for c in {a..z}
  102. do l=${l}$c
  103. done
  104. for c in {A..Z}
  105. do u=${u}$c
  106. done
  107. nmcli -f GENERAL.DEVICE,GENERAL.HWADDR,IP4.ADDRESS device show |\
  108. sed -n \
  109. -e '/GEN.*DEVICE/{
  110. s/.*:[\t ]\+//;
  111. h;
  112. }' \
  113. -e '/GEN.*HWADDR/{
  114. s/.*:[\t ]\+//;
  115. H;
  116. }' \
  117. -e '/IP4.*ADDRESS/{
  118. s/\(.*:[\t ]\+\)\([^/]\+\)\(.*$\)/\2/;
  119. H;
  120. }' \
  121. -e "/^$/{
  122. g;
  123. s/\n/=/;
  124. s/\n/,/;
  125. y/$u/$l/;
  126. s/^.*/DOWN &/;
  127. /,/s/^[^ ]\+ /UP /;
  128. p;
  129. }" |\
  130. sort
  131. }
  132. ifsi() {
  133. local l='' u=''
  134. for c in {a..z}
  135. do l=${l}$c
  136. done
  137. for c in {A..Z}
  138. do u=${u}$c
  139. done
  140. nmcli -f GENERAL.DEVICE,GENERAL.HWADDR,IP4.ADDRESS device show |\
  141. sed -n \
  142. -e '/GEN.*DEVICE/{
  143. s/.*:[\t ]\+//;
  144. h;
  145. }' \
  146. -e '/GEN.*HWADDR/{
  147. s/.*:[\t ]\+//;
  148. H;
  149. }' \
  150. -e '/IP4.*ADDRESS/{
  151. s/\(.*:[\t ]\+\)\([^/]\+\)\(.*$\)/\2/;
  152. H;
  153. }' \
  154. -e "/^$/{
  155. g;
  156. s/\n/=/;
  157. s/\n/,/;
  158. y/$u/$l/;
  159. p;
  160. }" |\
  161. sort
  162. }
  163. else
  164. ifinfo() { (
  165. ip -o -4 address |\
  166. sed -n -e 's/\(^[0-9]\+:[\t ]\+\)\([ew][^\t ]\+\)\([^0-9]\+\)\([^\/]\+\)\(.*$\)/\2=,\4/p' ;
  167. ip -o -4 link |\
  168. sed -n \
  169. -e 's/\(^[0-9]:[\t ]\+\)\([ew][^:]\+\)\(:[\t ]\+.*CAST,*\)\([^>]*\)\(.*state[\t ]\+\)\([DU][NOP][KNOW]*\)\(.*ether[\t ]\+\)\([^\t ]\+\)\(.*$\)/\2=\8 \6(\4)/p' ) | \
  170. sort | \
  171. sed -n -e '/^[ew]/{
  172. s/^.*=,/,/;
  173. H;
  174. }' \
  175. -e '${
  176. G;
  177. s/\n,/,/g;
  178. s/\([ew][0-9a-z]\+=[:0-9a-z]\+\)\([\t ]\+\)\([DU][^\x28]\+\)\(\x28[^\x29]\+\x29\)/\3 \1/g;
  179. s/\n\n/\n/g;
  180. p;
  181. }' | sort | uniq
  182. }
  183. ifsi() { (
  184. ip -o -4 address |\
  185. sed -n -e 's/\(^[0-9]\+:[\t ]\+\)\([ew][^\t ]\+\)\([^0-9]\+\)\([^\/]\+\)\(.*$\)/\2=,\4/p' ;
  186. ip -o -4 link |\
  187. sed -n \
  188. -e 's/\(^[0-9]:[\t ]\+\)\([ew][^:]\+\)\(:[\t ]\+.*CAST,*\)\([^>]*\)\(.*state[\t ]\+\)\([DU][NOP][KNOW]*\)\(.*ether[\t ]\+\)\([^\t ]\+\)\(.*$\)/\2=\8 \6(\4)/p' ) | \
  189. sort | \
  190. sed -n -e '/^[ew]/{
  191. s/^.*=,/,/;
  192. H;
  193. }' \
  194. -e '${
  195. G;
  196. s/\n,/,/g;
  197. s/\([ew][0-9a-z]\+=[:0-9a-z]\+\)\([\t ]\+\)\([DU][^\x28]\+\)\(\x28[^\x29]\+\x29\)/\1/g;
  198. s/\n\n/\n/g;
  199. p;
  200. }' | sort | uniq
  201. }
  202. fi
  203.  
  204. ifstate() {
  205. local iface_or_mac_or_ip_or_updown=''
  206. [ "$1" = "-v" ] && \
  207. iface_or_mac_or_ip_or_updown="$2" || \
  208. iface_or_mac_or_ip_or_updown="$1" # empty is OK
  209. if [ "$1" = '-v' ] && [ -n "$iface_or_mac_or_ip_or_updown" ]
  210. then ifinfo | sed -n -e "/$iface_or_mac_or_ip_or_updown/p"
  211. elif [ "$1" = '-v' ]
  212. then ifinfo
  213. elif [ -n "$iface_or_mac_or_ip_or_updown" ]
  214. then ifinfo | sed -n -e "/$iface_or_mac_or_ip_or_updown/{s/[(].*[)]//;p;}"
  215. else
  216. ifinfo
  217. fi
  218. }
  219.  
  220. ifstr() {
  221. local a=( `ifsi` ) s=''
  222. if [ "$1" = "-a" ] && [ -n "$2" ]
  223. then for s in ${a[@]}
  224. do [[ "$s" =~ $2 ]] && [[ "$s" =~ ',' ]] && echo ${s/*,/}
  225. done
  226. elif [ "$1" = "-m" ] && [ -n "$2" ]
  227. then for s in ${a[@]}
  228. do [[ "$s" =~ $2 ]] && s=${s/*=/} && echo ${s/,*/}
  229. done
  230. elif [ "$1" = "-i" ] && [ -n "$2" ]
  231. then for s in ${a[@]}
  232. do [[ "$s" =~ $2 ]] && echo ${s/=*/}
  233. done
  234. elif [ -n "$1" ]
  235. then for s in ${a[@]}
  236. do [[ "$s" =~ $1 ]] && echo $s | tr '=,' ' '
  237. done
  238. else
  239. ifsi | tr '=,' ' '
  240. fi
  241. }
  242.  
  243. # https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System
  244. rt_tables() { # take interface, ip, gateway, and derive network, assume mask
  245. local a=("$@")
  246. set ${!a[@]}
  247. rm /tmp/rt_tables 2>/dev/null
  248. local r=`for i ; do case $i in 2|5|8|11|14|17|20|23) n=$[ ( $i + 1 ) / 3 + 1 ] ; echo -n $[ $n - 1 ] rt$n\\\n ;; esac ; done`
  249. #echo $r
  250. sed -n \
  251. -e '/^[1-9][\t ]*rt[2-9]/d' \
  252. -e '1,/#1[\t ]inr.ruhep/{H;}' \
  253. -e "\${g;s/^.*\$/&\n${r}/p;q;}" /etc/iproute2/rt_tables | \
  254. sed -e '/^$/d' > /tmp/rt_tables
  255. [ -s /tmp/rt_tables ] && sudo cp /tmp/rt_tables /etc/iproute2/rt_tables
  256. # or in a file ending in .conf, in /etc/iproute2/rt_tables.d
  257. # set ${!a[@]}
  258. # rm /tmp/interfaces 2>/dev/null
  259. # local r=`
  260. #for i ; do case $i in 2|5|8|11|14|17|20|23) n=$[ ( $i + 1 ) / 3 + 1 ] ; echo -n $n rt$n\\\n ;; esac ; done
  261. #`
  262. }
  263.  
  264. #rt_tables a b c
  265. #return
  266. #exit 0
  267.  
  268. # commandline--
  269. #ip rule add from 192.168.0.1/32 table rt2
  270. #ip rule add to 192.168.0.1/32 table rt2
  271. #ip route add 192.168.0.0/24 dev enx001c2314614d src 192.168.0.1 table rt2
  272. # may get in trouble next line, if gw is not one hop off:
  273. #ip route add default via 192.168.0.1 dev enx001c2314614d table rt2
  274. #ip route list table rt2
  275. #ip rule show
  276. #
  277. ## interfaces(5) file used by ifup(8) and ifdown(8)
  278. #auto lo
  279. #iface lo inet loopback
  280. #
  281. #auto enx001c2314614d
  282. #iface enx001c2314614d inet static
  283. #address 192.168.0.1
  284. #netmask 255.255.255.0
  285. #gateway 192.168.0.1
  286. #dns-nameservers 192.168.0.1
  287. #up sleep 3 ; ip address replace 192.168.0.1 dev enx001c2314614d || true
  288. #up sleep 3 ; ip route add 192.168.0.0/24 dev enx001c2314614d src 192.168.0.1 table rt2 || true
  289. #up sleep 3 ; ip route add default via 192.168.0.1 dev enx001c2314614d table rt2 || true
  290. # leaving unsaid much usually said about a gw--one-hop off for others--if not 1 hop off from us
  291.  
  292.  
  293.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement