Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

init.d script

Status
Not open for further replies.

krammer

IS-IT--Management
Jul 15, 2007
59
US
Hello,

I have put together a script for "macchanger" to run at startup, and give a random MAC to all interfaces. The problem I'm having is getting it to run at startup. I've done:

chmod a+x macchanger (makes it executable)
chmod 777 macchanger (gives appropriate permissions)
update-rc.d macchanger start 92 1 2 3 4 5 . (adds to default runlevels?...not sure about this one)

When I run it normally after boot with "/etc/init.d/macchanger start" then it changes all macs with no problem, but still does not work at boot. Here is the script:

Code:
#/bin/sh
# macchanger startup script

case "$1" in
  start)
   NICS="$( ifconfig -a | grep "Link encap:Ethernet" | cut -f 1 -d " " )"
   # Randomize MAC address
      for NIC in ${NICS}
      do
         macchanger -r ${NIC/*:/} >/dev/null 2>&1
         macchanger -r ${NIC} > /dev/null 2>&1
         sleep 1
      done
   ;;
esac
exit 0

I have also tried added the full path of macchanger but that did not work as well.
 
try with full pathnames of all commands

ifconfig
grep
cut
macchanger
sleep

also the shebang needs an [tt][red]![/red][/tt]

[tt]#[red]![/red]/bin/sh[/tt]


HTH,

p5wizard
 
p5wizard,

Thanks, I made some changes and also looked at some other init.d scripts, but still get the same result. Works fine normally, but not at boot.

Here is what I changed:

Code:
#!/bin/bash
# Randomize MAC address

PATH=/usr/sbin:/usr/bin:/sbin:/bin

start()
  {
	NICS="$( /sbin/ifconfig -a | /bin/grep "Link encap:Ethernet" | /usr/bin/cut -f 1 -d " " )"
	for NIC in ${NICS}
	do
		/usr/bin/macchanger -r ${NIC/*:/} >/dev/null 2>&1
		/usr/bin/macchanger -r ${NIC} > /dev/null 2>&1
		sleep 1
	done
  }

case "$1" in
 	start)
	  start
	echo "**All MAC's are faked!"
  ;;
*)

echo "Usage: $0 {start}"
exit 1
esac

exit 0
 
I forgot to put it in for sleep "/bin/sleep" but still the same.
 
log the stdout stderr somewhere else than /dev/null so you can read it after the boot...


HTH,

p5wizard
 
I'd replace this:
NICS="$( /sbin/ifconfig -a | /bin/grep "Link encap:Ethernet" | /usr/bin/cut -f 1 -d " " )"
with this:
Code:
NICS=$(/sbin/ifconfig -a | /bin/grep 'Link encap:Ethernet' | /usr/bin/cut -f 1 -d ' ')
or this:
Code:
NICS=$(ifconfig -a | awk '/Link encap:Ethernet/{print $1}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
It works! Here is final script:

PHV- Why does this work now lol?

Code:
#!/bin/bash
# Randomize MAC address

PATH=/usr/sbin:/usr/bin:/sbin:/bin

start()
  {
	#NICS="$( /sbin/ifconfig -a | /bin/grep "Link encap:Ethernet" | /usr/bin/cut -f 1 -d " " )"
	#NICS=$(/sbin/ifconfig -a | /bin/grep 'Link encap:Ethernet' | /usr/bin/cut -f 1 -d ' ')
	NICS=$(/sbin/ifconfig -a | /usr/bin/awk '/Link encap:Ethernet/{print $1}')
	for NIC in ${NICS}
	do
		/usr/bin/macchanger -r ${NIC/*:/} >/home/log 2>&1
		/usr/bin/macchanger -r ${NIC} > /home/log 2>&1
		/bin/sleep 1
	done
  }

case "$1" in
 	start)
	  start
	echo "**All NIC's are now faked!"
  ;;
*)

echo "Usage: $0 {start}"
exit 1
esac

exit 0
 
Why does this work now
For me, it's simply a quotes issue ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
krammer said:
chmod a+x macchanger (makes it executable)
chmod 777 macchanger (gives appropriate permissions)

Actually, I wouldn't call those appropriate permissions... 755 would be better. Otherwise anyone else who has access to your system could add any commands they choose, which would be executed by root on the next boot.

Also the chmod a+x is redundant because 'x' is included in 777 ( = 111 111 111 binary, which = rwx rwx rwx).

Annihilannic.
 
Thanks Annihilannic,

I didn't realize that 777 includes the x to make it executable.
 
Bottom line:
man chmod

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top