Programmatically connecting to a wifi network in Linux - linux

There are multiple questions regarding this functionality for Android or iOS, but I am attempting to do this on Linux (OpenWrt 15.05 to be specific). I want to scan for wireless networks, present the list to the user, have them select one and send the username/password to the network.
I wrote a quick C program using iw_scan from the wireless_tools package so I can see the wireless networks, but I am having issues trying to determine how to connect to a wireless network. iwconfig from the same package just appears to modify an already existing connection.
I have spent a few hours surfing the web thinking there has got to be a package that already does this since it is such a common use case. Anyone know of one?
Thanks.

wpa_supplicant
Take a look at wpa_supplicant package. It includes wpa_supplicant daemon that can be controlled via wpa_cli and wpa_gui utilities or D-Bus API.
Programs like wicd and Network Manager uses wpa_supplicant internally.
NetworkManager
You can also use D-Bus API or command line interface (nmcli) for NetworkManager. It provides a higher level abstraction than wpa_supplicant does.

I've tried number of solutions to this, from wicd, to network manager's cli interface nmcli, to wifi in python. I didn't want a UI, I didn't want network manager, and the python wifi project was too complicated and I faced some problems with it. So I wrote a short, simple bash script for connecting to wpa and open networks (WEP not supported), a wrapper on wpa_passphrase and wpa_supplicant:
sudo apt-get install -y wireless-tools wpasupplicant expect macchanger
sudo wget -O /usr/local/bin/wifi-connect https://raw.githubusercontent.com/erjoalgo/erjoalgo-gnu-scripts/master/wifi-connect
sudo chmod +x /usr/local/bin/wifi-connect
wifi-connect -e Internet -p my-password
wifi-connect -h
The wpa-passphrase files (including plaintext passphrases) are persisted in $HOME/.config/wifi-connect, and if no ESSID is provided, it will automatically connect to any known available network. It supports changing the iface mac address via macchanger.

Related

socketcan alternative / add can0 to /dev/

I am currently working with CAN-BUS Systems.
I make the CAN Interface available using
sudo modprobe can
sudo modprobe can-raw
sudo modprobe mttcan
sudo ip link set can0 type can bitrate 250000
sudo ip link set up can0
It now shows up in ifconfig and works fine in my python code.
Since I am working with aws greengrass I need the device to show up in /dev/ in order to access it. Is there any way to use something different than socketcan? If so, how do I use it?
I have no idea how linux handles can interfaces.

XRUSB raspberry pi

I'm working with a raspberry pi connected with xrusb to a controller using python.I use make file to Compile and install the common usb serial driver module and it works fine. After reboot i have problem. The driver is lost. I have to install the module again using this
modprobe usbserial
insmod ./xr_usb_serial_common.ko
Any idea?
Now my answer might be off because of the way you say "install the driver". I bet the make script most likely just loaded the driver just like you did via modprobe.
In order to get the module to be loaded at boot time, you need to tell udev what to load/do during bootup. And tell the kernel to load your driver.. Otherwise it assumes you don't want it to be loaded at boot time.
Either you can do a automatic module handling via:
#nano /etc/modules-load.d/usbserial.conf
usbserial
or, you can specify options:
#nano /etc/modprobe.d/usbserial.conf
options usbserial parameter_name=parameter_value
Here's some documentation on how this works:
http://man7.org/linux/man-pages/man5/modules-load.d.5.html
https://wiki.archlinux.org/index.php/kernel_modules
(Even if you're not running Arch on your RPi, they still have one of the best documentation websites for Linux out there. User friendly, in depth etc. So apply the information there to your Distribution, they should be very much the same this day and age)
I found maybe a temporary solution so i can finish my project and look for the best way later.
I make a script to run after reboot to load the driver.
use:
sudo crontab -e
then go to the bottom and write
#reboot bash /your/path/script/script.sh
`

How to enable SSH on BusyBox without recompiling BusyBox?

I want to enable SSH service on BusyBox server (currently I can connect only via Telnet), so I think , that I need to compile SSH server , and send it to BusyBox router. So my question is - how to create SSH server binary to send it to BusyBox via FTP?
You're taking a wrong direction.
1/ "How to enable SSH on BusyBox."
You can't.
2/ And "without recompiling BusyBox?"
You won't, because #1.
Because busybox in itself does not provide an SSH server nor client.
Now that your question is answered:
The usual way to add an SSH server to a busybox based system is to add dropbear, first on the list of "tiny utilities whose functionality is not provided by busybox":
https://busybox.net/tinyutils.html
https://matt.ucc.asn.au/dropbear/dropbear.html
How to compile dropbear (because you will have to), then install and run on your system is now another topic, on which no one can help you without further details on your target system or your cross toolchain.

GATT services using bluez aren`t be added?

My problem :
I try to run the gatt-example server on the bluez
I make:
1- sudo hciconfig hci0 leadv
2- sudo ./bluetoothd -d -n
3- I connect with a master and try to discover all primary services but every time I find 2 GAP services only why ??
I hope to find a person who can help me to run any profile and find all the services and attributes from the central device.
You need to recompile bluez with --enable-experimental options and the run it with -E flag.
I have a small description here: https://github.com/jomarmar/bluez-experimental
which maybe of help.(Just for your info, I use Archlinux)

How to install Wireshark on Linux and capture USB traffic?

I have encountered numerous problems in the installation of Wireshark, and the capture of USB traffic, especially due to user permissions.
How to install Wireshark on Linux and capture USB traffic?
Tested on Ubuntu 14.04, but probably works on other distributions since none of the steps are specific to Ubuntu.
The first time you follow the tutorial, do all the steps 1 -> 7.
When you restart your computer, you have to repeat steps 6 and 7 to see the USB interfaces in Wireshark.
Install Wireshark and libpcap:
sudo apt-get install wireshark libpcap0.8
For Debian, Ubuntu and other Debian derivatives, continue to step 3.
For other Linux based systems or other installation methods, see the Wireshark Wiki, then go to step 6.
Reconfigure wireshark to allow non-superusers to track packets:
sudo dpkg-reconfigure wireshark-common
Select <Yes> in the prompt
Add your username to the "wireshark" usergroup:
sudo usermod -a -G wireshark <your_username>
You can verify if it’s done correctly by displaying the groups your username is part of:
groups <your_username>
If not, you can add the group "wireshark" manually:
groupadd wireshark
And then add your username to the group (see above)
Important: Logout of your session, then log back in.
This step depends on the kernel version that is installed on your machine. To know the version of your kernel, type:
uname -r
For versions of the kernel prior to 2.6.21, if debugfs is not already mounted on /sys/kernel/debug, ensure that it is mounted there by issuing the following command:
sudo mount -t debugfs / /sys/kernel/debug
For kernel version 2.6.21 and later, load the loadable module usbmon in the Kernel:
`sudo modprobe usbmon`
See [Wireshark Wiki](https://wiki.wireshark.org/CaptureSetup/USB#Linux) for more information about this differentiation.
If the usbmon interfaces don't appear in Wireshark, look for interfaces using dumpcap (the command-line tool of Wireshark):
sudo dumpcap -D
You should see the usbmon* interfaces. Now display the permissions of the usbmon interfaces:
ls -l /dev/usbmon*
If the usbmon* files have 'crw-------', then it's normal that Wireshark cannot read them because it's not run as root. Do not execute wireshark in root mode, it may damage files. Instead, you can give it regular users privileges :
sudo setfacl -m u:$USER:r /dev/usbmon*
Now the usbmon interfaces should appear in Wireshark.
Sources:
https://wiki.wireshark.org/CaptureSetup/USB#Linux
https://wiki.wireshark.org/CaptureSetup/CapturePrivileges#Most_UNIXes
https://unix.stackexchange.com/questions/55722/wireshark-couldnt-run-usr-sbin-dumpcap-in-child-process
http://anonscm.debian.org/viewvc/collab-maint/ext-maint/wireshark/trunk/debian/README.Debian?view=markup

Resources