I bought a Raspberry Pi 4. So, I put Arch Linux in the SD card.
I didn't buy the LED separately this time, but it will be operated in its original state, but there is one problem,
** It is troublesome to check where you are connected **
Basically, DHCP is used and there is no monitor, so it is troublesome to go to check the IP address regardless of whether it is a wired connection or a wireless connection. So, this time, as the title suggests, I decided to "get the connection information from myself when the network connection is established".
py:/etc/NetworkManager/dispatcher.d/notify-connection-information.py
#!/usr/bin/env python
import json
import os
import sys
from urllib.request import urlopen
WEBHOOK_URL = 'https://hooks.slack.com/services/THISIS/SLACK/WEBHOOK'
def main(conn_id, ip_addr):
payload = {
'username': 'info',
'icon_emoji': ':strawberry:',
'text': f'*attakei-pi* has connected to {conn_id}\n IP address is `{ip_addr}`',
'channel': '@attakei',
}
urlopen(WEBHOOK_URL, json.dumps(payload).encode())
if __name__ == '__main__':
argv = sys.argv[1:]
# if argv[0] != 'wlan0' or argv[1] != 'up':
if argv[1] != 'up':
sys.exit(0)
ip_addr = os.environ['DHCP4_IP_ADDRESS']
conn_id = os.environ['CONNECTION_ID']
main(conn_id, ip_addr)
It is a daemon (+ tool) for managing various things related to network connection. It manages the SSID and automatically connects to the already registered SSID.
There are also systemd-networkd
etc., but for the time being I am currently using this.
Now, NetworkManager has a dispatcher
feature that allows you to" execute a command to divide a network as an event. "
In the sample etc. posted on the ArchLinux Wiki, examples such as "Disable wireless when making a wired connection (+ vice versa)" and "Continue to VPN connection when connecting to a specific network" are written. I am.
This time, referring to these scripts, I made the above script "Declare the used SSID and ʻIP address
to the specified Slack channel when connecting to the network".
As far as I know by actually using it, Disptcher will pass the following information when executing the command.
Based on the conditions you want to trigger, organize where to get information and behavior.
Used SSID
"CONNECTION_ID
DHCP4_IP_ADDRESS
.Now that you have the information you need, you can write the code. Please refer to the code as it was written first.
Don't forget to shebang and grant execute permissions as the script will be called directly.
URLs
Recommended Posts