Advertisement
Rnery

Getting IP adress nic

Oct 2nd, 2023
1,328
1
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.70 KB | Source Code | 1 0
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3.  
  4. _ip_notfound() {
  5.     clear
  6.     sleep 1
  7.     echo "Erro, IP não encontrado."
  8.     exit 1
  9. }
  10.  
  11. _show_dev() {
  12.     ip address show dev eth0
  13. }
  14.  
  15. _address_condition() {
  16.     _show_dev > /dev/null 2>&1
  17. }
  18.  
  19. _ipv4() {
  20.     if _address_condition; then
  21.         _show_dev |
  22.         grep -E '\w{3}[.]' |
  23.         awk '{print $2}'
  24.     else
  25.         _ip_notfound
  26.     fi
  27. }
  28.  
  29. _ipv6() {
  30.     if _address_condition; then
  31.         _show_dev |
  32.         grep -E 'inet6\s[0-9a-fA-F]{4}[:]' |
  33.         awk '{print $2}'
  34.     else
  35.         _ip_notfound
  36.     fi
  37. }
  38.  
  39. case $1 in
  40.     4)  _ipv4;;
  41.     6)  _ipv6;;
  42.     *)  clear; echo "Não existe essa versão de IP, digite 4 ou 6"
  43. esac
  44.  
Tags: shellscript
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement