1835085 Members
2290 Online
110073 Solutions
New Discussion

scripting question

 
SOLVED
Go to solution
Igor Sovin
Super Advisor

scripting question

Hi!

My scrpit is the following:
while read host;
do
ssh -n ${host}
done < hostlist.txt

hostlist.txt contains:


hostname2 12 Sep 12:00
hostname3 12 Sep 13:00

How to make shell get only hostname from string, so would be able to ssh hostname1 then hostname2 etc.?
5 REPLIES 5
Murat SULUHAN
Honored Contributor
Solution

Re: scripting question

Hi Igor

for i in `cat hostlist.txt | awk '{print $1}'
do
....
......
...
done

Best Regards
Murat Suluhan
Peter Godron
Honored Contributor

Re: scripting question

or if you see man read
match the number of parameters to the number of fields

while read host a b c


which means hostname is read into host, the rest into a b and c, whcih can be ignored.
Tim D Fulford
Honored Contributor

Re: scripting question

cat hostlist.txt | perl -ane '{print "$F[0] "}'

would print the hostname. (you could use awk too if you like)

for host in $(cat hostlist.txt | perl -ane '{print "F[0] "}'
do
ssh -n $host
done

or as you have done it as a read..
while read host;
do
ssh -n ${host}
done < $(cat hostlist.txt | perl -ane '{print "F[0] "}'

Regards

Tim
-
Hemmetter
Esteemed Contributor

Re: scripting question

Hi Igor

try:
while read host restofline;
do
ssh -n ${host}
done < hostlist.txt

This will assign the first "word" to $host and the rest of the line to $restofline.

rgds
HGH
Igor Sovin
Super Advisor

Re: scripting question

Thanks a lot guys!
All suggestions are quite good!