LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-15-2019, 05:22 PM   #1
justme48
LQ Newbie
 
Registered: Apr 2018
Location: London
Distribution: CentOS 7
Posts: 7

Rep: Reputation: Disabled
Conky and changing the network interface(for whatever it may be).


Hi there,

I'm hoping you can help. I have a conky.conf script which has the network adaptors ie: ${addr eth0 }. I can change the eth0 to whatever I want using sed -e s/eth0/newinterface_here/g oldfile > newfile.

But I want to know how to read whatever the active interface is, this could be for any machine and one of the multiple names. ie: eno1,eth0,em1,ens1, enp2so etc(you get the idea). I only want the active port and not any wireless etc.
Something like: ip addr | awk '/state UP/ {print $2}' | sed 's/.$//'

And then pass the result to 'newinterface_here'

I've tried to write a script but the best I'm getting is that eth0 is removed from the script and not adding eg:'em1'.

Can you help please?

I know I can write multiple adaptors and select the active one but I don't want this. When the script is run I want it to do: 1) This is the adaptor 2)Replace the text eth0 with (1) 3) Write the new file correct for this machine.
 
Old 03-15-2019, 05:54 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 current / Linux Mint / Debian / Void Linux
Posts: 8,140

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
so you need a script that can find out what the active device is on any box, cat5 not wifi, then take that and add it to your conky file. I only have wifi but you need to know the command to find the information, then look at its output to figure out where to get what you're looking for

Code:
#output
2: wlo1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether 00:24:d7:cf:9e:a4 brd ff:ff:ff:ff:ff:ff

#gets my wifi device name holds it in a var. 
device="$(ip link | awk '/2:/{print $2}' | rev | cut -c 2- | rev)"
#use double quotes
 sed -i "s|eth0|$device|" dave
Code:
#putting it together

device="$(ip link | awk '/2:/{print $2}' | rev | cut -c 2- | rev)" | sed -i "s|eth0|$device|" dave

#before
$ cat dave
obodfdf

eth0  fgds
bogdg
sds

#after

$ cat dave
obodfdf

wlo1  fgds
bogdg
sds
putting it all in one line
Code:
sed -i "s|eth0|$(ip link | awk '/2:/{print $2}' | rev | cut -c 2- | rev)|" dave

with me using ip link, and UP for the check, as there is two up's in the output
Code:
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: wlo1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether 00:24:d7:cf:9e:a4 brd ff:ff:ff:ff:ff:ff

#returns

$ device="$(ip link | awk '/UP/{print $2}' | rev | cut -c 2- | rev)" ; echo $device
lo wlo1
but if you cannot work that out, then post back for further assistant.

Last edited by BW-userx; 03-15-2019 at 06:22 PM.
 
Old 03-15-2019, 06:06 PM   #3
justme48
LQ Newbie
 
Registered: Apr 2018
Location: London
Distribution: CentOS 7
Posts: 7

Original Poster
Rep: Reputation: Disabled
BW - Thanks. Yes the wired network.

ip addr | awk '/state UP/ {print $2}' | sed 's/.$//' - This works to get the name(or the text I need).

What I don't have is the correct script. I keep making a hash of everything and need advice.

As I said I have the sed part just about correct, I can manually make the change(as in I can run the sed part in terminal fine). I just need to know what to put in the script.



Code:
#!/bin/bash
INTERFACE="ip addr | awk '/state UP/ {print $2}' | sed 's/.$//'" && sed -e s/eth0/$INTERFACE/g oldfile > newfile

Last edited by justme48; 03-15-2019 at 06:12 PM.
 
Old 03-15-2019, 06:29 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 current / Linux Mint / Debian / Void Linux
Posts: 8,140

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
Quote:
Originally Posted by justme48 View Post
BW - Thanks. Yes the wired network.

ip addr | awk '/state UP/ {print $2}' | sed 's/.$//' - This works to get the name(or the text I need).

What I don't have is the correct script. I keep making a hash of everything and need advice.

As I said I have the sed part just about correct, I can manually make the change(as in I can run the sed part in terminal fine). I just need to know what to put in the script.



Code:
#!/bin/bash
INTERFACE="ip addr | awk '/state UP/ {print $2}' | sed 's/.$//'" && sed -e s/eth0/$INTERFACE/g oldfile > newfile
what works on the command line. Just put it into a script. that simple.
did you chck your put put of awk ?

Code:
$ INTERFACE="$(ip addr | awk '/state UP/ {print $2}')" ; echo $INTERFACE
wlo1:
the : colon will get in the way yes?
try this
Code:
sed -i "s|eth0|$(ip add | awk '/state UP/{print $2}' | rev | cut -c 2- | rev)|" test.conf
put that into a file then
chmod +x filename
then
Code:
./filename
then open your test file to see if you're getting the desired results.

this does not need to be a bash script, the file just needs to be executable.
the rev reverse the text gotten, then cut removes the unwanted part the - hyphen removes everything past the start 2 .. you might have to adjust the 2 to whatever works for you. that is if you get more than just the eth0, eth1 etc..

the put it back forwards again by using another rev

Last edited by BW-userx; 03-15-2019 at 06:55 PM.
 
1 members found this post helpful.
Old 03-16-2019, 06:20 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 11,054
Blog Entries: 7

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
conky has a
Code:
$gw_iface
variable that will give you the currently active interface.
then it has various
Code:
$if_...
constructs, particularly the
Code:
$if_up
construct.
among these options it's possible to implement what you want straight from conky without any external script.

https://github.com/brndnmtthws/conky/wiki
 
1 members found this post helpful.
Old 03-17-2019, 11:11 AM   #6
justme48
LQ Newbie
 
Registered: Apr 2018
Location: London
Distribution: CentOS 7
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks guys for the help. Missed that conky variable.
 
Old 03-17-2019, 02:06 PM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 11,054
Blog Entries: 7

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
aren't you going to show us your solution?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Screen dim time is very little. Whenever I pause to read whatever is on my screen or whatever the screen dims after about 3-5 seconds Ajwad Linux - Newbie 26 02-21-2016 01:45 PM
[SOLVED] confused about when to use cd ~/whatever or cd /whatever l0r3n Linux - Newbie 4 10-20-2015 10:37 PM
conky and conky-manager Tonus Slackware 4 09-03-2015 08:02 AM
LXer: Configure conky-Lua in Ubuntu (11.10 & 12.04), Fedora, debian and LinuxMint | Howto Conky LXer Syndicated Linux News 0 03-06-2012 12:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration