Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sat 23 Jun 2018, 10:23
All times are UTC - 4
 Forum index » House Training » Users ( For the regulars )
How to use MAC address as hostname in thin client? (Solved)
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 2 [16 Posts]   Goto page: 1, 2 Next
Author Message
atomicdog602

Joined: 26 Apr 2018
Posts: 8

PostPosted: Thu 26 Apr 2018, 11:54    Post subject:  How to use MAC address as hostname in thin client? (Solved)
Subject description: Need hostname as MAC address to match current setup
 

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:
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:
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.
Back to top
View user's profile Send private message 
fabrice_035


Joined: 28 Apr 2014
Posts: 469

PostPosted: Thu 26 Apr 2018, 13:38    Post subject:  

not sure understand, but try that:

Code:

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


add in Startup directory

Regard
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Thu 26 Apr 2018, 13:46    Post subject:  

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

Last edited by jafadmin on Thu 26 Apr 2018, 21:29; edited 1 time in total
Back to top
View user's profile Send private message 
atomicdog602

Joined: 26 Apr 2018
Posts: 8

PostPosted: Thu 26 Apr 2018, 15:59    Post subject:  

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!
Back to top
View user's profile Send private message 
atomicdog602

Joined: 26 Apr 2018
Posts: 8

PostPosted: Fri 27 Apr 2018, 11:35    Post subject:  

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:
#!/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
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Sat 28 Apr 2018, 11:59    Post subject:  

Test this script in a console, and if it gets you to the show, add it
to rc.local
Code:
#!/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."
Back to top
View user's profile Send private message 
Mike Walsh


Joined: 28 Jun 2014
Posts: 3800
Location: King's Lynn, UK.

PostPosted: Sun 29 Apr 2018, 20:16    Post subject:  

@ 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

_________________
MY PUPPY PACKAGES | 'Thanks' are always appreciated!
--------------------------------------

Back to top
View user's profile Send private message Visit poster's website 
atomicdog602

Joined: 26 Apr 2018
Posts: 8

PostPosted: Sun 29 Apr 2018, 23:35    Post subject:  

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! Very Happy
Dunning Kruger Chart2.jpg
 Description   
 Filesize   31.85 KB
 Viewed   183 Time(s)

Dunning Kruger Chart2.jpg

Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Mon 30 Apr 2018, 19:21    Post subject:  

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:
#!/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, 11:32; edited 1 time in total
Back to top
View user's profile Send private message 
atomicdog602

Joined: 26 Apr 2018
Posts: 8

PostPosted: Wed 09 May 2018, 13:30    Post subject:  

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 kitchen table seem to work as expected.

Thank you. No way would I have figured this out without your help.
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Wed 09 May 2018, 18:30    Post subject:  

Cool. Gook luck with the rollout. Let us know. Cool
Back to top
View user's profile Send private message 
atomicdog602

Joined: 26 Apr 2018
Posts: 8

PostPosted: Fri 11 May 2018, 12:46    Post subject:  

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]
DHCP.jpg
 Description   
 Filesize   27.07 KB
 Viewed   97 Time(s)

DHCP.jpg

Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 808

PostPosted: Sat 12 May 2018, 09:01    Post subject:  

Try this change, then:
Code:

dhcpcd -t 30 -h hostname -d $NIC

change to:

dhcpcd -t 30 -h "$NEW_HOSTNAME" -d $NIC
Back to top
View user's profile Send private message 
atomicdog602

Joined: 26 Apr 2018
Posts: 8

PostPosted: Wed 16 May 2018, 21:09    Post subject:  

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.
Back to top
View user's profile Send private message 
greengeek


Joined: 20 Jul 2010
Posts: 5054
Location: Republic of Novo Zelande

PostPosted: Sun 27 May 2018, 15:05    Post subject:  

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?
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 2 [16 Posts]   Goto page: 1, 2 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » House Training » Users ( For the regulars )
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1026s ][ Queries: 15 (0.0201s) ][ GZIP on ]