Network related bash question

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Network related bash question

Post by Logansfury »

Hello everyone,

I want to see if I can manage to make a conky widget that displays the status of the computers in my network.

I know all my computers device names, local IP addresses, and mac addys. I wanted to make a little graphic widget that lists all the comps by name and presents either a red or green indicator to show if particular comp is online and available. How should I best go about this? Would setting up a script to send like a single ping every 1-5 mins to each comp on the network be realistic? What script could react to the returning data?

I honestly don't know if this is a basic or complicated ask >.<

Thanks for reading,

Logan
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Network related bash question

Post by Koentje »

Conky script networkpc.conky

Code: Select all

conky.config = {
-- **********************************************************************
-- "Network PC"
-- **********************************************************************

	background = false,

	alignment = 'top_left',
	gap_x = 10,
	gap_y = 10,
	minimum_width = 306,
	minimum_height = 20,

	draw_borders = false,
	border_width = 0,
	stippled_borders = 0,
	default_color = 'white',
	default_outline_color = 'white',
	default_shade_color = 'black',
	draw_graph_borders = true,
	draw_outline = false,
	draw_shades = false,
	show_graph_scale = false,
	show_graph_range = false,
	double_buffer = true,
	no_buffers = true,
	out_to_console = false,
	out_to_stderr = false,
	extra_newline = false,
	uppercase = false,
--	use_spacer = 'right',
--	format_human_readable = true,
	pad_percents = no,

	own_window = true,
	own_window_class = 'conky-networkpc',
	own_window_title = 'conky-networkpc',
	own_window_type = 'dock',
	own_window_transparent = true,
	own_window_colour = '000000',
	own_window_argb_visual = true,
	own_window_argb_value = 255,
	own_window_hints = 'undecorated,below,sticky,skip_taskbar,skip_pager',

	use_xft = true,
	xftalpha = 1,
	font = 'Noto Sans:bold:size=9',

	color0 = 'white',
	color1 = '#EAEAEA',
	color2 = '#FFA300',
	color3 = 'grey',

	update_interval = 1,

};
conky.text = [[
#
# Check if network PC is active
${font Noto Sans:bold:size=9}${voffset 0}${offset 1}${color2}NETWORKPC${execpi 15 ./ping-networkpc.sh}
#
#
]];


Bash script networkpc.sh

Code: Select all

#!/bin/bash

# Single ip address or multiple ip addresses.
# Multiple ip addresses seperated by a space
ips=(192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.200 192.168.1.250 192.168.1.251)


for ip in "${ips[@]}"; do

  pvar=$(fping -q -c1 -t100 $ip >/dev/null 2>&1; echo $?)
   if [ "$pvar" = "0" ]; then
    echo "\${goto 110}\${color white}$ip\${goto 220}\${color 66FF00}Online!"
   else
    echo "\${goto 110}\${color white}$ip\${goto 220}\${color FF4422}Offline!"
  fi

done

Schermafdruk van 2024-01-31 23-50-41.jpg
Schermafdruk van 2024-01-31 23-50-41.jpg (8.96 KiB) Viewed 514 times
Image
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Network related bash question

Post by Koentje »

I personally use the graphbar for this. So i can see on/offline for over 10 minutes and also ping in milliseconds.
Bar changes color if ping is low (green), average (grey), high (orange) and offline (red). You can even use domain names.

screenshot.png
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Network related bash question

Post by Logansfury »

HOLY CRAP!!

I just got back from walking the dog, sorry for the delay in response.

This is EXACTLY what I was wanting, a display of my home intranet comps with thier 192 local addys and indicators of on and offline.

I had actually just earlier finished a background image to put green or red indicators by:

Image

Im grabbing all the scripts you have provided here to see how they look on my system

I may put some stuff on the 2nd monitor!
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Network related bash question

Post by Logansfury »

Koentje wrote: Wed Jan 31, 2024 6:50 pm Conky script networkpc.conky

Code: Select all

conky.config = {
-- **********************************************************************
-- "Network PC"
-- **********************************************************************

	background = false,

	alignment = 'top_left',
	gap_x = 10,
	gap_y = 10,
	minimum_width = 306,
	minimum_height = 20,

	draw_borders = false,
	border_width = 0,
	stippled_borders = 0,
	default_color = 'white',
	default_outline_color = 'white',
	default_shade_color = 'black',
	draw_graph_borders = true,
	draw_outline = false,
	draw_shades = false,
	show_graph_scale = false,
	show_graph_range = false,
	double_buffer = true,
	no_buffers = true,
	out_to_console = false,
	out_to_stderr = false,
	extra_newline = false,
	uppercase = false,
--	use_spacer = 'right',
--	format_human_readable = true,
	pad_percents = no,

	own_window = true,
	own_window_class = 'conky-networkpc',
	own_window_title = 'conky-networkpc',
	own_window_type = 'dock',
	own_window_transparent = true,
	own_window_colour = '000000',
	own_window_argb_visual = true,
	own_window_argb_value = 255,
	own_window_hints = 'undecorated,below,sticky,skip_taskbar,skip_pager',

	use_xft = true,
	xftalpha = 1,
	font = 'Noto Sans:bold:size=9',

	color0 = 'white',
	color1 = '#EAEAEA',
	color2 = '#FFA300',
	color3 = 'grey',

	update_interval = 1,

};
conky.text = [[
#
# Check if network PC is active
${font Noto Sans:bold:size=9}${voffset 0}${offset 1}${color2}NETWORKPC${execpi 15 ./ping-networkpc.sh}
#
#
]];


Bash script networkpc.sh

Code: Select all

#!/bin/bash

# Single ip address or multiple ip addresses.
# Multiple ip addresses seperated by a space
ips=(192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.200 192.168.1.250 192.168.1.251)


for ip in "${ips[@]}"; do

  pvar=$(fping -q -c1 -t100 $ip >/dev/null 2>&1; echo $?)
   if [ "$pvar" = "0" ]; then
    echo "\${goto 110}\${color white}$ip\${goto 220}\${color 66FF00}Online!"
   else
    echo "\${goto 110}\${color white}$ip\${goto 220}\${color FF4422}Offline!"
  fi

done


Schermafdruk van 2024-01-31 23-50-41.jpg
I have copied these two files, I have edited in my own intranet addys, separated by spaces, and I have chmod +x on the .sh file. When I fire up the .conky all I see is the word network, none of my IPs are listed.

What must I do to activate this?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Network related bash question

Post by Koentje »

Install fping, i forgot to say! :oops:
sudo apt install fping
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Network related bash question

Post by Logansfury »

Koentje wrote: Wed Jan 31, 2024 8:27 pm Install fping, i forgot to say! :oops:
sudo apt install fping
WOOT more stuff :D

I just did the install and the terminal bears out that I didn't have it previously, but now that its installed I have restarted the script and it still only displays the window title
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Network related bash question

Post by Logansfury »

Is a reboot necessary?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Network related bash question

Post by Koentje »

If you run the bash script, what does it say?
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Network related bash question

Post by Logansfury »

Koentje wrote: Wed Jan 31, 2024 8:36 pm If you run the bash script, what does it say?

Code: Select all

logansfury@OptiPlex-5040:~$ cd .conky/Network\ Koentje
logansfury@OptiPlex-5040:~/.conky/Network Koentje$ ./networkpc.sh
${goto 110}${color white}192.168.0.101${goto 220}${color 66FF00}Online!
${goto 110}${color white}192.168.0.36${goto 220}${color 66FF00}Online!
${goto 110}${color white}192.168.0.174${goto 220}${color 66FF00}Online!
${goto 110}${color white}192.168.0.126${goto 220}${color 66FF00}Online!
${goto 110}${color white}192.168.0.235${goto 220}${color FF4422}Offline!
${goto 110}${color white}192.168.0.161${goto 220}${color 66FF00}Online!
logansfury@OptiPlex-5040:~/.conky/Network Koentje$
Looks like its got the pc's correct as far as whos online or not. It's just not displaying on my monitor
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Network related bash question

Post by Koentje »

Do you have this in your conky script? ${execpi 15 ./ping-networkpc.sh}
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Network related bash question

Post by Logansfury »

Koentje wrote: Wed Jan 31, 2024 8:46 pm Do you have this in your conky script? ${execpi 15 ./ping-networkpc.sh}
I do

Code: Select all

# Check if network PC is active
${font Noto Sans:bold:size=9}${voffset 0}${offset 1}${color2}NETWORKPC${execpi 15 ./ping-networkpc.sh}
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Network related bash question

Post by Koentje »

Logansfury wrote: Wed Jan 31, 2024 8:39 pm logansfury@OptiPlex-5040:~/.conky/Network Koentje$ ./networkpc.sh
DUH!! :roll:
Image
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Network related bash question

Post by Koentje »

But what about this one? ;)

Schermafdruk van 2024-02-01 02-25-20.jpg
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Network related bash question

Post by Logansfury »

Koentje wrote: Wed Jan 31, 2024 9:29 pm But what about this one? ;)


Schermafdruk van 2024-02-01 02-25-20.jpg
I never saw any scripts in the post for using that other one.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Network related bash question

Post by Koentje »

That's correct... if you ask nice? :mrgreen:
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Network related bash question

Post by Logansfury »

Koentje wrote: Wed Jan 31, 2024 9:34 pm That's correct... if you ask nice? :mrgreen:
May I have the scripts for the graph please?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Network related bash question

Post by Logansfury »

Koentje wrote: Wed Jan 31, 2024 9:27 pm
Logansfury wrote: Wed Jan 31, 2024 8:39 pm logansfury@OptiPlex-5040:~/.conky/Network Koentje$ ./networkpc.sh
DUH!! :roll:
oh OHHHHHHHHHHHH!! That file named networkpc.sh was supposed to be named ping-networkpc.sh all along wasn't it? I just changed the name and the widget is now display computer online status.

Far out!!
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Network related bash question

Post by Koentje »

The graph can be downloaded from my website http://www.cobrasoft.nl/download/conky/pinggraph
Image
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Network related bash question

Post by Koentje »

I attached the one with the icons..
networkpc2.tar.gz
(7.91 KiB) Downloaded 21 times


But you need to edit the /etc/host file for this one. Add all hostnames and ip's.

My host file looks like this:

Code: Select all

127.0.0.1  localhost
127.0.1.1  p3300lx
192.168.1.2  router2.asus.com
192.168.1.3  fritzbox
192.168.1.11  galaxy
192.168.1.22  camsung
192.168.1.101  p3300lx
192.168.1.102  mediapc
192.168.1.103  kodi
192.168.1.200  nas
192.168.1.250  ipcam-oprit1
192.168.1.251  ipcam-oprit2
192.168.1.252  ipcam-washok
192.168.1.253  ipcam-aanbouw
192.168.1.254  ipcam-shelby

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Image
Post Reply

Return to “Scripts & Bash”