On Wed, Nov 4, 2009 at 1:33 PM, Bryn M. Reeves <bmr(a)redhat.com> wrote:
On Wed, 2009-11-04 at 13:13 +0000, Dan Track wrote:
> On Wed, Nov 4, 2009 at 1:10 PM, Dan Track <dan.track(a)gmail.com> wrote:
> > Hi,
> >
> > I'm running a command like this:
> >
> > for i in server1 server2;do ssh root@$i "`hostname`";done.
> >
> > However the hostname command always outputs the hostname of the server
> > that the above command is run from. I'd like to know how to run this
> > hostname command so that it actually runs on server 1, server2 etc..
> >
> > Thanks
> > Dan
> >
>
> Sorry just to add the actual script was like this:
>
> for i in server1 server2;do ssh root@$i "DNSNAME=\"basename
> \`hostname\`\";echo $DNSNAME";done
Not sure why you're setting a variable here but to have "basename" run
as a command and assign the output to DNSNAME you need to have basename
inside a pair of backticks too.
You'll then hit another problem because you want to have nested
backticks (one pair for basename and another for hostname). Bash
supports '$()' as an alternative to backticks that does allow nesting -
writing $(hostname) is equivalent to `hostname` and allows you to write
$(basename $(hostname)).
I'm not sure basename is going to do what you want here though - are you
looking for the short host name or the domain name? The basename command
separates components of a path based on the '/' (or whatever the system
defined path separator is). E.g.:
$ DNS=$(basename $(hostname))
$ echo $DNS
breeves.fab.redhat.com
If you just want the short hostname you can pass -s to hostname:
$ ssh pe1950-1.gsslab hostname -s
pe1950-1
Or the domain with -d:
$ ssh pe1950-1.gsslab hostname -d
gsslab.fab.redhat.com
Have a look at the man page for hostname for more options.
Regards,
Bryn.
Hi Bryn,
Many thanks. I tried hostname -s but I keep getting the following:
hostname: Host name lookup failure
This may be because the hostname's are short already e.g just
"server1" instead of "server1.example.com"
I've updated teh script to your recommendations but I still get the
local hosts hostname in teh output instead of the remote servers
hostname. Any other thoughts?
I now run the following:
for i in server1 server2;do ssh root@$i "DNSNAME=$(basename
$(hostname)$);echo $DNSNAME";done
Thanks
Dan