Make your conky automatically switch network devices
  • famewolffamewolf
    PMPosts: 62
    **THIS IS A WORK IN PROGRESS and not yet 100%.   These scripts run as root so they are not going to know who the active user is.   I'm looking into what environment variable will tell me the id of the current user.  Right now it's just here for discussion. Currently you'd have to hardcode the username so if you have multiple users it won't handle it.**


    I'm sure there are better ways to do this..in fact with more time the 4 scripts could be combined into one using a case statement but I went with quick and dirty.  In a nutshell when you change the network adapter you are using all the scripts in /etc/network/if-up.d get ran.  By checking to make sure the interface we want came up we can do global find/replacements of the network adapter to what we want.  This requires you to create 4 files in /etc/network/if-up.d named eth0, eth1, wlan0 and wlan1.  You can skip the scripts for adaptors you don't have if you wish.  I just covered the majority of them.  Once these are saved when you change from wlan0 to eth0 or vice versa the appropriate script should get ran...it does replacements in the .conkyrc file and conky auto reloads when it notices the file has changed.  An easy way to see if this script is working is to have the .conkyrc display "eth0" or "wlan0" as text so you can watch as they change. 

    Save the following as /etc/network/if-up.d/eth0
    #######################################
    #!/bin/sh
    #

    # Exit if we're not starting "eth0".
    [ "$IFACE" = 'eth0' ] || exit 0

    sed -i 's/wlan0/eth0/g' /home/<user>/.conkyrc
    sed -i 's/wlan1/eth0/g' /home/<user>/.conkyrc
    sed -i 's/eth1/eth0/g' /home/<user>/.conkyrc
    #######################################


    Save the following as /etc/network/if-up.d/eth1
    #!/bin/sh
    #

    #######################################
    # Exit if we're not starting "eth1".
    [ "$IFACE" = 'eth1' ] || exit 0

    sed -i 's/wlan0/eth1/g' /home/<user>/.conkyrc
    sed -i 's/wlan1/eth1/g' /home/<user>/.conkyrc
    sed -i 's/eth0/eth1/g' /home/<user>/.conkyrc
    #######################################


    Save the following as /etc/network/if-up.d/wlan0
    #!/bin/sh
    #

    #######################################
    # Exit if we're not starting "wlan0".
    [ "$IFACE" = 'wlan0' ] || exit 0

    sed -i 's/eth0/wlan0/g' /home/<user>/.conkyrc
    sed -i 's/wlan1/wlan0/g' /home/<user>/.conkyrc
    sed -i 's/eth1/wlan0/g' /home/<user>/.conkyrc
    #######################################


    Save the following as /etc/network/if-up.d/wlan1
    #!/bin/sh
    #

    #######################################
    # Exit if we're not starting "wlan1".
    [ "$IFACE" = 'wlan1' ] || exit 0

    sed -i 's/wlan0/wlan1/g' /home/<user>/.conkyrc
    sed -i 's/eth0/wlan1/g' /home/<user>/.conkyrc
    sed -i 's/eth1/wlan1/g' /home/<user>/.conkyrc
    #######################################
  • mfrazzzmfrazzz
    PMPosts: 18
    How about this (just one file):

    #!/bin/sh
    #
    # change myusername to be the username you login with
    user="myusername"
    cd /home/$user

    # Get the current network adapter in .conkyrc
    conkynet=`awk '/upspeed/ { print $4 }' .conkyrc`;conkynet=${conkynet%?}

    sed -i 's/$conkynet/$IFACE/g' .conkyrc

    Thanked by 2lxle Zaka
  • lxlelxle
    PMPosts: 2,656
    Excellent mfrazzz, are you able to configure it to determine the users name?
  • famewolffamewolf
    PMPosts: 62
    Either version as-is would work for the live dvd since the user qwerty is known.  Looping through the names in /home isn't gonna do the trick since if they have their home directory encrypted the conkyrc won't be available but apparently variable $USER (echo $USER to see) ($user in lowercase didn't work for me) contains the name of the current logged in person so.....building on mfrazzz's version....

    #!/bin/sh
    #
    cd /home/$USER

    # Get the current network adapter in .conkyrc
    conkynet=`awk '/upspeed/ { print $4 }' .conkyrc`;conkynet=${conkynet%?}

    sed -i 's/$conkynet/$IFACE/g' .conkyrc


    You could also execute the "logname" command to get current user so putting "user=`logname`" in mfrazzz's at beginning would do the trick.
    Mfrazzz, you are however assuming the user has the default .conkyrc included in lxle whereas the version I posted would work with ANY .conkyrc including customized ones since eth0, wlan0 etc will always be present.  To make my original version work correctly just change "<user>" to "$USER".

    ***One final issue remains....since if-up.d is triggered at the time the interface comes up...upon bootup doesn't the interface come up long before the user actually logs in?  If so then perhaps Mfrazzz's hardcoded version is the best method but again won't work with encrypted home directories (I don't think)***
    Thanked by 1Zaka
  • mfrazzzmfrazzz
    PMPosts: 18
    Correct, I'm making the assumption that upspeed is going to be there for the awk search to work.  I meant to put that in my post.

    And correct that the if-up.d is going to be triggered before a user logs in, thus I'm doing the hardcoding of the username to use.  I guess you could cycle through /home and update any .conckyrc you find for those users (only interactive ones would probably have the file). 
  • famewolffamewolf
    PMPosts: 62
    Perhaps $USER or executing logname could be used as a check so that if it's being ran on initial bootup it sleeps until login has occured....I'm sure there is something that can be checked...possibly whether $USER is blank.  I mention this because this would get around any issues with encrypted home's.  Or thinking further it could be as simple as checking if conky is running [see code below]....the other question is where does the lxde network monitor store the interface name it monitors?  If so it could also be updated to provide realtime updates.

    #!/bin/sh
    run=`ps ax | grep /usr/bin/conky | grep -v grep `
    if [ "$run" ];
    then
    echo "Conky is running"
    else
    echo "Conky is not running"
    fi


    Answering my own question the lxde network monitor stores the interface name in ~/.config/lxpanel/Lubuntu/panels   The "Lubuntu" part will change depending on which paradigm is active.  The search string would be "iface=wlan0"  [The default is eth0 but the same search/sed mfrazzz used could be adapted......]