maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   General (https://talk.maemo.org/forumdisplay.php?f=7)
-   -   Hook when connecting to a wifi network? (https://talk.maemo.org/showthread.php?t=46466)

tremby 2010-03-04 11:57

Hook when connecting to a wifi network?
 
I'd like to be able to have a script run automatically when I connect to a wifi network.

It's the main university one and it's one of those which wants me to give my username and password in a web page before I'm allowed proper access to the internet.

The script would be easy -- it'd check which wifi network we just connected to (by SSID, for instance) and then if it matched the uni network it'd just be a simple curl job to send my username and password.

(Come to think of it I haven't even checked if curl's available -- I can work around that if not.)

Does something like this exist? I'm not new to Linux, I'm happy editing system configs and scripts etc.

This is on my new N900.

Matan 2010-03-04 12:10

Re: Hook when connecting to a wifi network?
 
dbus-scripts allows that. Install this package, and create a file called /etc/dbus-scripts.d/wlan containing

/usr/local/bin/myscript * * com.nokia.icd status_changed * WLAN_INFRA CONNECTED

And the script will run whenever you connect to a WLAN (infrastructure mode only not for ad-hoc connections).

You can replace the third star with connection id of your university connection, so the script will only run when connected to university network, not to any network.

There is a GUI for dbus-scripts, if you don't want to create the file by yourself, which is called dbus-scripts-settings.

Both packages are available in extras-devel and in my repository (see my signature).

curl is available in SDK repository, but there is also wget in extras.

RobbH 2010-03-04 12:21

Re: Hook when connecting to a wifi network?
 
You'll find some discussion of this in this thread: http://talk.maemo.org/showthread.php?t=43028 and still more if you follow the link in the first post.

Please share any solution you come up with!

gidoca 2010-03-04 12:21

Re: Hook when connecting to a wifi network?
 
Quote:

Originally Posted by tremby (Post 555354)
I'd like to be able to have a script run automatically when I connect to a wifi network.

It's the main university one and it's one of those which wants me to give my username and password in a web page before I'm allowed proper access to the internet.

The script would be easy -- it'd check which wifi network we just connected to (by SSID, for instance) and then if it matched the uni network it'd just be a simple curl job to send my username and password.

(Come to think of it I haven't even checked if curl's available -- I can work around that if not.)

Does something like this exist? I'm not new to Linux, I'm happy editing system configs and scripts etc.

This is on my new N900.

If you want to automatically visit web pages, fill out forms, etc. you may also want to have a look at python-mechanize (it's in extras-devel). That'd be ways easier than using curl IMO.

Edit: Aditionally to Matan's solution, you could maybe also use /etc/network/if-up.d. It seems like scripts in there are executed when connecting to a network. However, i haven't tried it myself.

v13 2010-03-04 12:28

Re: Hook when connecting to a wifi network?
 
Quote:

Originally Posted by tremby (Post 555354)
I'd like to be able to have a script run automatically when I connect to a wifi network.

[...]

The script would be easy -- it'd check which wifi network we just connected to (by SSID, for instance) and then if it matched the uni network it'd just be a simple curl job to send my username and password.

You can run such scripts by putting them in /etc/network/if-*.d. What you want is /etc/network/if-up.d. Most probably you'll have to determine the ssid yourself (e.g. with iwconfig) but that will be easy.

You can find documentation in interfaces(5) man page. Look at "IFACE OPTIONS".

tremby 2010-03-04 16:57

Re: Hook when connecting to a wifi network?
 
I have it working.

I couldn't find iwconfig (though I didn't look very hard) and so found no way right away of getting the SSID.

So I took the dbus-scripts approach. I was confused at first that the script wasn't called and tried restarting dbus and dbus-scripts. Restarting dbus gave me an error -- it was calling sort with --reverse which doesn't exist on this device. I replaced the --reverse with -r (beware, --reverse appears twice and only one of them is to do with sort) and then dbus restarted properly. I don't know if this was a necessary change.

But then I lost all wifi. I double checked my scripts and they were fine. I rebooted the device and then everything was working.

I had the script append all arguments to a file at first so I could check what was what and get the network ID I needed. Then I commented that line out and wrote the rest of the script. (I left the line in since it may be helpful to others.)

The scripts I'm left with:

/etc/dbus-scripts/wlan
Code:

/home/user/isslogin.sh * * com.nokia.icd status_changed * WLAN_INFRA
/home/user/isslogin.sh
Code:

#!/bin/bash
USER='--my university username--'
PASS='--my university password--'
IP=$(ifconfig wlan0 | grep "inet addr" | cut -d : -f 2 | awk '{print $1}')

#echo $* >>/home/user/dbus

if [ $5 = "d49d3--my university network ID--3e410" -a $7 = "CONNECTED" ]; then
        wget --no-check-certificate --post-data '_FORM_SUBMIT=1&which_form=reg&destination=http%3A%2F%2Fgoogle.com%2F&source='$IP'&error=&bs_name='$USER'&bs_password='$PASS https://--network login URL--.pl -O /dev/null
fi

Obviously the form data and URL will have to be different for different networks.

I generated mine by connecting to the network and then, before logging in, doing wget http://google.com and then inspecting the HTML returned.

Matan 2010-03-04 17:06

Re: Hook when connecting to a wifi network?
 
If your script is only does something for one connection, then the test is better done in dbus-scripts then in your script. Use the following line:

/home/user/isslogin.sh * * com.nokia.icd status_changed d49d3--my university network ID--3e410 WLAN_INFRA CONNECTED

And the script will only be called for this connection, so you don't need the if statement in your script.

dbus-scripts needs to be restarted after changing configuration - the easiest way is to run (as root) killall dbus-scripts . It is then restarted automatically by upstart.

tremby 2010-03-04 17:07

Re: Hook when connecting to a wifi network?
 
Yeah, I realized I could put the logic in the dbus script -- I just thought I'd leave myself flexibility in case I wanted to do other things for other networks or events. Thanks for your help.

afrinc 2010-07-02 15:25

Re: Hook when connecting to a wifi network?
 
I was doing some thing very similar and someone suggest me to don't use "--no-check-certificate".

So you can try with : "--ca-directory=/etc/certs/common-ca/" instead.

In my case, I was not using dbus but an /etc/if-up.d/ script.

Regards,

matthewbpt 2011-05-03 15:53

Re: Hook when connecting to a wifi network?
 
What do I need to put in to the dbus-script to excute a script on gprs connect and disconnect?

EDIT: Figured it out, instead of:
Code:

/some/script.sh * * com.nokia.icd status_changed * WLAN_INFRA
it's
Code:

/some/script.sh * * com.nokia.icd status_changed * GPRS


All times are GMT. The time now is 16:18.

vBulletin® Version 3.8.8