experimental replacement for ceni

Programming (no configs, no support)
Forum rules
Only original work ;)
User avatar
liknites
Oyster-Slurper
Posts: 26
Joined: Fri Jun 05, 2015 1:31 am
Location: Nouvelle France

experimental replacement for ceni

Unread post by liknites » Tue Jun 09, 2015 1:07 pm

After having unusual trouble getting ceni to work, I thought to myself "isn't this a bit overwrought?" In the spirit of bloatless bohdisattva, the poor mud-wretch that I am devised a simple shell script to configure wireless networking. I'm new to this, so I thought I'd ask for advice on the following:

Code: Select all

#!/bin/sh

# acquire WiFi info
ip link set wlan0 up
echo -n "Network Name: "
read NAME
echo -n "Password: "
read PASS

# write configuration file
cat <<EOT > /etc/wpa_supplicant.conf
# WPA-PSK/TKIP
update_config=1
ctrl_interface=/var/run/wpa_supplicant

network={
	ssid="$NAME"
	psk="$PASS"
}
EOT

# connect to the network
iwconfig wlan0 essid $NAME
wpa_supplicant -i wlan0 -B -Dnl80211 -c /etc/wpa_supplicant.conf
dhclient wlan0
What do you think? I've considered adding a few featurbugs like scanning for available networks, but I'm trying to KISS the nut of the problem first.

User avatar
liknites
Oyster-Slurper
Posts: 26
Joined: Fri Jun 05, 2015 1:31 am
Location: Nouvelle France

Re: experimental replacement for ceni

Unread post by liknites » Tue Jun 09, 2015 1:28 pm

In case it was unclear, the script prompts the user for their wirless ESSID and WPA passphrase. It then writes the wpa_supplicant.conf file (in plaintext, is this really a security threat?) and initializes wpa_supplicant and dchlient.

I'm not sure how to get the script to autodetect things like the wireless driver (nl80211 in this case) and I assume wlan0 is the interface (maybe a bad assumption?).

I'm trying to figure out how to take the output of

Code: Select all

 iwlist wlan0 scan | grep ESSID | cut -d '"' -f2 
or somesuch and render it as a selectable list in POSIX-compliant shell. The idea being instead of typing the network name you could just pick from a range of options, which is fine as long as the network isn't hidden.

Thoughts?

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: experimental replacement for ceni

Unread post by wuxmedia » Tue Jun 09, 2015 2:16 pm

http://tldp.org/LDP/Bash-Beginners-Guid ... 09_06.html
for the menu?
ceni and netpro work pretty nice for me, suppose perl is a bit of a bloat :)
although i never got the hang on wpa-things...
if the file is locked to root then it's not so insecure, it's how /e/n/i works.
"Seek, and Ye shall find"
"Github | Chooons | Site"

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: experimental replacement for ceni

Unread post by machinebacon » Tue Jun 09, 2015 2:46 pm

Actually ceni is really not needed (by the way, if ceni vomits on the floor recently, it's due to a udev bug, see the upgrade warnings section), and I'd be the first to remove it, together with its perl bloatness.

to create an array:

Code: Select all

ESSID_TMP=/tmp/essid
iwlist scan | grep ESSID | cut -d '"' -f2 > $ESSID_TMP  # removed wlan0 from iwlist scan
ar=( $(cat "$ESSID_TMP") )
select opt in "${ar[@]}"; do
# here do something with the ESSID
break
done
something like this? Remember, some wifi devices are not wlan0 but eth1, and soon (probably) renamed to the Red Hat style ugliness. In the command above, I have tried without explicitly selecting a device, and it populated the ESSID_TMP file correctly, without errors. You might try!
..gnutella..

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: experimental replacement for ceni

Unread post by machinebacon » Tue Jun 09, 2015 3:10 pm

/moved it to the Programming section because it's more script than config :)
..gnutella..

User avatar
liknites
Oyster-Slurper
Posts: 26
Joined: Fri Jun 05, 2015 1:31 am
Location: Nouvelle France

Re: experimental replacement for ceni

Unread post by liknites » Wed Jun 10, 2015 3:45 am

Thanks everybody for the input.

@wux: It seems like networking is one of the more arcane aspects of linux, at least to me.

@bacon: Thanks for the suggestions and tips. Apparently select is found only in bash, ksh, and zsh. Maybe I'll suck it up, but for now I'm looking for a more portable workaround. I've added checking to see if a configuration file already exists and offering to use it, and a few other minor improvements.

Here's what I've got so far:

Code: Select all

#!/bin/sh

# if connected, nuke the 'net
if ( ps -A | grep "wpa_supplicant" > /dev/null 2>&1 ); then
	echo "\nKilling connection..." &
	kill $(ps -A | awk '/wpa_supplicant/ {print $1}')
fi

# look for past configurations and ask to use
if [ -e /etc/wpa_supplicant.conf ]; then
	echo "\nPrevious configuration found:\n"
	echo "$(awk '/"/' /etc/wpa_supplicant.conf)\n"
	read -p "Use the above (y/n)? " ANSWER1
	if echo "$ANSWER1" | grep -iq "^y"; then
		wpa_supplicant -i wlan0 -B -Dnl80211 -c /etc/wpa_supplicant.conf > /dev/null 2>&1 
		dhclient wlan0 > /dev/null 2>&1
		if ( ps -A | grep "wpa_supplicant" > /dev/null 2>&1 ); then
			echo "\nConnected!\n"
		else
			echo "\nFailed.\n"
		fi
		exit
	else
		break
	fi
fi

# acquire WiFi info
ip link set wlan0 up > /dev/null 2>&1
echo -n "\nNetwork: "
read NAME
echo -n "Password: "
read PASS

# write configuration file
cat <<EOT > /etc/wpa_supplicant.conf
# WPA-PSK/TKIP
update_config=1
ctrl_interface=/var/run/wpa_supplicant

network={
	ssid="$NAME"
	psk="$PASS"
}
EOT

# connect to the network
iwconfig wlan0 essid $NAME > /dev/null 2>&1
wpa_supplicant -i wlan0 -B -Dnl80211 -c /etc/wpa_supplicant.conf > /dev/null 2>&1
dhclient wlan0 > /dev/null 2>&1

# check connection
if ( ps -A | grep "wpa_supplicant" > /dev/null 2>&1 ); then
	echo "\nConnected!\n"
else
	echo "\nFailed.\n"
fi
I've already mentioned that I'm new to putting out scripts & such for others to view, so I appreciate any and all feedback.

User avatar
liknites
Oyster-Slurper
Posts: 26
Joined: Fri Jun 05, 2015 1:31 am
Location: Nouvelle France

Re: experimental replacement for ceni

Unread post by liknites » Mon Jun 15, 2015 5:28 pm

Update

After fiddling with it for a few days, I've improved the UI, added better errors and exceptions, and of course ascii art!

Code: Select all

#!/bin/sh
echo
echo "#    #               # ## # "
echo "#    #                 #    "
echo "#### #### #### # # # # ## # "
echo "#  # #  # #  # # # # # #  # "
echo "#### #### #### ##### # #  # "
echo "             #              "
echo "             #              "
echo "........................... "
echo
CONF=/etc/wpa_supplicant.conf
if ( pgrep dhclient ) > /dev/null 2>&1
	then
		kill "$( pgrep dhclient )"
fi
if ( pgrep wpa_supplicant ) > /dev/null 2>&1
	then
		kill "$( pgrep wpa_supplicant )"
fi
echo "Available devices:"
echo 
ip link | awk '/</ {print $2 | "tr -d :"}'
echo
printf "Interface: "; read FACE
ip link set "$FACE" down > /dev/null 2>&1
if [ $? -eq 1 ]
	then
		echo
    		echo "Device '$FACE' not found, exiting..."
		echo
		exit
fi
ip link set "$FACE" up
echo
if [ -e /etc/wpa_supplicant.conf ]; then
	echo "Previous configuration found:"
	echo
	awk '/"/' /etc/wpa_supplicant.conf
	echo
	printf "Use the above (y/n)? "; read ANSWER
	echo
	if echo "$ANSWER" | grep -iq "^y"
	then
		wpa_supplicant -i "$FACE" -B -Dwext,nl80211 -c "$CONF" > /dev/null 2>&1
		dhclient "$FACE" > /dev/null 2>&1
		exit
	fi
fi
echo "Scanning for networks..."
iwlist "$FACE" scan | awk -F '"' '/ESSID/ {print $2 | "sort -u"}' 2> /dev/null
echo 
printf "Network: "; read NAME
iwconfig "$FACE" essid "$NAME" > /dev/null 2>&1
echo
stty -echo
printf "Password: "; read PASS
stty echo
echo
if [ -z "$PASSWORD" ]; then
	ip link set "$FACE" up
	echo "Connecting to the network..."
	echo
	iwconfig "$FACE" essid "$NAME" > /dev/null 2>&1
	dhclient "$FACE" > /dev/null 2>&1
	exit
fi
echo
echo "Writing configuration file..."
cat <<EOT > "$CONF"
# WPA-PSK/TKIP
update_config=1
ctrl_interface=/var/run/wpa_supplicant

network={
	ssid="$NAME"
	psk="$PASS"
}
EOT
echo "Starting wpa_supplicant..."
wpa_supplicant -i "$FACE" -B -Dwext,nl80211 -c "$CONF" > /dev/null 2>&1
echo "Connecting to the network..."
echo
dhclient "$FACE" > /dev/null 2>&1
exit

Anyone want to help test?

now with delectable POSIX compatibility!
Last edited by liknites on Mon Jun 15, 2015 7:57 pm, edited 4 times in total.

User avatar
rust collector
Motörhead
Posts: 535
Joined: Mon Jan 13, 2014 3:56 pm
Location: no_nb

Re: experimental replacement for ceni

Unread post by rust collector » Mon Jun 15, 2015 5:36 pm

Well, what can I say?
It seems to work fine (for me, in this case)

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: experimental replacement for ceni

Unread post by wuxmedia » Mon Jun 15, 2015 5:58 pm

^ getting it just for the asskey art :)
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
liknites
Oyster-Slurper
Posts: 26
Joined: Fri Jun 05, 2015 1:31 am
Location: Nouvelle France

Re: experimental replacement for ceni

Unread post by liknites » Mon Jun 15, 2015 6:21 pm

Thanks & thanks!

anything to add? perhaps systemd, qt, gtk, and texlive dependencies? B)

User avatar
rust collector
Motörhead
Posts: 535
Joined: Mon Jan 13, 2014 3:56 pm
Location: no_nb

Re: experimental replacement for ceni

Unread post by rust collector » Mon Jun 15, 2015 6:28 pm

SOunds good, atleast qt, and gtk3, and you might as well pull in network-manager, while you are messing with it.

User avatar
liknites
Oyster-Slurper
Posts: 26
Joined: Fri Jun 05, 2015 1:31 am
Location: Nouvelle France

Re: experimental replacement for ceni

Unread post by liknites » Mon Jun 15, 2015 7:06 pm

So, I did pull in a haskell dependency --- to use shellcheck. Looks like the last post of code was a bit messy, instead of putting up a wall of text again I'm just going to edit that post with the correction.

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: experimental replacement for ceni

Unread post by wuxmedia » Mon Jun 15, 2015 7:30 pm

shell check written in haskell : mindblown
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: experimental replacement for ceni

Unread post by wuxmedia » Mon Jun 15, 2015 7:34 pm

alright, i'll bite, I guess pidof is a bashism?
"Seek, and Ye shall find"
"Github | Chooons | Site"

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: experimental replacement for ceni

Unread post by machinebacon » Mon Jun 15, 2015 7:42 pm

Bit late to the party but hopefully not too late - don't know if the code above is the recent version, anyway:

would it be possible to add
stty -echo
before reading the password, and then turning echo on again with
stty echo

Thanks ;)
..gnutella..

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: experimental replacement for ceni

Unread post by rhowaldt » Mon Jun 15, 2015 7:45 pm

^^ iirc, yes :)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: experimental replacement for ceni

Unread post by rhowaldt » Mon Jun 15, 2015 7:54 pm

i get an error that iwlist isn't found. it is in the wireless-tools package, so note that as dependency i suppose?
agreed on bacon's password-hiding-suggestion.
suggestion: add numbers to the list of available networks, so that you do not have to type in the entire name, and possibly typo.
suggestion: is it possible to sort the networks by strongest signal? it seemed strange to me that the network i was looking for was at the bottom instead of the top.

it didn't work for me: at the end, it was waiting a long time (45 seconds or so?), then gave me back my prompt but the internet didn't work. i dunno why, because there was no feedback - maybe add that?

edit: log.

Code: Select all

┌┤rhowaldt@lalala [~/scripts]
└──┤$ sudo ./bbqwifi 

#    #               # ## # 
#    #                 #    
#### #### #### # # # # ## # 
#  # #  # #  # # # # # #  # 
#### #### #### ##### # #  # 
             #              
             #              
........................... 

Available devices:

lo
eth0
wlan0

Interface: wlan0

Scanning for networks...

2doctorsathome
Appeltje Eitje
De buren
goedman 1
H220N134BC8
H369A8DCDFD
Koedief
KPN-VGV7519C70939
KPN-VGV7519DC9C61
si to ty?
tele2-1
Tele2-2
trudette's Wi-Fi Network
Ziggo
Ziggo26444
Ziggo29342
Ziggo38512
Ziggo63041
Ziggo81384

Network: Ziggo81384

Password: 123456

Connecting to the network...


┌┤rhowaldt@lalala [~/scripts]
└──┤$ sudo ifdown wlan0
Removed stale PID file
Internet Systems Consortium DHCP Client 4.3.2
Copyright 2004-2015 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/00:15:af:31:6a:fa
Sending on   LPF/wlan0/00:15:af:31:6a:fa
Sending on   Socket/fallback
DHCPRELEASE on wlan0 to 192.168.178.1 port 67

┌┤rhowaldt@lalala [~/scripts]
└──┤$ sudo ifup wlan0
Internet Systems Consortium DHCP Client 4.3.2
Copyright 2004-2015 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/00:15:af:31:6a:fa
(etc)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
liknites
Oyster-Slurper
Posts: 26
Joined: Fri Jun 05, 2015 1:31 am
Location: Nouvelle France

Re: experimental replacement for ceni

Unread post by liknites » Mon Jun 15, 2015 7:56 pm

updated

@wux: I checked it out, but it seems fine. I couldn't find a real difference in using pidof or pgrep. Thoughts?

@machinebacon: added & updated

@rho: thanks for the feedback. I'll try the numbers and sorting by signal strength. I'll look for a way around iwlist too.

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: experimental replacement for ceni

Unread post by wuxmedia » Mon Jun 15, 2015 9:00 pm

^ not really just jealous of the ps | awkage :)
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
liknites
Oyster-Slurper
Posts: 26
Joined: Fri Jun 05, 2015 1:31 am
Location: Nouvelle France

Re: experimental replacement for ceni

Unread post by liknites » Mon Jun 15, 2015 9:13 pm

^ it's amazing how ignorance can fuel unnessecary, inelegant design :)))

there's nothing like wrangling

Code: Select all

ps -A | grep -E "whatever" | bloat
and then realizing oh, there's pgrep... 0_o
Last edited by liknites on Mon Jun 15, 2015 9:40 pm, edited 2 times in total.

Post Reply