How to extract NFS information from mount on Linux and Solaris? - linux

I need to extract NFS mount information using mount on Linux (RHEL 4/5) and Solaris (Solaris 10) systems. As this is part of an SSH command, the extraction needs to happen in one line. Unfortunately, Linux and Solaris display the mountpoint at different parts of the line:
Linux:
10.0.0.1:/remote/export on /local/mountpoint otherstuff
Solaris:
/local/mountpoint on 10.0.0.1:/remote/export otherstuff
I would like to get the following space separated output
10.0.0.1 /remote/export /local/mountpoint
I managed to do it separately with sed (Solaris 10 sed), but I need one command returing the same output for both.
Linux sed:
sed 's/\([^:]*\):\([^ ]*\)[^\/]*\([^ ]*\) .*/\1 \2 \3/'
Solaris sed:
sed 's/\([^ ]*\) *on *\([^:]*\):\([^ ]*\) .*/\2 \3 \1/'
Solution:
I adapted the accepted answer to also work with DNS names and not only IPs:
awk -F'[: ]' '{if(/^\//)print $3,$4,$1;else print $1,$2,$4}'

awk could help you:
awk -F'[: ]' '{if(/^[0-9]/)print $1,$2,$4;else print $3,$4,$1}'
see this test:
kent$ cat f
10.0.0.1:/remote/export on /local/mountpoint otherstuff
/local/mountpoint on 10.0.0.1:/remote/export otherstuff
kent$ awk -F'[: ]' '{if(/^[0-9]/)print $1,$2,$4;else print $3,$4,$1}' f
10.0.0.1 /remote/export /local/mountpoint
10.0.0.1 /remote/export /local/mountpoint

A some shoter version of Kents solution
awk -F'[: ]' '{print /^[0-9]/?$1" "$2" "$4:$3" "$4" "$1}' file
10.0.0.1 /remote/export /local/mountpoint
10.0.0.1 /remote/export /local/mountpoint

Related

Regex to match IP addresses but ignore localhost

So I have this script that does something with IPs allocated to my OS (GNU/Linux) that I get from running ifconfig. It works fine, however, I was wondering if I could filter out loopback/localhost IP (127.0.0.1) in the same regex expression [I assume every server within my cluster has said IP and I don't need to do anything with it in my script.]
What my script uses is:
ifconfig | awk '/(([0-9]{1,3}\.){3})/ {print}' |sed -e "s/.*addr\://g" -e "s/\s.*//g"
I get results like:
> ifconfig | awk '/(([0-9]{1,3}\.){3})/ {print}' |sed -e "s/.*addr\://g" -e "s/\s.*//g"
172.16.0.1
127.0.0.1
I know it might be a stupid question, but could I filter any IP that starts with 127 in my first regex?
I could try changing awk for grep, somethin like:
> ifconfig |egrep -o "addr\:(([0-9]{1,3}\.){3}[0-9]{1,3})" |sed -e "s/.*addr\://g"
but if I try to negate (?!127) at the beginning, bash will interpret it as !127 which would just throw me something from the history.
I mean, I could just run another grep at the end of the oneliner like grep -v "127.0.0.1", but I just wanted to avoid greping something already greped. Not that anything is wrong with that, just trying to know little more and be more efficient, I guess.
With only one grep without sed or awk:
# ip a|grep -oP "inet \K[0-9.]*(?=.*[^ ][^l][^o]$)"
192.168.1.31
172.16.5.31
You can just add a clause to match the 127.0.0.1 and exclude it by adding the next as below. This way Awk ignores doing any action on the lines containing this pattern.
.. | awk '/127.0.0.1/{next}/(([0-9]{1,3}\.){3})/{print}' | ..

Linux grep only n,m character strings

I have a file which has below contents,
TESTING
TSET24D
DSWEDFBG
WTSETO
MSDWEHLKGY
and want to grep the strings only 6 and 8 characters long.
I tried the below one,
[root#server ~]# cat listfile | grep -o -w -E '^[[:alnum:]]{6,8}'
TESTING
TSET24D
DSWEDFBG
WTSETO
[root#server ~]#
which seems to work on some servers and its returing the strings between 6-8 also coming.
Any idea please..
In your regex, {6,8} looks for a repetition between 6 and 8 times (so 7 is included). You need to use a pipe (OR in regex) to split the search for only 6 OR 8 times.
$ grep -o -w -E '^[[:alnum:]]{6}|^[[:alnum:]]{8}' listfile
DSWEDFBG
WTSETO
Using Awk with its POSIX compliant length() function,
awk '(length($0)==6 || length($0)==8) && $0 ~ /[[:alnum:]]{6}|[[:alnum:]]{8}/' file
DSWEDFBG
WTSETO
works fine for a input file as
TESTING
TSET24D
DSWEDFBG
WTSETO
MSDWEHLKGY
------
--------
------a
#1234$21
(or)
more simply as Ed Morton suggests, just do
awk '/^([[:alnum:]]{6}|[[:alnum:]]{8})$/' file
In awk:
awk '/^......(..)?$/' file
DSWEDFBG
WTSETO
The same for [[:alnum:]]s:
$ awk '/^[[:alnum:]]{6}([[:alnum:]]{2})?$/' file
DSWEDFBG
WTSETO

Awk print lines starting with regex (IP address)

Im trying to read a file for those lines which has IP address on the first column.
my command below does not return any value.
cat test.csv | awk '$1 == "^[[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}]" { print $0 }'
The regex can capture IP address.
Tried the below too,
cat test_1.csv | awk '$1~/^[[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\]/ {print $0}'
test.csv
1.1.1.1 ipaddress gateway
2.2.2.2 ipaddress_2 firewall
www.google.com domain google
You can do it more easily with grep:
grep -P '^\d+(\.\d+){3}\s' test.csv
or
grep -P '^\d{1,3}(\.\d{1,3}){3}\s' test.csv
When you use {1,3} (Interval expressions) in GNU awk, you have to use either --re-interval (or) --posix option to enable it.
Use :
awk --posix '$1 ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/' file
(OR)
awk --re-interval '$1 ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/' file
From man awk:
r{n,m}
One or two numbers inside braces denote an interval expression.
Interval expressions are only available if either --posix or
--re-interval is specified on the command line.

Find network interface by IP address - Linux/Bash

I'm wondering how I can query by IP address using sed, and it will show which interface name that is using it.
For example..
ipconfig -a | grep 10.0.0.10
I would expect it to come back with ETH0
ifconfig | grep -B1 10.0.0.10 | grep -o "^\w*"
You should use this comand :
ifconfig | grep -B1 "inet addr:10.0.0.10" | awk '$1!="inet" && $1!="--" {print $1}'
Hope this help !
ip -br -4 a sh | grep 10.0.0.10 | awk '{print $1}'
If you want sed specific solution you may try this. Its little hard to digest how it works , but finally this combination works.
ifconfig | sed -n '/addr:10.0.0.10/{g;H;p};H;x' | awk '{print $1}'
If you want to take it as an argument via script use "$1" or so instead if 10.0.0.10.
Sed manual for reference : http://www.gnu.org/software/sed/manual/sed.html#tail

Retrieve the name of a Network Interface using an IP Address and AWK Bash

I am trying to use Bash on CentOS 6.4 to retrieve the network interface name attached to an IP address using AWK. I have a bit of command from a Solaris box, but I'm not sure how to convert it to Linux output.
The command looks like this:
ifconfig -a | awk '
$1 ~ /:/ {split($1,nic,":");
lastif=sprintf("%s:%s",nic[1],nic[2]);}
$2 == "'$1'" { print lastif ; exit; }
'
Its part of a script, so it takes commandline argument like monitor.sh x.x.x.x y.y.y.y and it uses the first x.x.x.x to get the interface name, then makes $1 == $2 so then it can ping y.y.y.y later. I'm guessing that in Solaris the ifconfig -a output is different than CentOS. I can get the interface name if the IP and interface are on the same line, but in linux, they're on two different lines. Any ideas.
I don't have CentOS, but in RHEL, IP address is listed as inet address. I believe they should be same.
The following command should give you the interface name which has a IP address.
export iface=$(ifconfig | grep -B1 "inet addr:x.x.x.x" | awk '$1!="inet" && $1!="--" {print $1}')
echo "$iface" # To get the interface name for x.x.x.x ip
And this one should show the IP including localhost :
ifconfig | grep "inet addr:" | sed -e 's/addr:/addr: /g' | awk '{print $3}'
geting ifname for 127.0.0.1 (or any other IP)
ifconfig | awk '/127.0.0.1/ {print $1}' RS="\n\n"
lo
getting ip:
ifconfig | awk -F"[ :]+" '/inet addr:/ {print $4}'
Post the output of ifconfig, and I can help you fine tune for your OS

Resources