DEV Community

loading...

Automating some annoying clicks

Christian Bewernitz
πŸ³β€πŸŒˆ white non-trans* he/him, born GDR/Europe, father of two sons, married
・2 min read

I just automated away something that annoyed me:

Whenever I'm in some foreign WIFI (e.g. in the train) I need to find the right URL to open in the browser to confirm some dialog or login before having access to the internet.

I recently found out that I can just enter the IP-address of the primary DNS server from my network information into the browser as the most reliable way.

But that's still to many clicks for doing it multiple times a day (e.g. when switching between trains), so I composed a few bash commands to do that for me:

xdg-open "http://$(grep "nameserver" /etc/resolv.conf | tr  -d "nammeserver ")" > /dev/null &
Enter fullscreen mode Exit fullscreen mode

Let's see what's happening here from inside out:

$ grep "nameserver" /etc/resolv.conf
nameserver 192.168.8.1
Enter fullscreen mode Exit fullscreen mode

delivers the current primary DNS,

... | tr -d "nameserver "
Enter fullscreen mode Exit fullscreen mode

drops the first word and space from the previos command so that only the IP address is left,

"http://$(...)"
Enter fullscreen mode Exit fullscreen mode

prefixes the output from above commands (executed in a sub-shell) to make it a URL

xdg-open "..." > /dev/null &
Enter fullscreen mode Exit fullscreen mode

opens the default browser (because the string is a URL),

  • > /dev/null: without writing some message like "Opening in existing browser session." to the terminal
  • &: in the background (without blocking the shell)

I added the whole thing to a new wifi alias in my ~/.extend.bashrc:

alias wifi='xdg-open "http://$(grep "nameserver" /etc/resolv.conf | tr  -d "nammeserver ")" > /dev/null &'
Enter fullscreen mode Exit fullscreen mode

so it's always on my fingertips :)

PS: It's not working everywhere (e.g. not in the German ICE trains, thanks Deutsche Bahn)
and the next hit of Ctrl+C adds the output

[n]   Done     ...the command...
Enter fullscreen mode Exit fullscreen mode

So I'm very open for suggestion of how to improve it.

Discussion (0)