You are not logged in.

#1 2017-04-30 23:42:42

insanoflex
Member
Registered: 2017-04-29
Posts: 15

wireless connection script doesn't seem to do anything.

I'm using a simple script to connect to my wireless network. When run, there is no output, and I'm not connected. Here's the script:

#!/bin/bash

exec ip link set wlp3s0 up
exec iw dev wlp3s0 connect "Wireless_Service"
exec dhcpcd wlp3s0

The weird thing (to me) is that when I run these commands from the terminal, it works just fine.

Offline

#2 2017-04-30 23:49:33

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,238
Website

Re: wireless connection script doesn't seem to do anything.

First, the 'exec' commands are supurfluous and can be removed.

Add `set -eu` below the shebang (#!/bin/bash) which will tell bash to abort if any command fails.  Lastly, execute the script with `bash -x` to execute it in debug mode (ie, bash -x /path/to/this/script.sh)

Offline

#3 2017-04-30 23:59:30

insanoflex
Member
Registered: 2017-04-29
Posts: 15

Re: wireless connection script doesn't seem to do anything.

Ok, its working now, I'm not sure why now. But now I know how to debug it if it stops. Thanks! Actually, I have another question. This script requires root access, and I'd like to run it when I log in. It's okay to me if the script asks the user for root password (I'm the only user). Is there a way to have a script do this? I've tried doing a 'su' in the script, but I'm not sure how to leave the script not logged in with root priviledges, and I'm guessing doing a 'su' in a script isn't a good idea.

Last edited by insanoflex (2017-05-01 00:02:33)

Offline

#4 2017-05-01 06:35:03

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,891

Re: wireless connection script doesn't seem to do anything.

#!/bin/sh
if [ $UID != 0 ]; then
    sudo $0
    exit
fi
# payload action here

But you can also just sudo the commands inside. You'll be asked for the first one and then the sudo cache covers the other two.
Or: you just execute the script as sudo from your bashrc AND allow it NOPASWD in sudoers.

Be aware that the script should be smarter for auto-invocation, because it will raise conflicts on multiple logins.

Offline

#5 2017-05-01 06:38:01

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: wireless connection script doesn't seem to do anything.

Why not just use systemd-networkd? You get much better fault handling, good logging and you don't have to jump through all these janky hoops...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB