Problem in Bash Script for getting interface, ip and mac address
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
$(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.
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?
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.
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 ??
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).
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.