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 02-09-2014, 06:05 PM   #1
phillyfilly
LQ Newbie
 
Registered: Feb 2014
Posts: 10

Rep: Reputation: Disabled
Need someone to translate


Please translate, from Linux into English, word by word.

netstat -an > nstat; for i in `cat nstat | grep :80 | awk '{print $5}' | awk -F : '{print $1}' | sort | uniq`; do echo -n "$i = "; cat nstat | grep c $i;done | grep -v "=\0" -

Running Fedora 20 on VMware Workstation 10.

Thank you!!!!!!!!!!!
 
Old 02-09-2014, 06:38 PM   #2
Isaac Velando
LQ Newbie
 
Registered: Feb 2014
Location: Texas
Distribution: Arch, Ubuntu Server, CentOS
Posts: 29

Rep: Reputation: 21
I could have something off but I think there's a syntax error with the line you posted, but I believe the intent of the script is clear (for me) so I used this slightly altered version (tested on CentOS 5):

netstat -an > nstat; for i in `cat nstat | grep :80 | awk '{print $5}' | awk -F : '{print $1}' | sort | uniq`; do echo -en "$i = "$(cat nstat | grep -c $i)"\n";done | grep -v "= 0" -

Code:
netstat -an > nstat
Write the output of netstat -an to a file called "nstat"; netstat -an will list all network connections using numeric ports rather than symbolic (443 instead of https, 80 instead of http, etc).

Code:
cat nstat | grep :80
Print the contents of the nstat file and print only lines containing the string ":80"; this will filter to http web connections.

Code:
 | awk '{print $5}'
This parses each line from above using white space as a delimiter and prints the 5th entry; in this case that is the foreign address including the port such as 1.2.3.4:80

Code:
 | awk -F : '{print $1}'
This further parses the lines from above but now uses the delimiter of ":" and so it prints the 1st item delimited by a colon; this is the IP address such as 1.2.3.4

Code:
 | sort | uniq
Sort these IP addresses and remove duplicates.

Code:
 for i in `that list of unique IP addresses from before`; do some stuff; done
Check every one of the unique IP addresses and do something with them

Code:
 cat nstat | grep -c $i
The -c option causes grep to count how many lines are matched by the pattern in $i, which in this case is a unique IP address

In my version (and I'm fairly certain the intent of the original)
Code:
 echo -en "$i = "$(cat nstat | grep -c $i)"\n"
Print the line:

"[Unique IP] = [Number of times it was found as a web connection]"

And the "\n" is a new line character. The -e option interprets backslash escaped characters like \n here, and -n suppresses the trailing new line. The $( commands ) syntax allows us to make use of the output of whatever is within the parentheses, and in bash we can combined together strings like this by placing them together.

Code:
 | grep -v "= 0" -
The -v option for grep inverts matching so it only prints what isn't matched. This goes at the end - after the loop - so once we have this list that we've seen above, only print lines that don't have = 0, so don't print any with no occurrences.

So you're using netstat to find all unique IP addresses connected on the standard http port 80 and count how many times they each occur. Hope this helps.

For reference, while this hopefully helps teach by example (which is how I like to learn myself), in the future you might consider some tips to help disect something like this. If you're unsure what a command does consult its man (manual) page:
Code:
man netstat
for instance. If you want to quickly look up an option you can type "/" while on the man page to enter search mode and for instance search for -n or -a; use "n" to find the next match and "N" to find the previous. Also whenever you see a hefty one-liner you're unsure of I recommend doing as I did and split it up into pieces and focus on the purpose of each bit one at a time.

Last edited by Isaac Velando; 02-09-2014 at 06:59 PM.
 
1 members found this post helpful.
Old 02-09-2014, 07:42 PM   #3
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,460
Blog Entries: 11

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
explainshell.com
 
Old 02-09-2014, 10:26 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,515

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
I would further add that if you wish to learn more, you should re-write the line as there is a large amount of redundancy and useless cruft that could be removed so it is not nearly as confusing.

As an example, awk would have been able to complete the entire task, so you would not need cat, grep, sort, uniq, file redirection or even the for loop (maybe as have not confirmed required output)
 
2 members found this post helpful.
Old 02-09-2014, 10:46 PM   #5
Isaac Velando
LQ Newbie
 
Registered: Feb 2014
Location: Texas
Distribution: Arch, Ubuntu Server, CentOS
Posts: 29

Rep: Reputation: 21
Agreed grail. In fact I'd add that awk is worth looking into in depth for anyone doing terminal work as it's basically a (perhaps limited) programming language itself that can accomplish, as grail suggests, a surprising amount by itself.
 
  


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] Translate WifiSlax-3.1 powerlinux Linux - Distributions 1 11-23-2010 08:49 AM
BASH/No X: Using google translate to convert TXT files (translate) frenchn00b Programming 10 09-13-2009 10:55 PM
to translate or not to translate HTML rblampain General 2 07-05-2007 09:04 AM
translate C to MIPS kponenation Programming 1 10-01-2005 09:12 PM
please, translate to me maginotjr Slackware 6 07-08-2005 06:40 PM

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

All times are GMT -5. The time now is 12:56 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