LinuxQuestions.org Member Success Stories Just spent four hours configuring your favorite program? Just figured out a Linux problem that has been stumping you for months?
Post your Linux Success Stories here. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-18-2025, 08:28 AM
|
#1
|
|
Member
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Arch
Posts: 896
|
i wrote perl script that shows ( connections - users - programs ) with Conky.
here is that perl script :
Code:
#!/usr/bin/perl
use strict;
use warnings;
# This script uses the 'ss' command to list network connections and the
# processes and users that own them. It relies on parsing human-readable
# output, which can be fragile if the output format of 'ss' changes.
# Command to execute. Flags are:
# -t: TCP, -u: UDP, -p: processes, -a: all, -n: numeric, -e: extended
my $ss_cmd = 'ss -tupane';
# A hash to store our results, structured as:
# $connections->{program_name}->{pid}->{user} = "username"
# $connections->{program_name}->{pid}->{conns} = ["connection_string", ...]
my %connections;
# Execute the command and open a pipe to its output.
# Die with an error message if the command cannot be run.
open(my $ss_pipe, '-|', $ss_cmd)
or die "Cannot execute '$ss_cmd': $!";
# Skip the header line of the ss output.
<$ss_pipe>;
while (my $line = <$ss_pipe>) {
chomp $line;
# The regex is the most complex part. It now also captures the UID.
# It captures:
# 1: Protocol (tcp or udp)
# 2: State (e.g., ESTAB, LISTEN)
# 3: Local Address:Port
# 4: Peer Address:Port
# 5: Process Name
# 6: PID
# 7: UID
if ($line =~ /^(udp|tcp)\s+([A-Z-]+)\s+\S+\s+\S+\s+([\d.:*]+\[?[\d.:]*\]?:\S+)\s+([\d.:*]+\[?[\d.:]*\]?:\S+)\s+users:\(\("([^"]+)",pid=(\d+)[^)]*\)\).*\buid:(\d+)/) {
my ($proto, $state, $local, $peer, $pname, $pid, $uid) = ($1, $2, $3, $4, $5, $6, $7);
# Look up the username from the UID.
# If the UID is not found, default to "uid:<uid_number>".
my $user = getpwuid($uid);
$user //= "uid:$uid";
# Format the connection string for display.
my $conn_string = sprintf(
" %-5s %-12s %-22s -> %-22s",
$proto, $state, $local, $peer
);
# Store the user info (only needs to be stored once per process).
$connections{$pname}{$pid}{'user'} = $user;
# Store the connection string.
push @{ $connections{$pname}{$pid}{'conns'} }, $conn_string;
}
}
close $ss_pipe;
# Print the results in an organized fashion.
if (!%connections) {
print "No active connections with process information found.\n";
exit;
}
# Sort by program name for consistent output.
foreach my $pname (sort keys %connections) {
foreach my $pid (sort { $a <=> $b } keys %{ $connections{$pname} }) {
# Retrieve the stored user and connection list.
my $user = $connections{$pname}{$pid}{'user'};
my $conns_ref = $connections{$pname}{$pid}{'conns'};
# Print the header with Program, PID, and the new User info.
print "Program: $pname (PID: $pid, User: $user)\n";
foreach my $conn (@$conns_ref) {
print "$conn\n";
}
print "\n";
}
}
here is conky.conf :
Code:
conky.config = {
own_window = true,
own_window_type = 'normal',
own_window_transparent = true,
own_window_argb_visual = true,
own_window_hints = 'undecorated,skip_taskbar,skip_pager,below',
double_buffer = true,
use_spacer = 'right',
double_buffer = true,
no_buffers = true,
text_buffer_size = 4096,
use_xft = true,
alignment = 'bottom_left',
gap_x = 20,
gap_y = 10,
update_interval = 5,
minimum_width = 600,
maximum_width = 1200,
stippled_borders = 3,
border_width = 1,
default_color = '888888',
draw_outline = no,
draw_borders = yes,
font = 'monospace:size=12',
uppercase = no,
draw_shades = yes,
}
conky.text = [[
${color lightblue}Connections by programs:${color}
${color white}${execi 10 /usr/bin/perl /usr/local/bin/netinfo.pl}
${hr 2}
]];
|
|
|
|
All times are GMT -5. The time now is 06:33 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|