0

I'm trying to filter remote machine mac address , IP and hostname from 100 machines. Initially I'm capturing the information using below command.

$for i in `cat IP`;do in ssh root@$IP "ifconfig eth0 && hostname " ;done

eth0      Link encap:Ethernet  HWaddr 00:MN:77:TR:XX:ZZ
          inet addr:192.168.122.25  Bcast:192.168.122.255  
          Mask:255.255.224.0
test.com-112304
eth0      Link encap:Ethernet  HWaddr 00:TT:77:MM:XX:YY
          inet addr:192.168.122.22  Bcast:192.168.122.255  
          Mask:255.255.224.0
test.com-11035

Initially the output redirect to one file. from their I need to process output which comes in the above format.

I can able to accomplish to print IP and mac by line as follows

$cat input | awk '/HWaddr/{printf $NF;printf " ";getline;print $2;}' | cut -c1-18,24-

00:MN:77:TR:XX:ZZ 192.168.122.25
00:TT:77:MM:XX:YY 192.168.122.22

Actually I want to include hostname along with the output as follows

00:MN:77:TR:XX:ZZ 192.168.122.25  test.com-112304
00:TT:77:MM:XX:YY 192.168.122.22  test.com-11035

test.com will be common name for all hostname. I tried redirect both output in two variable and called as follows , but no luck.

a=`cat input | awk '/HWaddr/{printf $NF;printf " ";getline;print $2;}' | cut -c1-18,24-`
b=`grep test.com input`
echo $a $b  | xargs -n2 

Please shed some views

Lee HoYo
  • 1,217
  • 9
  • 9
user183980
  • 274
  • 3
  • 12
  • 1
    If you could post the output of your command we could then give you more clear solution for your output, as of now it is not clear. – RavinderSingh13 Jun 27 '18 at 14:13
  • 1
    You make things too complicated I think. When `ssh` to the remote server, you can output the `Mac address` of `eth0` and the hostname at one line. – Lee HoYo Jun 27 '18 at 14:24
  • see what pasting your code to https://shellcheck.net shows as needing fixing. I don't see that you use the `i` variable you declare in `for i` among others.j Agree with above comments. Good luck. – shellter Jun 27 '18 at 15:21

1 Answers1

3

I'm trying to filter remote machine mac address , IP and hostname from 100 machines.

  1. Machine mac address, from here: cat /sys/class/net/eth0/address
  2. You have hostname utility, why don't you just hostname -i? You can parse ifconfig | grep inet | awk '{print $2}' | sed 's/addr://' and hope ifconfig versions are the same across all machines (some ifconfig verions return inet <ip> some return inet addr:<ip> like yours).
  3. Hostname with hostname

Putting together:

echo $(cat /sys/class/net/eth0/address) $(ifconfig eth0 | grep inet | awk '{print $2}') $(hostname)

Executing a command for each line in some file is a job for xargs and remember about properly escaping your arguments:

cat IP | xargs -n1 -I{} ssh root@{} 'echo $(cat /sys/class/net/eth0/address) $(ifconfig eth0 | grep inet | awk '\''{print $2}'\'' | sed '\''s/addr://'\'') $(hostname)'

If you really have 100 machines, ansible might interest you.

If you really have that "input" file and you can't change it, you may parse it like this:

sed 's/eth0/#eth0/' input \
| xargs -d'#' -n1 -- bash -c '{ echo "$1" | grep HWaddr | awk "{print \$5}"; echo "$1" | grep inet | awk "{print \$2}" | sed "s/addr://"; echo "$1" | grep "^test.com"; } | tr "\n" " "; echo' --

And this hurts my eyes:

for i in `cat IP`; then
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • this is customized and old kernel and there is no such location /sys , might be hardcoded. The only way to check by ifconfig . let me test this and come back to us – user183980 Jun 28 '18 at 05:41
  • cool it worked like a charm, many thanks :) the last sed one :) its complicated too :p – user183980 Jun 28 '18 at 06:03