Linux/hostid
< Linux
Test Set Hostid
#include <stdio.h> #include <unistd.h> int main() { long id,res; // get real (default) hostid id = gethostid(); printf("current hostid is: %xq\n",id); // set new hostid if is superuser res = sethostid(0x11223344); if (res == 0) printf("if result is zero - success! (%d) \n",res); // check if it is changed.... id = gethostid(); printf("current hostid is: %x ;-PPPppppp\n",id); }
Source: http://www.edaboard.com/thread5989.html
Example
Host ID is stored in /etc/hostid as 4 bytes in reverse byte order
# hostid 11223344 # xxd /etc/hostid 0000000: 4433 2211 D3".
Set Hostid
To print a byte, you need to convert it to octal first:
i=65 # ASCII 'A' printf \\$(printf "%o" $i); # 'A'
HOSTID=11223344 C1=`echo $HOSTID | cut -b 7-8` C2=`echo $HOSTID | cut -b 5-6` C3=`echo $HOSTID | cut -b 3-4` C4=`echo $HOSTID | cut -b 1-2` # Convert to Decimal C1=`printf "%d" 0x$C1` C2=`printf "%d" 0x$C2` C3=`printf "%d" 0x$C3` C4=`printf "%d" 0x$C4` # Covert to Octal C1=`printf "%03o" $C1` C2=`printf "%03o" $C2` C3=`printf "%03o" $C3` C4=`printf "%03o" $C4` # Convert to Byte C1=`printf \\\\$C1` C2=`printf \\\\$C2` C3=`printf \\\\$C3` C4=`printf \\\\$C4` # Export to /etc/hostid printf "$C1$C2$C3$C4" > /etc/hostid # Test hostid
Set Random hostid
sethostid
#!/bin/bash if [ "$1" != "" ] ; then HOSTID=$1 # Check length CC=`echo $HOSTID | wc -c` if [ $CC -lt 8 ] ; then echo "Set random hostid or specify [AABBCCDD]" echo "Usage: $0 [AABBCCDD]" exit 1 fi # Get first 4 hex bytes C1=`echo $HOSTID | cut -b 7-8` C2=`echo $HOSTID | cut -b 5-6` C3=`echo $HOSTID | cut -b 3-4` C4=`echo $HOSTID | cut -b 1-2` #printf "Setting hostid: $C4$C3$C2$C1\n" # Convert to Decimal C1=`printf "%d" 0x$C1` C2=`printf "%d" 0x$C2` C3=`printf "%d" 0x$C3` C4=`printf "%d" 0x$C4` else # Generate random hostid C1=$(( $RANDOM % 256 )) C2=$(( $RANDOM % 256 )) C3=$(( $RANDOM % 256 )) C4=$(( $RANDOM % 256 )) # Show Hex #printf "Setting hostid: %x%x%x%x \n" $C4 $C3 $C2 $C1 fi # Covert to Octal C1=`printf "%03o" $C1` C2=`printf "%03o" $C2` C3=`printf "%03o" $C3` C4=`printf "%03o" $C4` # Convert to Byte C1=`printf \\\\$C1` C2=`printf \\\\$C2` C3=`printf \\\\$C3` C4=`printf \\\\$C4` # Export to /etc/hostid printf "$C1$C2$C3$C4" > /etc/hostid # Test hostid