Simple stuff in bash but would like an extension instead

I’m trying to port a bash script to a gnome extension and have very very limited knowledge about javascript and GNOME extensions. I’ve manage to display an icon at least… and put some color to it!
Now here is what I would like to do:

  1. Extension ping an IP address (static): if ping ok icon is green, if not ok, icon is red
  2. Click on the icon brings up a menu saying: turn off if icon is green, turn off if icon is red
  3. Clicking on that menu runs that bash script (eventually with a parameter (on or off)

For information the script launches a python script to either turn on a smart plug (on that same network) or properly shut down a rPi and then turns off the plug after 10 seconds.

Thank you for some help on the topic. Web searches didn’t bring enough resources for me to figure that out.

Hi and welcome to the community!

I would first start with these resources:

There are also a number of other guide on gjs.guide that may be helpful.

See the Extension Review Guidelines if you plan on sharing your extension on extensions.gnome.org. Generally we prefer that scripts for extensions be written in GJS, but if Python is necessary these scripts should be short and simple.

Hey! Thank you for the guides. Checking more about pinging and javascript it seems super tricky (if even possible/reliable). You don’t seem to think this is an issue though, probably because of the GJS subprocesses. I may have to rethink that ‘pinging’ part and extract the plug status or some other way.
Thank you.

Just to be clear, JavaScript is “just a langauge”; a collection of variable assignments, a handful of basic types, control structures and so on. In order to do anything useful with JavaScript you a need a platform, which in the case of GJS is the GNOME Platform.

If you’re intent on implementing ping from scratch, you’ll want to reference Gio.Socket, Gio.InetAddress and so forth. I’m not sure it’s worth the effort though…

Exactly! Currently my bash script runs locally (on 2 Fedora PCs) and does this:

  1. checks is there is a ‘on file’ to know if the printer has been turned on
  2. if on, then the rPi is being switch off through ssh, the bash script waits then runs a pip package to turn off the plug
  3. if off, then the bash script turns on the plug through that pip package, creates an ‘on (empty) file’.

Problem is that with 2 PC you don’t know about the printer status and worst case you will just turn a switch on which is already on.

I kind of thought it “wouid be nice” to have a small icon on my top bar to tell me if the printer is on or off and eventually allow me to switch it on or off. This means I cannot use a local ‘on file’.

My thoughts were initially to ping the printer and see if it’s on, but that seems to be a bad idea. I now have the options of either creating that empty file on a network shared server, or querying the plug through that python program and get an on or an off back.

Apparently there are other libraries (requiring node.js) and therefore using different languages. Maybe I should go that way?
I will still need to turn off the Pi (that’s really the main issue) to avoid damaging the SD card over time, and be able to run a SSH command from that extension.

As you can feel (maybe not), I just wanted an extension and the ability to share that printer easily over my network. I thought it would not be that hard or need a rewrite. Was I wrong?

With more specifications, do you have specific recommendations toward the path I should take?
Thank you.

If all you want to do is test whether a network device is reachable, ping is fine for that and not very difficult to do:

const Gio = imports.gi.Gio;

const proc = Gio.Subprocess.new(['ping', '-c', '1', '192.168.1.100'],
    Gio.SubprocessFlags.SILENCE);
    
proc.wait_check_async(null, (proc, res) => {
    try {
        proc.wait_check_finish(res);
        log('Host is online');
    } catch (e) {
        log('Host is offline');
    }
});

As for your other requirements, it’s really up to you what’s best for your use case. I can give you advice on how to do things in extensions and GJS, but not so much what you should do :man_shrugging:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.