Doing a class with writing a bash scrip for diagnostics.
the bit i'm completely stuck on is how to i only capture a specific line and segment of text in that line.
IE: if i do an ifconfig
[user@localhost ~]$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.88.140 netmask 255.255.255.0 broadcast 192.168.88.255
inet6 fe80::fbaa:d42c:24db:dca7 prefixlen 64 scopeid 0x20
ether 00:0c:29:bc:c1:4d txqueuelen 1000 (Ethernet)
I would like bash to capture line 2 column 12 to 27 for ipv4 address into a $string, and continue to do the same for netmask, broadcast, mac address and ipv6 address/cidr...
also do the same to get out any nameservers in the resolve.conf file.
I then of course have to repeat this for each interface...
Unless someone can reccomend a better way of doing this.
I also am wondering if there is a way i can insert a $string to the ifcfg-ens33 file to change the IP address (and everything else) or am i best off completely recreating the file?
i think with this information i should be able to do the rest of the prac quite easily
EDIT: I can see i need to do something with fields, but i can work out how to make it only do field 2 of line 2 into $ipv41
EDIT2: i have come up with this after more reading. but i cant get the output to go into the variables so far. tried echo, printf, >, >>, >>>
#import settings from ip command
#discover interface type into string
/sbin/ip -o -4 addr show up primary scope global| awk '{print $2}' | echo >
$if1
#discover interface ipaddress by interface type into strings
/sbin/ip -o -4 addr show $if1 | awk '{print $4}' | echo > $ipv4
/sbin/ip -o -4 addr show $if1 | awk '{print $6}' | echo > $bast
/sbin/ip -o -4 addr show $if1 | awk '{print $9}' | echo > $dhcp
/sbin/ip -o -6 addr show $if1 | awk '{print $4}' | echo > $ipv6
#print discovered info to screen
echo -e "---- Interface = $if1"
echo -e "---- DHCP Enabled? $dhcp"
echo -e "---- IPv4 Address = $ipv4"
echo -e "---- Broadcast = $bast"
ip
is recommended overifconfig
these days, and it can produce much more easily parseable output.... | echo > $if1
, what you want isif1=$(...)
, for exampleif1=$(/sbin/ip -o -4 addr show up primary scope global | awk '{print $2}')
. You might want to read on shell command substitution. I hope this helps!