Page 1 of 1

[SOLVED] How to connect to a wifi network by shell script?

Posted: 11 Jun 2023, 11:24
by hope
Hi to all.
I want to add a new network(wifi network by its configurations, connection name, ssid, passwd etc.) by using shell script to my Porteus, I am using Porteus in usb stick therefore I do not want to enter all configurations all time by hand again and again. Is is possible to add a new network by using shell scripting? What I achieve is that, I wrote all config in a file and shell script read all those and add new wifi network connection to Porteus and I can only click "connect" and connect to my wireless connection.

I can handle coding part, I only want to know and guide about :
1. is is possible and,
2. where do I have to look for (i.e. man page of which commands ? what is the shell command behind network clients? etc.

Thanks.
[edited my question and add more info about what I want to achieve.]

How to connect to a network by shell script?

Posted: 11 Jun 2023, 13:07
by hope
I think I found all commands I need in
https://developer-old.gnome.org/Network ... nmcli.html.

example :

Code: Select all

nmcli dev wifi con "Cafe Hotspot 1" password caffeine name "My cafe" 
given in the page will solve my problem.

[SOLVED] How to connect to a wifi network by shell script?

Posted: 11 Jun 2023, 20:29
by Ed_P
A very impressive find hope. Hopefully you will post a copy of your script when you're done. (obviously with tweaked passwords)

[SOLVED] How to connect to a wifi network by shell script?

Posted: 11 Jun 2023, 20:43
by hope
Ed_P wrote:
11 Jun 2023, 20:29
A very impressive find hope. Hopefully you will post a copy of your script when you're done. (obviously with tweaked passwords)
Thanks Ed_P, actually at first, my script contained only one command line like :

Code: Select all

#!/bin/sh
nmcli dev wifi connect "ssid-of-wifi" password 123 name wifi1 hidden yes
Then, after finding that Porteus is using NetworkManager, I searched more and I found in that https://stackoverflow.com/questions/354 ... sing-nmcli. It helps me to get more scriptable commands(Pocokman's answer), and I improved my script as :

Code: Select all

#!/bin/sh
nmcli c add type wifi con-name wifi1 ifname wlan0 ssid "ssid-of-wifi"
nmcli con modify wifi1 wifi-sec.key-mgmt wpa-psk
nmcli con modify wifi1 wifi-sec.psk 123
nmcli con up wifi1
:)