How to use MAC address as hostname in thin client? (Solved)

Using applications, configuring, problems
Post Reply
Message
Author
atomicdog602
Posts: 8
Joined: Thu 26 Apr 2018, 13:29

How to use MAC address as hostname in thin client? (Solved)

#1 Post by atomicdog602 »

Issue: mac address as hostname

BACKGROUND: I have a large farm of foxconn thin clients currently running an ancient build of thinstation. Seeing I did not build this image, and it being so old, I started on creating a tahrpup TFTP boot setup as replacement. This forum has answered and solved almost every problem just by searching. But I have an issue I really need help solving.

ISSUE: Our current setup with thinstation creates a hostname ts_macadddress no colons. This is how we identify machines. Somehow during boot I need to insert a script (I think) in etc/rc.d/rc.sysinit that asks ifconfig eth0 its mac address, removes colons, appends ts_ in front of the mac and then makes it the clients hostname by updating (or creating?) etc/hosts and etc/hostname. Remember all the clients boot off an image I made from a snapshot so right now they all have the same hostname. This is an assumption from my feeble attempts at searching for a solution.

I have found hints at doing this, but because I have no clue about scripting I am stuck. I am going to post snippets of stuff I found that might help somebody here that can maybe help me.

The following is not code for puppy, but maybe it helps...

This is stuff I found from a rasberry pi forum about mac as hostname:

Code: Select all

MAC="pi""$( sed "s/^.*macaddr=\([0-9A-F:]*\) .*$/\1/;s/://g" /proc/cmdline )"
echo "$MAC" > "/etc/hostname"
CURRENT_HOSTNAME=$(cat /proc/sys/kernel/hostname)
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$MAC/g" /etc/hosts
hostname $MAC
This was on a linux forum:

Code: Select all

HOSTNAME=(`/sbin/ifconfig eth0 |grep HWaddr |cut -d ' ' -f11 |sed 's/://g'`)

echo HOSTNAME=$HOSTNAME >> /etc/sysconfig/network
HOSTNAME $HOSTNAME
echo NETWORKING=yes > /etc/sysconfig/network
And for the life of me somewhere I saw a discussion about using ifconfig to return the mac address for another purpose at boot but it is lost in the abyss.

This one thing is the last hurdle for me and I am stuck. Can anybody help?

Thank you in advance! ps. I know it might be stupid, but the boss wants it exactly the same so here I am.

User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

#2 Post by fabrice_035 »

not sure understand, but try that:

Code: Select all

hostname `echo ts_`$(/sbin/ifconfig eth0 |grep HWaddr |cut -d ' ' -f11 |sed 's/://g'
add in Startup directory

Regard

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#3 Post by jafadmin »

Code: Select all

New_Hostname="ts_"$(sed s/://g /sys/class/net/eth0/address)
hostname $New_Hostname
Last edited by jafadmin on Fri 27 Apr 2018, 01:29, edited 1 time in total.

atomicdog602
Posts: 8
Joined: Thu 26 Apr 2018, 13:29

#4 Post by atomicdog602 »

Oh fantastic! Looks like either may work (as best I can tell from the logic). Will try both.

Now how would I update the hostname file and hosts file? As these machines boot off an image that already has a hostname and host files set?

Am I right assuming that a variable has to be set and inserted into those two files etc/hosts and etc/hostname?

Like underneath one of those commands above assign the name to $hostname then code the insertion into the two files? Which I have no clue how to do.

***Another question would be: could I just delete the hostname and hosts file from the image? Would they be (re)created if they don't exist -even though its an image of a running system?

I may be talking out of my butt. I am only guessing from all my failed searches.

I will be able to test this tonight and will respond back with the results. And test any further suggestions. Thank you thus far. This means a lot to me. I have documented my entire process going from live USB to custom PXE boot image and will be creating a how to from my perspective to give back. Soon like tomorrow I hope!

atomicdog602
Posts: 8
Joined: Thu 26 Apr 2018, 13:29

#5 Post by atomicdog602 »

Both scripts work as far as returning the mac correctly (the first with the addition of a closed parenthes and an =) but where I put it in rc.sysinit is not returning the mac address. I get just "ts_" so where this is in rc.sysinit it must be too early to make this request of ifconfig or cat ../eth0/net/address

Is rc.sysinit not the place to do it? Is there a place somewhere else I need to insert the code? Right now it is inserted where the code check for the name "puppypc" and if exists creates a random name. (around line 300) I assumed this would be the right place

I guess I am asking if somebody knows at what point I should insert this code so the machine identifies itself as the new name when calling out to the network? Somewhere else in rc.sysinit, or another file like rc.local?

This is my very ugly code:

Code: Select all

#!/bin/ash

#added by AtomicDog set mac address as hostname for unique identification
New_Hostname=`echo ts_`$(/sbin/ifconfig eth0 |grep HWaddr |cut -d ' ' -f11 |sed 's/://g')
#New_Hostname="ts_"$(sed s/://g /sys/class/net/eth0/address)
hostname $New_Hostname
echo $New_Hostname > /etc/hostname
HOSTSFILEFIRST="`grep -w 'puppypc11400' /etc/hosts|sed 's% puppypc11400%%'`"
HOSTSFILEREST="`grep -v 'localhost' /etc/hosts`"
echo "$HOSTSFILEFIRST $New_Hostname" > /etc/hosts
echo "$HOSTSFILEREST" >> /etc/hosts
#testing things are working right
echo hostname is: $New_Hostname
echo Hosts file is:
echo $HOSTSFILEFIRST
echo $HOSTSFILEREST

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#6 Post by jafadmin »

Test this script in a console, and if it gets you to the show, add it
to rc.local

Code: Select all

#!/bin/sh

OLD_HOSTNAME="$( hostname )"
NEW_HOSTNAME="ts_"$(sed s/://g /sys/class/net/eth0/address)

echo "Changing hostname from $OLD_HOSTNAME to $NEW_HOSTNAME..."

hostname "$NEW_HOSTNAME"

if [ -n "$( grep "$OLD_HOSTNAME" /etc/hosts )" ]; then
 sed -i "s/$OLD_HOSTNAME/$NEW_HOSTNAME/g" /etc/hosts
 echo -e "$NEW_HOSTNAME" > /etc/hostname
fi

echo "Done."

User avatar
Mike Walsh
Posts: 6351
Joined: Sat 28 Jun 2014, 12:42
Location: King's Lynn, UK.

#7 Post by Mike Walsh »

@ atomicdog:-

Perhaps /etc/init.d might be a better place for your script..?

I run a small utility called BitMeteros, which gives a real-time readout of bandwidth used across my systems. Runs from /var/lib, periodically writing to an SQL database in the same location.

It's started from /etc/init.d/bitmeter & /etc/init.d/bitmeterweb (the second starts the web interface, since it's viewable in the browser itself.....the major culprit for clocking up data usage.)

Point being that it needs the network to have initialized itself, connected and 'settled down' fully before it starts up. /etc/init.d is also the location I use for 'auto-mount' scripts in some of my older Pups which don't utilise the newer Pmount. The newer version does set-up auto-mounting; I need this, because I run a number of common apps across the kennels from a remote data partition through the use of extensive sym-linking.....so those partitions have to be 'auto-mounted' before Puppy is fully initialized.

Just my tuppence-worth.


Mike. :wink:

atomicdog602
Posts: 8
Joined: Thu 26 Apr 2018, 13:29

#8 Post by atomicdog602 »

Jafadmin your much more elegant script works just fine, but the machine has already registered on the network as puppypc11400 -the default name with random number generated at startup (around line 300 in rc.sysinit).

As mentioned inserting your elegant code (or my ugly code) in place, or just after line 300 in rc.sysinit does not work (the code does work, just not yet). I assume the ethernet adapter has not initialized so there is no mac address available yet to give (whether using cat or using ifconfig).

Your code is solid, but using it in rc.local is too late as far as the network already identifying the machine as puppypc(randomnumber). It does update hostname and hosts properly when rc.local is called.

I am going to try your code where Mike suggests. If I can just find out where to put it, my life will be complete. Heck, I'm open to any and all suggestions you have as to where I can stick it.

And thanks for the help as always! :D
Attachments
Dunning Kruger Chart2.jpg
(31.85 KiB) Downloaded 233 times

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#9 Post by jafadmin »

So you need this to run before the network connects, right? Is your image relying on one of the automatic network connecting tools like SNS or Frisbee?

If it were me, I'd replace the rc.network with rc.network_basic and do the naming and dhcp, there. I only ever use Dougal's network tool, so this solution works for me. Some of the other tools apparently do not use the rc.network file to init.

All my puppies boot up without network, then I manually connect them to a network, or use my own "rc." script to do it.

A new /etc/rc.d/rc.network would look something like this:

Code: Select all

#!/bin/sh

ifconfig lo 127.0.0.1
route add -net 127.0.0.0 netmask 255.0.0.0 lo

#begin rerwin
if [ -f /root/.dhcpcd.duid ];then  #Using a DUID in client ID
 #If running from pup_save file and no common DHCP Unique Identifier (DUID) present and partition has a full-install or partition-save and a duid file, make that file the common duid.
 [ ! -s /mnt/home/.common.duid -a ! "`ls -l /mnt/home | cut -f 11 -d ' '`" = "/" -a -s /mnt/home/root/.dhcpcd.duid ] && cp /mnt/home/root/.dhcpcd.duid /mnt/home/.common.duid
 #Ensure that any local duid matches the common duid or will force new duid.
 [ -s /mnt/home/.common.duid ] && cp /mnt/home/.common.duid /root/.dhcpcd.duid || cp /dev/null /root/.dhcpcd.duid
 if [ -s /root/.dhcpcd.duid ];then   #duid info for DHCP request.
  [ ! -d /var/lib/dhcpcd ] && mkdir /var/lib/dhcpcd
  cp /root/.dhcpcd.duid /var/lib/dhcpcd/dhcpcd.duid
 else  #Force new DUID
  [ -f /var/lib/dhcpcd/dhcpcd.duid ] && rm /var/lib/dhcpcd/dhcpcd.duid
 fi
fi
#end rerwin

NIC="eth0"
OLD_HOSTNAME="$( hostname )"
NEW_HOSTNAME="ts_"$(sed s/://g /sys/class/net/$NIC/address)
hostname "$NEW_HOSTNAME"
if [ -n "$( grep "$OLD_HOSTNAME" /etc/hosts )" ]; then
 sed -i "s/$OLD_HOSTNAME/$NEW_HOSTNAME/g" /etc/hosts
 echo -e "$NEW_HOSTNAME" > /etc/hostname
fi

rm -f /var/lib/dhcpcd/*.info 
rm -f /var/run/*.pid 
ifconfig $NIC up
sleep 3
dhcpcd -t 30 -h "$(hostname)" -d $NIC

###END###
Last edited by jafadmin on Tue 05 Jun 2018, 15:32, edited 1 time in total.

atomicdog602
Posts: 8
Joined: Thu 26 Apr 2018, 13:29

#10 Post by atomicdog602 »

jafadmin wrote:So you need this to run before the network connects, right? Is your image relying on one of the automatic network connecting tools like SNS or Frisbee?

If it were me, I'd replace the rc.network with rc.network_basic and do the naming and dhcp, there. I only ever use Dougal's network tool, so this solution works for me. Some of the other tools apparently do not use the rc.network file to init.

All my puppies boot up without network, then I manually connect them to a network, or use my own "rc." script to do it.

A new /etc/rc.d/rc.network would look something like this:
What sort of mystical voodoo is this? So I replaced rc.network with your code and it *seems* to be working. I won't know until machines are all rolled out, but so far it feels good on the whole. Multiple machines on my Out house table seem to work as expected.

Thank you. No way would I have figured this out without your help.

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#11 Post by jafadmin »

Cool. Gook luck with the rollout. Let us know. 8)

atomicdog602
Posts: 8
Joined: Thu 26 Apr 2018, 13:29

#12 Post by atomicdog602 »

I did a small test rollout and there were 2 snags. One was the client sending a long mac address, I was able to fix this by changing dhcpcd.conf line 11 (identify by clientID instead of duid) The other is unusual because previously the machines were identifying themselves as "puppypc11400" which was the hostname of all the clones I made.

Now the machines identify themselves as just "hostname" on the windows DHCP server. See attached image. The machines with ts_mac are the existing old clients.

I can live with this if I have to (the machines themselves definitely identify as ts_macaddy), but I am wondering if something else needs to be fixed or if there is a slight glitch in the code.


[/img]
Attachments
DHCP.jpg
(27.07 KiB) Downloaded 141 times

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#13 Post by jafadmin »

Try this change, then:

Code: Select all

dhcpcd -t 30 -h hostname -d $NIC

change to:

dhcpcd -t 30 -h "$NEW_HOSTNAME" -d $NIC

atomicdog602
Posts: 8
Joined: Thu 26 Apr 2018, 13:29

#14 Post by atomicdog602 »

Yep that was it. I tried myself but did not have quotes around the variable. I knew it was easy.

I almost wish I could upload the image for others. It identifies itself by mac address, does not ask to save when closing, and I can even package it as a 300MB TFTP bootable image -that seems to work on almost anything that can handle 64bit.

Thanks again for guiding me through this.

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#15 Post by greengeek »

atomicdog602 wrote: and I can even package it as a 300MB TFTP bootable image -that seems to work on almost anything that can handle 64bit.
is TFTP similar to PXE?

atomicdog602
Posts: 8
Joined: Thu 26 Apr 2018, 13:29

#16 Post by atomicdog602 »

Yea sorry. PXE. Works great.

Post Reply