LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories
User Name
Password
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


Reply
  Search this Thread
Old 07-18-2025, 08:28 AM   #1
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Arch
Posts: 896

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
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}
]];
Attached Thumbnails
Click image for larger version

Name:	network_info.png
Views:	31
Size:	217.9 KB
ID:	44996  
 
  


Reply

Tags
conky, connections, network, programs, users



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [Conky]problems with perl script and how conky prints output of that script. ////// Linux - Software 4 01-17-2024 09:58 AM
conky question: passing conky-variable to shell-script zlin50 Linux - Software 2 12-29-2012 06:47 PM
LXer: Conky Colors Makes Your Conky Beautiful In Seconds (Version 3.20 Released) LXer Syndicated Linux News 0 08-01-2010 11:21 AM
LXer: Conky Ubuntu Lucid Theme - Among The Easiest to Install Conky Theme in Ubuntu LXer Syndicated Linux News 0 07-05-2010 04:11 PM
Conky on Suse 10.0 - *** buffer overflow detected *** conky terminated Slidex Linux - Software 1 03-17-2006 11:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories

All times are GMT -5. The time now is 06:33 PM.

Contact Us - Advertising Info - Rules - Privacy - Donations - Contributing Member - LQ Sitemap - "Weather apps tell you it'll rain. Wyndo tells you when to go."
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