Newline in my script-built shell variable

Hi all,

I'm not exactly a shell script guru, so this is probably a very silly question and I'm not seeing the point, but you know, sometimes it happens...

I have this script which adds entries to local arp cache using it to find the corresponding IP address.

# export MAC=00:25:90:34:3d:f2
# export IP=$(fping -c1 -g -q `ifconfig eth0 | grep "inet addr" | awk -F : {'print $2'} | awk {'print $1'} | awk -F . {'print $1"."$2"."$3".0/24"'}` &>/dev/null; echo  && arp -n | grep $MAC | awk {'print $1'})

The problem is that in every macro I use from now on, the variable $IP inserts a newline before being printed or used.

# echo "IP is $IP"
> IP is
> 192.168.26.59

I cannot use this variable inside a more complex command because of this newline.

Why is this happening and how to solve it?

I thank you all in advance.

Bye,
M.

Slide the trailing quote left of the variable, and the lf is just white space, poof.

# echo "IP is "$IP

After a brief glance at your code, I'd say the echo is to blame. If you don't need it, remove it. Or, you can do as DGPickett suggests and not quote the $IP.

Regards,
Alister

Ok, I'm so sorry to have opened a thread for such a stupid question... I was totally sure to have tried every possible combination. :wall:

Ok, it was the last working day in the week and the last working hour in the day, but... :smiley:

Thanks both of you!

M.

I like my method of finding my local IP address better:

DEVICE="$(netstat -nr | sed -n '/^0.0/s/.* //p')"
test -z "$DEVICE"  &&  { echo "There is no default route"; exit 1; }
IP=$(/sbin/ifconfig $DEVICE 2>/dev/null | sed -n '/inet /s/.*addr:\([0-9.]*\).*/\1/p')

But if you want your external IP address or URL, there's a much more simple way:

IP=$(curl -s queryip.net/ip/)
URL=$(curl -s queryip.net/url/)

Hi,

I actually have to find the IP address of another server on the same LAN given its MAC address.
Thanks for sharing your ideas and code!

M.

Just for the fun of it :slight_smile:

echo "Your ip is:" $(/sbin/arp -a | grep 00:00:44:00:01:44 | awk -F'[(|)]' '{print $2}')

And still for fun: :wink:

you are assuming that the IP address is already in the arp cache. Since it may be not, I have to build my arp cache with the fping command!

M.

I like:

$ netstat -rn | sed '
> /^127/d
> s/ .* UH .*//
> t
> d
> '
171.186.182.236
$ 

I did not get this:
where are you asking for the association between an a-priori known MAC address and the IP address assigned from the DHCP?

The scenario:

I'm on machine A with all networking data up and running.
I know that on my subnet there's a machine B whose MAC address is given.
As soon as B is booted-up it takes an IP from the DHCP server on the network and I want to know it.

I'd really be interested in more subtle and keen ways of doing this wrt what I posted in the thread opening.

Thanks,
M.

Well, the arp cache is a great way to see who is out there, so if other methods fail, just ping the broadcast so all hosts echo back and it fills the cache. You may get bridge MACs for more remote hosts.

I don't get any UH's when I do netstat -rn.
I get two U's and one UG.

But even if I replace UH, I end up with a network, not an IP.
What's that IP address you showed? Is it your host's external IP address? How?

If DHCP or configuration has not given your host an IP, or you do not have PATH MTU, the UH will be missing. Even my windows PC shows its IP on netstat -rn, because the NIC is gateway to the collision domain hosts. You can get it from a U record, too:

$ netstat -rn
Routing tables
Dest/Netmask                  Gateway            Flags    Use Interface  Pmtu
127.0.0.1             127.0.0.1          UH       0   88959488  lo0        4136
171.186.182.236       171.186.182.236    UH       0   35483654  lan7       4136
171.186.182.0         171.186.182.236    U        2          0  lan7       1500
127.0.0.0             127.0.0.1          U        0          0  lo0        4136
default               171.186.182.1      UG       0          0  lan7       1500
$ netstat -rn | sed -n '
  /^127/d
  s/^[1-9][0-9.]*  *\([1-9][0-9.]*\)  *U .*/\1/p
 '
171.186.182.236
$

Your solution is not very robust. netstat -nr always give me something like this on Linux:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 br0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 br0
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 br0

Well, that looks like you have no DHCP assigned IP yet. No loopback, either! Weird!

I never have DHCP assigned IPs. My ISP uses DHCP to assign my router a real IP, but inside the premises I am lord almighty of the network.

You're right about loopback though. I didn't notice that before. I'll have to check into that.

---------- Post updated at 07:26 PM ---------- Previous update was at 06:57 PM ----------

I think I remember loop addresses previously being included in the output of netstat -nr, but I just checked, and it's not included in any of the 4 Linux boxes I have running right now (2 Fedora, 1 Ubuntu and 1 Arch). Yet I can ping and ssh to localhost on each.

I must not have been paying attention when it became implicit on Linux.

Windows has routing to push all local IPs to loopback, so I guess this is a step in the other direction. The IP stack can deal with loopback and hide it from routing. On Solaris and HP-UX I always see it. Well, not the most portable thing. Even ping output varies a lot. It's a shame they cannot get their act together on one set of network commands and facilities, consistent presentations! I was not amused when they overloaded the routing table with path MTU. At least HP seems to does it with less clutter than I recall on Solaris.