Parameters passed to commands but not working in Bash shell

Hi, I am trying to do this thing useing my shell bash ( sorry for my english )

I have in a file 63 hostnames, i wanna ask to the DHCP admin, to reserv that reserves 63 IP addresses of this hosts, using their mac address.

I have thinked this script:

for ((i=1;i<63;i++)); do
	arp $(head -n $i host.txt |tail -n 1) | awk '/Cyp/ {$1 $3}' > host_mac.txt
	
done;

This are my trys:

Cypuysrv456:~# head -n 1 host.txt |tail -n 1
CypUYNB106


Cypuysrv456:~# ping  $(head -n 1 host.txt |tail -n 1)
ping: unknown host CypUYNB106


Cypuysrv456:~# ping CypUYNB106
PING CypUYNB106.hh.Cyp.com (172.Z.X.Y) 56(84) bytes of data.
64 bytes from Cypuynb106.hh.Cyp.com (172.Z.X.Y): icmp_seq=1 ttl=128 time=0.333 ms
64 bytes from Cypuynb074.hh.Cyp.com (172.Z.X.Y): icmp_seq=2 ttl=128 time=0.276 ms
^C
--- CypUYNB106.hh.Cyp.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.276/0.304/0.333/0.033 ms


Cypuysrv456:~#

I'll explain:

Cypuysrv456:~# head -n 1 host.txt |tail -n 1
CypUYNB106

I get the names of the file and the system returns one by one the hostnames

Cypuysrv456:~# ping  $(head -n 1 host.txt |tail -n 1)
ping: unknown host CypUYNB106

Dam! it does not recognize the host, maybe the system it can not resolve the hostname.

Cypuysrv456:~# ping CypUYNB106
PING CypUYNB106.hh.Cyp.com (172.Z.X.Y) 56(84) bytes of data.
64 bytes from Cypuynb106.hh.Cyp.com (172.Z.X.Y): icmp_seq=1 ttl=128 time=0.333 ms
64 bytes from Cypuynb074.hh.Cyp.com (172.Z.X.Y): icmp_seq=2 ttl=128 time=0.276 ms
^C
--- CypUYNB106.hh.Cyp.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.276/0.304/0.333/0.033 ms

:eek: he could resolve it. :S

� Why not before ?

I change in the file, the hostname with the completely hostname, but no fortune.

CypUYNB106.hh.Cyp.com

Since some days ago i have joined this machine to the domain ( active directory ) this is a gnu/linux server.

� Any idea why the system coud not resolve the name ?

# cat /etc/resolv.conf
domain hh.Cyp.com
search hh.Cyp.com
nameserver 172.Z.X.F
nameserver 172.Z.W.R

Regards,

what is the o/p of :-

cat hosts.txt.

hosts.txt it's the file where there is the 63 hostnames

# cat host.txt

CypUYNB106
CypUYNB144
CypUYNB150
CypUYNB151
CypUYNB152
CypUYWT1124
CypUYWT1126
CypUYWT1133
CypUYWT1135
CypUYWT1142
CypUYWT1152

And more hostnames

Try one of the below:-

for ((i=1;i<63;i++)) ; do
cat <(head -n $i host.txt | tail -n 1) | xargs -n 1 -x arp
done
for ((i=1;i<63;i++)) ; do
echo $(head -n $i host.txt  | tail -n 1) | xargs -n 1 -x arp 
done

:D:D:D

Mmm.. not working,

It can not resolve the hostname:

xargs: arp: exited with status 255; aborting
: Unknown host

I put ping in sted of arp, anyway after a ping i can consult arp -a
and no lucky

Cypuysrv456:~# for ((i=1;i<63;i++)) ; do echo $(head -n $i host.txt  | tail -n 1) | xargs  ping -c 1 ; done
ping: unknown host CypUYWT857
Cypuysrv456:~# ping CypUYWT857
PING CypUYWT857.hh.Cyp.com (172.X.Y.Z) 56(84) bytes of data.
64 bytes from Cypuywt857.hh.Cyp.com (172.X.Y.Z): icmp_seq=1 ttl=128 time=2.31 ms
64 bytes from Cypuywt857.hh.Cyp.com (172.X.Y.Z): icmp_seq=2 ttl=128 time=0.211 ms
^C
--- CypUYWT857.hh.Cyp.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1005ms
rtt min/avg/max/mdev = 0.211/1.264/2.317/1.053 ms
Cypuysrv456:~#

:S
dazed and confused :frowning:

this is not the code I wrote to you

for ((i=1;i<63;i++)) ; do echo $(head -n $i host.txt  | tail -n 1) | xargs  ping -c 1 ; done

what is your OS?

I know, the code you wrote, gave me

xargs: arp: exited with status 255; aborting
: Unknown host

Then i change it, just to know if were arp's problem. but the problem it's at the time it is queried.. ( if it's queried :S ) the DNS maybe.. when it's executed the command..

My OS is:

Debian Lenny

Cypuysrv456:~# uname -a
Linux Cypuysrv456 2.6.26-1-686 #1 SMP Fri Mar 13 18:08:45 UTC 2009 i686 GNU/Linux

no problem with my code I have tested it with successful o/p.

it seems that the hosts or some of them in file host.txt can't be resolved;

xargs: arp: exited with status 255; aborting
: Unknown host

did you try to put the IP address instead of the name of the hosts?

---------- Post updated at 18:46 ---------- Previous update was at 18:44 ----------

kindly see my o/p when I try the code:-

bash-3.00$ for ((i=1;i<10;i++)) ; do echo $(head -n $i /etc/hosts  | tail -1| nawk '{print $2}') | xargs -n 1 arp; done

o/p:-

arp: Internet: unknown host # host is not exist in my LAN
localhost (127.0.0.1) -- no entry
server2 (192.168.7.216) at 0:21:28:2:b0:54 permanent published
omc_splat2 (192.168.7.65) at 0:14:4f:1e:e7:7f
server9 (192.168.7.62) -- no entry
server7 (192.168.7.54) -- no entry
server4 (192.168.7.58) -- no entry

Finally i get something, but i still do not understand why sometime works, and why sometimes not..

i still can't get some mac address.. anyway i get some others..

I think that the file it self /root/hosts.txt was the problem.. because i wrote another host1.txt and works.

THANKS ahmad.diab!!! :smiley: :smiley:
Thank you very much!!
:slight_smile:
Kindly Regards,

strange to me too ..it is not from the code if you do the arp  (e.g arp server4) 
command done on the xterm sometimes you get output some times not!!!! 
it seems this is an issue related to the network itself

:):):slight_smile:

Try:

dos2unix host.txt

It may be you have carriage return characters in your file because it came from a dos/windows platform.