You are not logged in.
Hello archers. I'm a bit embarassed about a problem that I've been trying to solve and can't. I want to know how to have custom bash scripts run at startup in systemd. Currently I'm trying to get this one to run at startup:
#! /bin/sh
sudo ifconfig wlan0 down
sudo macchanger -r wlan0
sudo ifconfig wlan0 up
sleep 1
echo -e ' \t '
echo "Mac address successfully changed."
exec bash
It doesn't seem to work. I looked up mac address spoofing in the Arch wiki and tried my best to follow this:
Systemd Unit
Same thing with systemd:
/etc/systemd/system/macspoof@.service
[Unit]
Description=MAC address change %I
Before=dhcpcd@%i.service
[Service]
Type=oneshot
ExecStart=/usr/sbin/ip link set dev %i address 36:aa:88:c8:75:3a
ExecStart=/usr/sbin/ip link set dev %i up
[Install]
WantedBy=network.target
No dice. Here's what mine looks like:
[Unit]
Description=MAC address change %I
Defore=dhcpcd@i.service
[Service]
Type=oneshot
ExecStart=sudo ifconfig wlan0 down
ExecStart=sudo macchanger -r wlan0
[Install]
WantedBy=network.target
There's obviously something I'm missing and Google doesn't seem to be yielding ANY results related to running custom scripts at startup in systemd.
Thanks for reading.
Last edited by xworld (2013-02-12 05:59:29)
Offline
You have a pretty glaring typo in the service file... And I don't think you will need sudo.
Offline
You didn't needed to create a service based on macspoof@.service, you only needed to enable it adding your wlan device after the @. Like this:
systemctl enable macspoof@wlan0.service
(assuming you still use wlan0 as your wifi device)
Read man systemd.unit to understand what %i means.
And, unless is a service executed by systemd user session service or you are using the User= parameter, the services are executed by root, so sudo is not necessary.
Also, since systemd does not execute the commands by using a shell, but using an exec() call, is better you don't expect it to guess the path.
So, change lines like this:
ExecStart=sudo macchanger (...)
to this:
ExecStart=/usr/bin/macchanger (...)
(I'm guessing macchanger is on /usr/bin; I don't know, I don't have it installed. Find your path using whereis macchanger)
And one last thing, ifconfig is deprecated, use ip instead.
"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.
Offline
Ah yes, I guess 'before' is not spelled with a D. And I see I missed a % in there as well. Thank you guys for your fast responses. So instead of ifconfig wlan0 down would I do something like:
ip link set wlan0 down?
Also, something I've always noticed is that I can run ifconfig wlan0 down and then change the mac but running ifconfig wlan0 up never works. I have to manually click on networkmanager and then reconfigure my network in order to be able to connect to the internet.
Hmm. I must be doing something wrong still. Here's my new file:
[Unit]
Description=MAC address change %I
Before=dhcpcd@%i.service
[Service]
Type=oneshot
ExecStart=/sbin/ifconfig wlan0 down
ExecStart=/usr/bin/macchanger -r wlan0
[Install]
WantedBy=network.target
It still doesn't want to work.
Last edited by xworld (2013-01-17 02:02:17)
Offline
Well ifconfig wlan0 up would only set the card up; it wont do the wpa/wep things, or get you an address with dhcpcd, etc.
That is, it will set the card as "up", but it wont reconnect it to the lan.
Thats the reason the macspoof service has Before=dhcpcd@%i.service; to make the mac change before it tries to get an ip with dhcpcd.
EDIT: I think I'm missing something. Is there a reason for not just doing
systemctl enable macspoof@wlan0.service
instead of creating a custom systemd service?
Last edited by chris_l (2013-01-17 02:09:05)
"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.
Offline
Ah, I understand. So if I wanted a custom script to run at startup would i put the path to that script in the ExecStart? Because so far nothing seems to work.
Offline
To execute an script, call it as argument of the interpreter. In another words:
ExecStart=/bin/bash /etc/myscript.sh
"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.
Offline
Ok thanks very much. I suppose I should still use the other variables in the macspoof script that I've been working with. I can't figure out why it won't run at start-up. I also suppose I'm on my own from here.
Thanks again for the help.
EDIT: How foolish of me. It works now. I forgot to do:
systemctl start macspoof@wlan0.service
The only trouble is that it doesn't open up a terminal and then run the commands I asked it to, even though the functionality is all there. Here's the script it's running:
#! /bin/sh
sudo ifconfig wlan0 down
sudo macchanger -r wlan0
sudo ifconfig wlan0 up
sleep 1
echo -e ' \t '
echo "Mac address successfully changed."
exec bash
But instead of opening a terminal and then printing out what I asked it to after changing the mac it simply changes the mac. Which is good. But, is there something I'm missing?
Last edited by xworld (2013-01-17 05:04:33)
Offline
But instead of opening a terminal and then printing out what I asked it to after changing the mac it simply changes the mac. Which is good. But, is there something I'm missing?
You are missing the fact that "not opening a terminal" is the expected behavior
Maybe you'll see a log of what happened by doing systemctl status macspoof@wlan0.service
But, there is a point on doing that?
"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.
Offline
Why would it open a terminal, you didn't tell it to? What terminal do you want it to use? And what is the "exec bash" at the end for? That will do absolutely nothing useul. Nor will the sudo (which was pointed out above).
Last edited by Trilby (2013-01-17 16:31:00)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
I realized today while I was thinking about it that I didn't actually give a command to open the terminal. And the exec bash is because before I did that it printed everything on the screen and exited so fast that I couldn't read any of it. So exec bash was to keep the terminal open after it was done.
It was kind of an experimental script.
Offline
Most terminals can be passed a parameter to stay open after the command passed to them completes.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Exec bash was the one I landed on. Although I imagine there are better ways.
Offline
I known that this is somewhat tangential, but why aren't you using a udev rule (it doesn't require bringing the interface down)?
SUBSYSTEM=="net", ENV{DEVTYPE}=="wlan", RUN+="/usr/bin/macchanger -r $env{INTERFACE}"
Offline