LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-26-2018, 06:47 AM   #1
deepak_message
Member
 
Registered: Oct 2007
Posts: 175

Rep: Reputation: 17
Problem in Bash Script for getting interface, ip and mac address


Hi,

I have prepared script to find out network device, IP and Mac address of the remote machine.
I am week in scripting. so please help me to do it.
the sscript working for stand alone machine. but not working for remote machines. please help.

*****************************************************
#!/bin/sh

for hosts in cat $(cat hosts.txt)
do
ssh $hosts -q -t 'for i in $(cat /usr/sbin/ip addr show |grep BROADCAST | cut -f 2 -d ":"`device.txt) do IP=$(/usr/sbin/ip addr show $i | awk '/inet / {print $2}' | cut -d/ -f 1) MAC=$(/usr/sbin/ip link show $i | awk '/ether/ {print $2}') echo -e "$i$IP$MAC" >>output.txt done'

done
# EndOfFile
 
Old 02-26-2018, 07:22 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
please use [code]here comes your script[/code] to keep formatting if your code.
As far as I see this script cannot work at all, so would be better to give us more details: what is the input/output when it works and when it failed.
What is the expected result? Please give us real results/responses to understand it better.
 
1 members found this post helpful.
Old 02-26-2018, 07:29 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by deepak_message View Post
Hi,
I have prepared script to find out network device, IP and Mac address of the remote machine. I am week in scripting. so please help me to do it. the sscript working for stand alone machine. but not working for remote machines. please help.
Code:
#!/bin/sh

for hosts in cat $(cat hosts.txt)
do
        ssh $hosts -q -t 'for i in $(cat /usr/sbin/ip addr show |grep BROADCAST | cut -f 2 -d ":"`device.txt) do IP=$(/usr/sbin/ip addr show $i | awk '/inet / {print $2}' | cut -d/ -f 1) MAC=$(/usr/sbin/ip link show $i | awk '/ether/ {print $2}') echo -e  "$i$IP$MAC" >>output.txt done'

done
# EndOfFile
You have been a member here, and have been asking about scripts for ELEVEN YEARS now, so I find it VERY hard to believe you're 'weak in scripting'. You also need to provide details when asking a question. You say only "not working for remote machines", but don't bother telling us what the error(s)/message(s) you're seeing are, what the format of that hosts.txt file is, where that "device.txt" file is coming from and what's in it, what version/distro of Linux you're using, or what your goal is.

If all you're doing is running a command on a remote machine, start by reading the man page for ssh.
 
Old 02-26-2018, 07:46 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
LINUX does not have txt files for the most part. absolute path will help getting the right file and issuing the right program depending on what you are doing.

I do not know why you're cat'ing a program binary.
Quote:
Originally Posted by deepak_message View Post

$(cat /usr/sbin/ip addr show |grep BROADCAST ...
Code:
$ ip addr | grep BROADCAST | cut -f 2   
2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
that is what it gets you, without cat'ing ip . test your command on the command line first before you add them into your script to make sure they work first, when you get to where you can write a script wit that 'this should work' then you can just put that tactic in your back pocket for when you need it.

this is what cat'ing get you when applies to that line of code
Code:
$  cat ip addr | grep BROADCAST | cut -f 2 
cat: ip: No such file or directory
cat: addr: No such file or directory
but you do not always know that using a script, sometimes them messages get hidden.

Using the command line you can work out your code:
Code:
step 1:
userx@slackwhere101:~
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:24:d7:cf:9e:a4 brd ff:ff:ff:ff:ff:ff
    inet 135.31.93.129/23 brd 135.31.93.255 scope global dynamic wlan0
       valid_lft 2340sec preferred_lft 2340sec
    inet6 fe80::224:d7ff:fecf:9ea4/64 scope link 
       valid_lft forever preferred_lft forever

//gets me that up top then with that information I know what I am looking at, so, 
//now I have a better idea on how to now proceed 
 
step 2:
$ ip addr | grep inet                         
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
    inet 135.31.93.129/23 brd 135.31.93.255 scope global dynamic wlan0
    inet6 fe80::224:d7ff:fecf:9ea4/64 scope link 

step 3:
userx@slackwhere101:~
$ ip addr | grep inet | awk 'FNR == 3 {print}'
   inet 135.31.93.129/23 brd 135.31.93.255 scope global dynamic wlan0
1. Then strip it down from there if you're looking for just ip address numbers.
2. Then use the same methodology for getting your MAC Address.
3. Then put it all together in a BASH Script.

Simple as acb, 123

Code:
$ ip     
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
       ip [ -force ] -batch filename
where  OBJECT := { link | address | addrlabel | route | rule | neighbor | ntable |
                   tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
                   netns | l2tp | fou | tcp_metrics | token | netconf }
       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
                    -h[uman-readable] | -iec |
                    -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |
                    -4 | -6 | -I | -D | -B | -0 |
                    -l[oops] { maximum-addr-flush-attempts } | -br[ief] |
                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
                    -rc[vbuf] [size] | -n[etns] name | -a[ll] |*-c[olor]}
Code:
$ ip address | awk 'FNR == 9 {print}'
   inet 135.31.93.129/23 brd 135.31.93.255 scope global dynamic wlan0
works too.

Last edited by BW-userx; 02-26-2018 at 08:13 AM.
 
Old 02-26-2018, 08:08 AM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Isolating the information can be done using awk.
Code:
/sbin/ip a | awk '/BROADCAST/ {d=$2}; /ether/ {m=$2}; /global/ {print d,m,$2}'
For "Run a Remote Process and Capture Output Locally' see the OpenSSH Cookbook
 
1 members found this post helpful.
Old 02-26-2018, 08:28 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by allend View Post
Isolating the information can be done using awk.
Code:
/sbin/ip a | awk '/BROADCAST/ {d=$2}; /ether/ {m=$2}; /global/ {print d,m,$2}'
For "Run a Remote Process and Capture Output Locally' see the OpenSSH Cookbook
cool, but how does that last pram work itself out? Using the 'catch' word for that line. trying a few of them,
Code:
userx@slackwhere101:~
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:11:d7:ff:9e:a4 brd ff:ff:ff:ff:ff:ff
    inet 111.31.59.123/23 brd 123.31.12.255 scope global dynamic wlan0
       valid_lft 2016sec preferred_lft 2016sec
    inet6 fe80::224:d7ff:fecf:9ea4/64 scope link 
       valid_lft forever preferred_lft forever

userx@slackwhere101:~
$ ip a | awk '/BROADCAST/ {d=$2} /link/ {m=$2}; /global/ {print d,m,$2}'
wlan0: 00:24:d7:cf:9e:a4 122.31.99.123/23
userx@slackwhere101:~
$ ip a | awk '/BROADCAST/ {d=$2} /link/ {m=$2}; /scope/ {print d,m,$2}'
 00:00:00:00:00:00 127.0.0.1/8
 00:00:00:00:00:00 ::1/128
wlan0: 00:26:d7:cc:9e:a4 456.41.70.123/23
wlan0: fe80::234:d7ff:fecf:9ea4/64 fe80::224:d7ff:fecf:9ea4/64
userx@slackwhere101:~
$ ip a | awk '/BROADCAST/ {d=$2} /link/ {m=$2}; /dynamic/ {print d,m,$2}'
wlan0: 00:33:d7:aa:9e:a4 321.81.00.123/23
where using scope returned more information, so them words cannot be just being used as a pattern, to find then print the second 'pattern' $2 on that line, yes/no?

Just being curious.

Last edited by BW-userx; 02-26-2018 at 08:41 AM.
 
Old 02-26-2018, 08:47 AM   #7
deepak_message
Member
 
Registered: Oct 2007
Posts: 175

Original Poster
Rep: Reputation: 17
Hi allend,

Thanks for your reply. but the problem is , only shows interface name, where IP is configured.
I need all interface name and their mac address whether IP is configured on not.

/sbin/ip a | awk '/BROADCAST/ {d=$2}; /ether/ {m=$2}; /global/ {print d,m,$2}'
 
Old 02-26-2018, 09:09 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
if their is a NIC then you're going to get a MAC if no IP then MAC is still there, Find MAC then check for IP if no IP then not configured, report same to file. basic logic ??
 
Old 02-26-2018, 09:25 AM   #9
deepak_message
Member
 
Registered: Oct 2007
Posts: 175

Original Poster
Rep: Reputation: 17
But, I have required interface name with mac address whether IP is configured or not of remote machines.
 
Old 02-26-2018, 09:35 AM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by deepak_message View Post
But, I have required interface name with mac address whether IP is configured or not of remote machines.
No, you have not. You are only looking at the line containing the string "BROADCAST", which doesn't contain either the IP address or the MAC address. [I think, but you never have put your code in code tags, so...]
BW-userx has explained how to do it three times so far. (Such patience).

Last edited by scasey; 02-26-2018 at 09:37 AM.
 
Old 02-26-2018, 09:35 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by deepak_message View Post
But, I have required interface name with mac address whether IP is configured or not of remote machines.
well as I do not have anything with a not configured, I have no idea what that looks like, though I would suspect that no IP numbers would be present for a MAC that is not configured, therefore Logic states look for something with a MAC and no IP numbers, results in not configured.

others that deal with IP stuff might know a easier method.
 
Old 02-26-2018, 12:45 PM   #12
deepak_message
Member
 
Registered: Oct 2007
Posts: 175

Original Poster
Rep: Reputation: 17
Thanks all for suggestion and support.

Finally, I have prepared script. below is the script.....................

#!/bin/sh

#####have to create separate file hosts.txt, for keeping remote machine IP or hostname#####
for hosts in `cat hosts.txt`;
do
echo "Hostname ----$hosts-----------"
ssh $hosts -q -t /sbin/ip addr show |grep BROADCAST | cut -f 2 -d ":">device.txt
#########Above line for collect interface name, it will store in device.txt############
for i in `cat device.txt`;
do
IP=$(/usr/sbin/ip addr show $i | awk '/inet / {print $2}' | cut -d/ -f 1)
MAC=$(/usr/sbin/ip link show $i | awk '/ether/ {print $2}')
tabs 15
echo -e "$i\t$IP\t$MAC"
done
done
# EndOfFile

Output is like this..........

Hostname ----rhel7.server.com-----------


ens33 192.168.5.131 00:0c:29:47:12:25

ens38 00:0c:29:47:12:2f
 
Old 02-26-2018, 01:03 PM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
something like this was already suggested:
Code:
for hosts in `cat hosts.txt`;
do
  echo "Hostname ----$hosts-----------"
  ssh $hosts -q -t /sbin/ip a | awk '/BROADCAST/ {d=$2}; /ether/ {m=$2}; /global/ {print d,m,$2}'
done
you need to take care about [remote] commands: which one will be executed on which host? Which device does really exist on which host?

Last edited by pan64; 02-26-2018 at 01:06 PM.
 
Old 02-26-2018, 04:50 PM   #14
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Quote:
But, I have required interface name with mac address whether IP is configured or not of remote machines.
You are assuming the IP is available by using 'ssh $hosts'. If the host is unavailable, the ssh connection will fail.

PS - @BW-userx The awk relies on the awk behaviour of reading line by line. The line containing "BROADCAST" gives the device in field 2. The line containing "ether" gives the MAC in field 2. The line containing "global" gives the IP and mask in field 2. At this point all the requested information is available and is printed.
 
Old 02-26-2018, 05:35 PM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by allend View Post
You are assuming the IP is available by using 'ssh $hosts'. If the host is unavailable, the ssh connection will fail.

PS - @BW-userx The awk relies on the awk behaviour of reading line by line. The line containing "BROADCAST" gives the device in field 2. The line containing "ether" gives the MAC in field 2. The line containing "global" gives the IP and mask in field 2. At this point all the requested information is available and is printed.
yeah I got that part figured out, it was the key words used to get the line on the ip address.
ip a give the entire output file output to read using awk
Code:
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:24:d7:cf:9e:a4 brd ff:ff:ff:ff:ff:ff
    inet 172.31.99.123/23 brd 172.31.99.255 scope global dynamic wlan0
       valid_lft 2585sec preferred_lft 2585sec
    inet6 fe80::224:d7ff:fecf:9ea4/64 scope link 
       valid_lft forever preferred_lft forever
getting the ip using a pattern of scope, or, global, or, dynamic gives different results. I got it figured out, looking closer, scope is said more than once, whereas global is only printed out once in the entire thing.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find MAC address of the 'main' network interface arizonagroovejet Linux - General 4 05-14-2014 01:08 PM
finding mac address of our interface from kernel space karan2386 Linux - Kernel 1 03-31-2012 05:02 AM
env variable mac address script problem schris403 Programming 7 08-01-2007 03:04 PM
get mac address given a specific interface kpachopoulos Programming 6 06-07-2006 02:41 AM
MAC Address spoofing on alias/secondary interface tara Linux - Networking 3 08-31-2005 09:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration