Jump to content
Sign in to follow this  
ghostxterm

Bash Script To Set Random MAC and Hostname

Recommended Posts

ghostxterm

Just a simple script I wrote. Felt like sharing it with the world. Enjoy. :lol:

P.S. Also thought this would be a good post to talk about different methods of random string/number generation through scripting and other such coding avenues. I used md5sum but is there a better way? Please share your advice and experience.

P.P.S. Just for fun, can anyone think of a way to recode this in windows as a batch script using some kind of cmd-line macchanger?


#!/bin/bash

########################################

#

# Written and Tested on BT5 R1

#

# Script to auto randomize mac address 

#	on interface of users choice

#   	and randomly generate hostname

#   	to secure anonymous identity

#   	prior to network connection.

#

#

# Coded by: ghostxterm@hmamail.com

#

########################################


if [ -n "$1" ]  #  If command-line argument present,

then        	#  then continue.

  echo

  echo "Setting random Mac Address for interface: $1"

else        	#  Else show usage and exit

  echo

  echo "Usage: ghostme <interface>"

  echo "Example: ghostme eth0"

  echo

  exit

fi


##########################################

# Generate Random Numbers and Characters 

##########################################


str0="$$" #use PID to set initial string

LEN=2

POS1=2

POS2=5

POS3=8

POS4=11

POS5=14

str1=$( echo "$str0" | md5sum | md5sum ) #run str0 through md5sum twice

set1="${str1:$POS1:$LEN}" #starts from position 2 of string

set2="${str1:$POS2:$LEN}" #starts from position 4...

set3="${str1:$POS3:$LEN}" # 			and so on...

set4="${str1:$POS4:$LEN}"

set5="${str1:$POS5:$LEN}"

hostname1="${str1:16:8}"


#######################

# Main part of script

#######################


ifconfig $1 down

ifconfig $1 hw ether 00:$set1:$set2:$set3:$set4:$set5

ifconfig $1 up

hostname "$hostname1.com"

 echo

 echo "New MAC is set to 00:$set1:$set2:$set3:$set4:$set5"

 echo "New HOSTNAME is set to $hostname1.com"

 echo

 echo "Testing with: ifconfig $1"

 echo

 echo "--------------------------------------------------------"

ifconfig $1 | grep -i "hwaddr"

 echo "--------------------------------------------------------"

 echo

exit


Share this post


Link to post
ghostxterm

I changed this part of the script to address issues with host assignment (e.g. 'sudo' returning with couldn't resolve hostname) It starts at line 55 and replaces the original 'hostname' command after 'ifconfig up'.



hostname "$hostname1.computer.com"


if [ -a /etc/bak.txt ]

then

  echo "bak.txt found, skipping backup..."

  rm /etc/hosts

  rm /etc/hostname

else

  cat /etc/hostname >> /etc/bak.txt #runs if this is first time being run

  mv /etc/hosts /etc/hosts.bak #back up hosts file so it can be restored if needed

  mv /etc/hostname /etc/hostname.bak #back up hostname file...

fi


echo "$hostname1" >> /etc/hostname


echo "127.0.0.1   	localhost" >> /etc/hosts

echo "127.0.1.1   	$hostname1.computer.com  	$hostname1" >> /etc/hosts

echo " " >> /etc/host.conf

echo "# The following lines are desirable for IPv6 capable hosts" >> /etc/hosts

echo "::1 	localhost ip6-localhost ip6-loopback" >> /etc/hosts

echo "fe00::0 ip6-localnet" >> /etc/hosts

echo "ff00::0 ip6-mcastprefix" >> /etc/hosts

echo "ff02::1 ip6-allnodes" >> /etc/hosts

echo "ff02::2 ip6-allrouters" >> /etc/hosts

echo "ff02::3 ip6-allhosts" >> /etc/hosts



Share this post


Link to post
t3rm1t

Useful tool for change mac is macchanger included in BT 5.

Share this post


Link to post

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  
×