Christofer C. Bell wrote:
> /bin/echo dt$(/sbin/ifconfig eth0 | head -1 | awk -F: '{print $4$5$6}') > 
> /etc/hostname

Why the full paths?  Those have only ever hurt me.  It is therefore a
pet peeve of mine to challenge the use of them.  They always hurt me.
And I always remove them whenever I see them.

> That will create a hostname of dt + the last 6 characters of the MAC
> address of eth0, dump that into /etc/hostname, and then "start" the

I think it misses the last two characters.  There is another colon in
the earlier part of the line causing your count to be off by one.

  $ ifconfig eth0
  eth0      Link encap:Ethernet  HWaddr 00:19:21:56:8f:ed  

  $ ifconfig eth0 | head -1 | awk -F: '{print $4$5$6}'
  21568f

Shouldn't that be 568fed instead of 21568f?  I think you wanted:

  ifconfig eth0 | head -1 | awk -F: '{print $5$6$7}'

However that does open yourself up to funny things if ifconfig prints
something else on that line.  I would tend to be paranoid and grab
the entire ethernet address first and then split out the characters
needed.  And that would avoid the problem with colons appearing
elsewhere on the line and also avoid it if ifconfig were to print
something else on the line after it.

  ethernet=$(ifconfig eth0 | awk '{print $NF;exit}')
  echo $ethernet | awk -F: '{print "dt"$4$5$6}'

Since you are using awk there is no need to use head.  Just exit after
the first line.  That removes one process.  And awk can print "dt" too.

There is a movement to switch from the ifconfig command to the ip
command.  If you were wanting to future proof your script I would
recommend using ip instead of ifconfig.  Although for this purpose of
getting the ethernet address I can't see ifconfig going away any time
soon.  But in general it is good to think toward the future and to use
the ip command instead of ifconfig.  And the same for the iw command
instead of iwconfig.  If ip is available then it would be something
like this:

  ethernet=$(ip addr show eth0 | awk '/ether/{print$2}')
  echo $ethernet | awk -F: '{print "dt"$4$5$6}'

Back to the issue of just the last six characters...  Again, awk can
do it.  This is purely a matter of style but I would probably do it
this way.

  ip link show eth0 | awk '/ether/{gsub(/:/,"");print substr($2,5,6);exit}'

However it is the same result as doing it the other way.  All a matter
of style and many different ways to do it.

> /etc/init.d/hostname.sh start
> 
> That will create a hostname of dt + the last 6 characters of the MAC
> address of eth0, dump that into /etc/hostname, and then "start" the
> hostname.sh "service" which simply sets the hostname.  You can read
> what it does in /etc/init.d/hostname.sh.

I would probably want to modify /etc/init.d/hostname.sh so that it set
the hostname there.  That way everything else in the boot process that
expects a hostname will have one as it normally does.

I like the repeatable uniqueness of using the ethernet address here.
It avoids the true randomness problems and things like the birthday
paradox.

> On my system, this gives a hostname of dt7281d9 which I picked sort of
> at random.  Since you're testing things I picked a prefix of dt for
> "device test" and then the 6 (presumably unique) characters from the
> MAC address.  This gives you an 8 character hostname.

In the old days the 'uname -n' was limited to eight characters.  I
believe that is limited to 64 characters now.  So no reason you
couldn't use the entire ethernet address as the hostname.

Bob

Attachment: signature.asc
Description: Digital signature

Reply via email to