Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ~/bin/.bash/.ifaces https://pastebin.com/DiPUdqPJ
- myip() {
- [[ "$1" =~ --FoolMeOnce ]] && s='' || s='https://'
- ( for site in ipaddr.pub/cli ipecho.net/plain icanhazip.com ifconfig.me \
- ipconfig.in/ip ident.me bot.whatismyipaddress.com diagnostic.opendns.com/myip \
- checkip.amazonaws.com trackip.net/ip api.ipify.org tnx.nl/ip ip.tyk.nu \
- l2.io/ip wgetip.com
- do echo "$site "
- wget -qO- ${s}$site
- echo
- done
- wget -qO- ${s}checkip.dyndns.org |\
- sed -n -E '/IP Address/s/^.*:[\t ]+([^<]+).*$/checkip.dyndns.org \n\1/p' ) |\
- sed -n -E '/^$/d;H;${g;s/^[\n]+//;s/( )\n/ /g;p;}'
- wget -qO- ${s}ipinfo.io | sed '1s/.*/ipinfo.io:/;$d;'
- }
- ifps() {
- oneline() {
- sed -n -E '
- s/.* //;
- G;
- /^([^\n]+).*\n\1(\n|$)/!{
- s/^([^\n]+)\n(.*)/\2\n\1/;
- h;
- };
- ${
- g;
- s/([^\n])(\n)([^\n])/\1,\3/g;
- s/^\n/:/;
- p;
- }'
- }
- sp='[\t ]*($|\n)' ; W='[\t ]+' ; w='[^\t ]+'
- for ip in $* `[ -z "$*" ] && \
- ss -tupla40H |\
- sed -n -E "
- s/%$w//;
- s/^${w}${W}${w}${W}${w}${W}${w}${W}($w):.*/\1/;
- tb;
- d;
- :b;
- /^[1-9]/{
- G;
- /^([^\n]+).*\n(\1)($|\n)/bo;
- s/\n.*//;
- P;
- H;
- :o;
- };"`
- do sudo echo -n $ip
- sudo ss -tupla40H |\
- sed -n -E "
- /${ip}:/{
- s/%$w//;
- /pid=/b pid;
- s/^($w)$W$w$W$w$W$w$W($w).*$/\2 \1/;
- b out;
- :pid;
- s/^($w)$W$w$W$w$W$w$W($w)$W$w[^\"]+.([^\"]+).[^=]+.([0-9]+).*${sp}/\2 \1 \3\/\4\5/;
- :out;
- /^0/d;
- s/^([^:]+).([^ ]+).[^ ]+.(.*)/\1 \2=\3/p;
- }" | oneline
- done
- }
- # ifstate
- # UP enx001c2314614d=00:1c:23:14:61:4d,192.168.0.1
- # DOWN wlx001cbfb4f948=00:1c:bf:b4:f9:48
- # UP enx0c5b8f279a64=0c:5b:8f:27:9a:64,192.168.8.100
- # ifstate -v
- # DOWN() wlp12s0=00:1c:bf:b4:f9:48
- # UNKNOWN(UP,LOWER_UP) enp0s29f7u4=0c:5b:8f:27:9a:64,192.168.8.100
- # UP(UP,LOWER_UP) enx001c2314614d=00:1c:23:14:61:4d
- # ifstate -v enx001c2314614d
- # UP enx001c2314614d=00:1c:23:14:61:4d,192.168.0.1
- # ifstate -v UP
- # UP enx001c2314614d=00:1c:23:14:61:4d,192.168.0.1
- # UP enx0c5b8f279a64=0c:5b:8f:27:9a:64,192.168.8.100
- #
- # ifstr
- # enx001c2314614d 00:1c:23:14:61:4d 192.168.0.1
- # wlx001cbfb4f948 00:1c:bf:b4:f9:48
- # enx0c5b8f279a64 0c:5b:8f:27:9a:64 192.168.8.100
- # ifstr -m enx001c2314614d
- # 00:1c:23:14:61:4d
- # ifstr -i 192.168.8.100
- # enx0c5b8f279a64
- # ifstr -a enx001c2314614d
- # 192.168.0.1
- # ifstr 00:1c:23:14:61:4d
- # enx001c2314614d 00:1c:23:14:61:4d 192.168.0.1
- #
- # rt_tables, making additional routing tables for two or more interfaces
- if [ -n "`which nmcli`" ]
- then ifinfo() {
- local l='' u=''
- for c in {a..z}
- do l=${l}$c
- done
- for c in {A..Z}
- do u=${u}$c
- done
- nmcli -f GENERAL.DEVICE,GENERAL.HWADDR,IP4.ADDRESS device show |\
- sed -n \
- -e '/GEN.*DEVICE/{
- s/.*:[\t ]\+//;
- h;
- }' \
- -e '/GEN.*HWADDR/{
- s/.*:[\t ]\+//;
- H;
- }' \
- -e '/IP4.*ADDRESS/{
- s/\(.*:[\t ]\+\)\([^/]\+\)\(.*$\)/\2/;
- H;
- }' \
- -e "/^$/{
- g;
- s/\n/=/;
- s/\n/,/;
- y/$u/$l/;
- s/^.*/DOWN &/;
- /,/s/^[^ ]\+ /UP /;
- p;
- }" |\
- sort
- }
- ifsi() {
- local l='' u=''
- for c in {a..z}
- do l=${l}$c
- done
- for c in {A..Z}
- do u=${u}$c
- done
- nmcli -f GENERAL.DEVICE,GENERAL.HWADDR,IP4.ADDRESS device show |\
- sed -n \
- -e '/GEN.*DEVICE/{
- s/.*:[\t ]\+//;
- h;
- }' \
- -e '/GEN.*HWADDR/{
- s/.*:[\t ]\+//;
- H;
- }' \
- -e '/IP4.*ADDRESS/{
- s/\(.*:[\t ]\+\)\([^/]\+\)\(.*$\)/\2/;
- H;
- }' \
- -e "/^$/{
- g;
- s/\n/=/;
- s/\n/,/;
- y/$u/$l/;
- p;
- }" |\
- sort
- }
- else
- ifinfo() { (
- ip -o -4 address |\
- sed -n -e 's/\(^[0-9]\+:[\t ]\+\)\([ew][^\t ]\+\)\([^0-9]\+\)\([^\/]\+\)\(.*$\)/\2=,\4/p' ;
- ip -o -4 link |\
- sed -n \
- -e 's/\(^[0-9]:[\t ]\+\)\([ew][^:]\+\)\(:[\t ]\+.*CAST,*\)\([^>]*\)\(.*state[\t ]\+\)\([DU][NOP][KNOW]*\)\(.*ether[\t ]\+\)\([^\t ]\+\)\(.*$\)/\2=\8 \6(\4)/p' ) | \
- sort | \
- sed -n -e '/^[ew]/{
- s/^.*=,/,/;
- H;
- }' \
- -e '${
- G;
- s/\n,/,/g;
- s/\([ew][0-9a-z]\+=[:0-9a-z]\+\)\([\t ]\+\)\([DU][^\x28]\+\)\(\x28[^\x29]\+\x29\)/\3 \1/g;
- s/\n\n/\n/g;
- p;
- }' | sort | uniq
- }
- ifsi() { (
- ip -o -4 address |\
- sed -n -e 's/\(^[0-9]\+:[\t ]\+\)\([ew][^\t ]\+\)\([^0-9]\+\)\([^\/]\+\)\(.*$\)/\2=,\4/p' ;
- ip -o -4 link |\
- sed -n \
- -e 's/\(^[0-9]:[\t ]\+\)\([ew][^:]\+\)\(:[\t ]\+.*CAST,*\)\([^>]*\)\(.*state[\t ]\+\)\([DU][NOP][KNOW]*\)\(.*ether[\t ]\+\)\([^\t ]\+\)\(.*$\)/\2=\8 \6(\4)/p' ) | \
- sort | \
- sed -n -e '/^[ew]/{
- s/^.*=,/,/;
- H;
- }' \
- -e '${
- G;
- s/\n,/,/g;
- s/\([ew][0-9a-z]\+=[:0-9a-z]\+\)\([\t ]\+\)\([DU][^\x28]\+\)\(\x28[^\x29]\+\x29\)/\1/g;
- s/\n\n/\n/g;
- p;
- }' | sort | uniq
- }
- fi
- ifstate() {
- local iface_or_mac_or_ip_or_updown=''
- [ "$1" = "-v" ] && \
- iface_or_mac_or_ip_or_updown="$2" || \
- iface_or_mac_or_ip_or_updown="$1" # empty is OK
- if [ "$1" = '-v' ] && [ -n "$iface_or_mac_or_ip_or_updown" ]
- then ifinfo | sed -n -e "/$iface_or_mac_or_ip_or_updown/p"
- elif [ "$1" = '-v' ]
- then ifinfo
- elif [ -n "$iface_or_mac_or_ip_or_updown" ]
- then ifinfo | sed -n -e "/$iface_or_mac_or_ip_or_updown/{s/[(].*[)]//;p;}"
- else
- ifinfo
- fi
- }
- ifstr() {
- local a=( `ifsi` ) s=''
- if [ "$1" = "-a" ] && [ -n "$2" ]
- then for s in ${a[@]}
- do [[ "$s" =~ $2 ]] && [[ "$s" =~ ',' ]] && echo ${s/*,/}
- done
- elif [ "$1" = "-m" ] && [ -n "$2" ]
- then for s in ${a[@]}
- do [[ "$s" =~ $2 ]] && s=${s/*=/} && echo ${s/,*/}
- done
- elif [ "$1" = "-i" ] && [ -n "$2" ]
- then for s in ${a[@]}
- do [[ "$s" =~ $2 ]] && echo ${s/=*/}
- done
- elif [ -n "$1" ]
- then for s in ${a[@]}
- do [[ "$s" =~ $1 ]] && echo $s | tr '=,' ' '
- done
- else
- ifsi | tr '=,' ' '
- fi
- }
- # https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System
- rt_tables() { # take interface, ip, gateway, and derive network, assume mask
- local a=("$@")
- set ${!a[@]}
- rm /tmp/rt_tables 2>/dev/null
- 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`
- #echo $r
- sed -n \
- -e '/^[1-9][\t ]*rt[2-9]/d' \
- -e '1,/#1[\t ]inr.ruhep/{H;}' \
- -e "\${g;s/^.*\$/&\n${r}/p;q;}" /etc/iproute2/rt_tables | \
- sed -e '/^$/d' > /tmp/rt_tables
- [ -s /tmp/rt_tables ] && sudo cp /tmp/rt_tables /etc/iproute2/rt_tables
- # or in a file ending in .conf, in /etc/iproute2/rt_tables.d
- # set ${!a[@]}
- # rm /tmp/interfaces 2>/dev/null
- # local r=`
- #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
- #`
- }
- #rt_tables a b c
- #return
- #exit 0
- # commandline--
- #ip rule add from 192.168.0.1/32 table rt2
- #ip rule add to 192.168.0.1/32 table rt2
- #ip route add 192.168.0.0/24 dev enx001c2314614d src 192.168.0.1 table rt2
- # may get in trouble next line, if gw is not one hop off:
- #ip route add default via 192.168.0.1 dev enx001c2314614d table rt2
- #ip route list table rt2
- #ip rule show
- #
- ## interfaces(5) file used by ifup(8) and ifdown(8)
- #auto lo
- #iface lo inet loopback
- #
- #auto enx001c2314614d
- #iface enx001c2314614d inet static
- #address 192.168.0.1
- #netmask 255.255.255.0
- #gateway 192.168.0.1
- #dns-nameservers 192.168.0.1
- #up sleep 3 ; ip address replace 192.168.0.1 dev enx001c2314614d || true
- #up sleep 3 ; ip route add 192.168.0.0/24 dev enx001c2314614d src 192.168.0.1 table rt2 || true
- #up sleep 3 ; ip route add default via 192.168.0.1 dev enx001c2314614d table rt2 || true
- # leaving unsaid much usually said about a gw--one-hop off for others--if not 1 hop off from us
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement