You are not logged in.

#1 2011-05-04 19:36:37

dodo3773
Member
Registered: 2011-03-17
Posts: 814

[Solved] Is there a better way to show apps accessing internet

Currently I am using

lsof -i -n -P | awk '{print $1}' | uniq | sed '/COMMAND/d'

which just spits out the names of the applications that are using internet access (which is all I want). But Deluge comes up as /usr/bin. It comes up that way in the system monitor as well. Does anyone know a better way of doing this other that lsof?

Last edited by dodo3773 (2011-05-04 21:32:11)

Offline

#2 2011-05-04 19:56:06

Lars Stokholm
Member
From: Denmark
Registered: 2009-03-17
Posts: 223

Re: [Solved] Is there a better way to show apps accessing internet

I don't know if its better, but I'm using 'netstat -pant'. Short and easy to remember.

Edit: Oh, if it should JUST spit out the names, I don't know. Sorry.

Last edited by Lars Stokholm (2011-05-04 20:03:54)

Offline

#3 2011-05-04 20:20:19

dodo3773
Member
Registered: 2011-03-17
Posts: 814

Re: [Solved] Is there a better way to show apps accessing internet

Lars Stokholm wrote:

I don't know if its better, but I'm using 'netstat -pant'. Short and easy to remember.

Edit: Oh, if it should JUST spit out the names, I don't know. Sorry.


Right on that works pretty well. But now instead of showing "/usr/bin"  it shows "python2"  Although, python2 is a lot more accurate then /usr/bin.

Last edited by dodo3773 (2011-05-04 20:31:36)

Offline

#4 2011-05-04 20:23:34

student975
Member
From: Russian Federation
Registered: 2011-03-05
Posts: 613

Re: [Solved] Is there a better way to show apps accessing internet


"I exist" is the best myth I know..

Offline

#5 2011-05-04 20:31:07

SanskritFritz
Member
From: Budapest, Hungary
Registered: 2009-01-08
Posts: 1,924
Website

Re: [Solved] Is there a better way to show apps accessing internet

Wow, thanks!


zʇıɹɟʇıɹʞsuɐs AUR || Cycling in Budapest with a helmet camera || Revised log levels proposal: "FYI" "WTF" and "OMG" (John Barnette)

Offline

#6 2011-05-04 20:58:41

dodo3773
Member
Registered: 2011-03-17
Posts: 814

Re: [Solved] Is there a better way to show apps accessing internet

Now I am getting /usr/bin/python2. Deluge is python2 though. I am thinking that actually showing deluge might not be possible.

Offline

#7 2011-05-04 21:07:27

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [Solved] Is there a better way to show apps accessing internet

It would require a bit of additional mangling

[karol@black ~]$ ps -o cmd= -p $(lsof -i -n -P | sed 1d | awk '{print $2}' | uniq)
firefox
/usr/bin/python2 /usr/bin/deluged
/usr/bin/python2 /usr/bin/deluge -u console

or

[karol@black ~]$ ps -o cmd= $(pgrep deluge$)
/usr/bin/python2 /usr/bin/deluge -u console

Offline

#8 2011-05-04 21:25:05

dodo3773
Member
Registered: 2011-03-17
Posts: 814

Re: [Solved] Is there a better way to show apps accessing internet

karol wrote:

It would require a bit of additional mangling

[karol@black ~]$ ps -o cmd= -p $(lsof -i -n -P | sed 1d | awk '{print $2}' | uniq)
firefox
/usr/bin/python2 /usr/bin/deluged
/usr/bin/python2 /usr/bin/deluge -u console

or

[karol@black ~]$ ps -o cmd= $(pgrep deluge$)
/usr/bin/python2 /usr/bin/deluge -u console

This is very interesting. When I do it I see

firefox
evolution --component=mail
/usr/bin/python2 /usr/bin/deluge

What I would like to get from this is

firefox
evolution
/usr/bin/deluge # or just deluge

So, maybe with an if then statement... Need to look at this a little closer. Thanks for the tip though. This is great.

Offline

#9 2011-05-04 21:27:34

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [Solved] Is there a better way to show apps accessing internet

You may simply cheat and grep away deluge, evolution or whatever.

Last edited by karol (2011-05-04 21:34:23)

Offline

#10 2011-05-04 21:30:36

dodo3773
Member
Registered: 2011-03-17
Posts: 814

Re: [Solved] Is there a better way to show apps accessing internet

Got it. I can get the results with this

ps -o cmd= -p $(lsof -i -n -P | sed 1d | awk '{print $2}' | uniq) | sed /deluge/d | awk '{print $1}' && ps -o cmd= -p $(lsof -i -n -P | sed 1d | awk '{print $2}' | uniq) | grep deluge | awk '{print $2}' | cut -c 10-15

and it will give me

firefox
evolution
deluge

Thanks a lot everybody.

Last edited by dodo3773 (2011-05-04 21:40:09)

Offline

#11 2011-05-04 21:59:29

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [Solved] Is there a better way to show apps accessing internet

The sed hieroglyphics might be more flexible:

lsof -nPi | awk '{print $1}' | uniq | sed 1d | sed -e 's#evolution --component=mail#evolution#' -e 's#/usr/bin/python2 /usr/bin/deluge#deluge#'

Simply add a sed substitution for any troublesome app.

Last edited by karol (2011-05-04 22:05:15)

Offline

#12 2011-05-04 22:25:05

dodo3773
Member
Registered: 2011-03-17
Posts: 814

Re: [Solved] Is there a better way to show apps accessing internet

karol wrote:

The sed hieroglyphics might be more flexible:

lsof -nPi | awk '{print $1}' | uniq | sed 1d | sed -e 's#evolution --component=mail#evolution#' -e 's#/usr/bin/python2 /usr/bin/deluge#deluge#'

Simply add a sed substitution for any troublesome app.


So

sed -e 's#evolution --component=mail#evolution#'

relaces "evolution --component=mail" with "evolution"

that is definately cleaner. Thanks.

Offline

#13 2011-05-05 16:45:40

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [Solved] Is there a better way to show apps accessing internet

Does that work? It looks to me like it shouldn't, but I don't have any apps that fit that criteria to test with.

The issue to me is that awk '{print $1}' will only print up to the first space, whereas both sed substitutions include a space. I might be missing something obvious though. smile

I dislike piping from awk to sed and would normally try to write the whole thing in awk if I have the time, but I haven't written anything in awk for a few months and it'll take too long for the benefit, so here's how I'd do it:

lsof -nPi | awk '!/COMMAND/{print $1}' | uniq | sed 's/.*\(evolution\).*/\1/;s/.*\(deluge\).*/\1/'

This, at least, removes some unnecessary sed calls, though the sed command is slightly less clear. You can embed newlines into the sed command if preferred like this:

lsof -nPi | awk '!/COMMAND/{print $1}' | uniq | sed 's/.*\(evolution\).*/\1/;
                                                     s/.*\(deluge\).*/\1/'

"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#14 2011-05-05 17:40:48

Cdh
Member
Registered: 2009-02-03
Posts: 1,098

Re: [Solved] Is there a better way to show apps accessing internet

Isn't this all very ugly? Why not write a simple tokenizer function in bash that returns the first token, if it is not /usr/bin/python or /opt/java/bin/java and in this case return the second token?
On second thought there maybe is such a script that returns the "true" name of the process somewhere...


฿ 18PRsqbZCrwPUrVnJe1BZvza7bwSDbpxZz

Offline

#15 2011-05-05 20:07:43

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [Solved] Is there a better way to show apps accessing internet

Good idea.

Untested:

lsof -nPi | awk '!/COMMAND/ {print $1}
                  /.*python[2]*/ || /.*java/ {print $2}' | uniq

It doesn't clear the path of applications run via those two. It's about two steps from being a full awk application. Add names to an associative array (makes it uniq) and strip the path.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#16 2011-05-06 09:53:29

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [Solved] Is there a better way to show apps accessing internet

Okay, there were several errors with all of the above, so I've fixed them here. Again, I couldn't test python & java output, but I made a text file with what I thought (from above) they would look like and tested against that. It may need a few minor tweaks though:

#!/bin/bash

lsof -nPi | awk '/.*python[2]*/ || /.*java/ {cmd[$2] = $2;next}
                 !/COMMAND/ {cmd[$1]=$1}
                 END {for (item in cmd ) {
                          sub(".*/", "", item)
                         print item}
                     }'

One possible improvement is that it could be made just an awk script and call lsof from within awk (use getline).


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#17 2011-05-06 14:32:47

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [Solved] Is there a better way to show apps accessing internet

skanky wrote:

Okay, there were several errors with all of the above, so I've fixed them here. Again, I couldn't test python & java output, but I made a text file with what I thought (from above) they would look like and tested against that. It may need a few minor tweaks though:

#!/bin/bash

lsof -nPi | awk '/.*python[2]*/ || /.*java/ {cmd[$2] = $2;next}
                 !/COMMAND/ {cmd[$1]=$1}
                 END {for (item in cmd ) {
                          sub(".*/", "", item)
                         print item}
                     }'

One possible improvement is that it could be made just an awk script and call lsof from within awk (use getline).

Nice! How about posting it here?

Offline

#18 2011-05-06 15:57:26

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [Solved] Is there a better way to show apps accessing internet

Done, thanks.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

Board footer

Powered by FluxBB