LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-05-2018, 04:18 AM   #1
catiewong
Member
 
Registered: Aug 2018
Posts: 190

Rep: Reputation: Disabled
How to log all ESTABLISHED connection


I know the command "netstat -na " may output all running service .

If I would like to know what service has been " ESTABLISHED " , their incoming IP etc information has been running / connected to the server .

I just would like to log all running service which it has been " ESTABLISHED " , may I know how to do it , would advise how to write such script .
 
Old 12-05-2018, 07:55 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> How to log all ESTABLISHED connection
Code:
nestat -tan | fgrep ESTABLISHED
or even:
Code:
netstat -tan | fgrep ESTABLISHED | while read _ _ _ From To _; do echo "$From $To"; done
 
Old 12-05-2018, 08:13 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by catiewong View Post
I just would like to log all running service which it has been " ESTABLISHED " , may I know how to do it , would advise how to write such script .
Which kind of service? If you are using iptables or netstat or tcpdump then you are only tracking the state of the TCP (presumably not UDP) connection at layer 4 aka the transport layer and not the service itself which would be layer 7 aka the application layer.
 
1 members found this post helpful.
Old 12-05-2018, 07:04 PM   #4
catiewong
Member
 
Registered: Aug 2018
Posts: 190

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Which kind of service? If you are using iptables or netstat or tcpdump then you are only tracking the state of the TCP (presumably not UDP) connection at layer 4 aka the transport layer and not the service itself which would be layer 7 aka the application layer.
All service are established .
 
Old 12-05-2018, 07:43 PM   #5
catiewong
Member
 
Registered: Aug 2018
Posts: 190

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
> How to log all ESTABLISHED connection
Code:
nestat -tan | fgrep ESTABLISHED
or even:
Code:
netstat -tan | fgrep ESTABLISHED | while read _ _ _ From To _; do echo "$From $To"; done

Thanks your advise , your script may be work , if I would like to know the details of established service in period of time ( paat and future time ) , eg. from 2018-10-01 to 2018-12-31 , is it possible ?
 
Old 12-05-2018, 08:26 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
AFAIK, the state of a connection as reported by netstat is transitory. I don't know that it's logged anywhere...nor, probably, would we want it to be.
 
Old 12-05-2018, 08:39 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Unless you have logged everything between 2018-10-01 to 2018-12-31 what is past is gone.

In particular, the ESTABLISHED state as discussed here is a property defined by netfilter/iptables and is derived by netstat from the current state of kernel network processes. For non-TCP connections the ESTABLISHED state is entirely a fabrication of netfilter/iptables, so the only way to log that really would be to add an appropriate rule similar to this...

Code:
IPTABLES -A CHAINNAME -m conntrack --ctstate ESTABLISHED -j LOGGER  options
...where CHAINNAME is where you want to place the rule, usually INPUT or OUTPUT, and LOGGER is the logging facility you want to use (probably LOG, NFLOG or ULOGD, each with appropriate optional arguments).

Last edited by astrogeek; 12-05-2018 at 09:24 PM. Reason: more complete
 
Old 12-05-2018, 09:53 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: is this a homework assignment or an actual problem?
 
Old 12-05-2018, 10:02 PM   #9
catiewong
Member
 
Registered: Aug 2018
Posts: 190

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
Unless you have logged everything between 2018-10-01 to 2018-12-31 what is past is gone.

In particular, the ESTABLISHED state as discussed here is a property defined by netfilter/iptables and is derived by netstat from the current state of kernel network processes. For non-TCP connections the ESTABLISHED state is entirely a fabrication of netfilter/iptables, so the only way to log that really would be to add an appropriate rule similar to this...

Code:
IPTABLES -A CHAINNAME -m conntrack --ctstate ESTABLISHED -j LOGGER  options
...where CHAINNAME is where you want to place the rule, usually INPUT or OUTPUT, and LOGGER is the logging facility you want to use (probably LOG, NFLOG or ULOGD, each with appropriate optional arguments).
If the past is gone , what can I do if I start to log the information from now ?
 
Old 12-05-2018, 10:18 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by catiewong View Post
If the past is gone , what can I do if I start to log the information from now ?
Read the logs... ?

I am not sure I understand the question.

Last edited by astrogeek; 12-05-2018 at 10:18 PM. Reason: typos
 
Old 12-06-2018, 12:39 AM   #11
catiewong
Member
 
Registered: Aug 2018
Posts: 190

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
Read the logs... ?

I am not sure I understand the question.
I think it is confused .

If the past infomration can not be get , how to start to log the information from now ?
 
Old 12-06-2018, 01:06 AM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by catiewong View Post
I think it is confused .

If the past infomration can not be get , how to start to log the information from now ?
It is confusing. Pleas rephrase which services you wish to log and the nature of the information you wish to collect.
 
1 members found this post helpful.
Old 12-06-2018, 01:22 AM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by catiewong View Post
I think it is confused .

If the past infomration can not be get , how to start to log the information from now ?
Your thread title How to log all ESTABLISHED connection, and original question...

Quote:
Originally Posted by catiewong View Post
If I would like to know what service has been " ESTABLISHED " , their incoming IP etc information has been running / connected to the server .

I just would like to log all running service which it has been " ESTABLISHED " , may I know how to do it , would advise how to write such script .
And when asked about whether you need only TCP connections or other such as UDP, you replied...

Quote:
Originally Posted by catiewong View Post
All service are established .
...I take to mean you want to log all connections which pass through the ESTABLISHED state.

To do that you will need an iptables rule because UDP, ICMP, SCTP and potentially other protocols exist in the ESTABLISHED state only in netfilter/iptables context.

I gave an example of an iptables rule that you can adapt for that, but you will need to decide how you want to log those and configure the corresponding logging target.

You can begin to learn how to do that by looking at the man pages for iptables-extensions and ulogd, and searching for resources online.

If you need more help here, or if this does not seem to do what you need, it will be very helpful if you can try to explain very clearly just what your actual goal is.

Last edited by astrogeek; 12-06-2018 at 01:30 AM. Reason: typos, clarity
 
Old 12-12-2018, 07:13 PM   #14
catiewong
Member
 
Registered: Aug 2018
Posts: 190

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
It is confusing. Pleas rephrase which services you wish to log and the nature of the information you wish to collect.
As I will migrate this server to anohter server , I need to know what service is using in it so that I can setup such service to new server .

therefore , I would like to know all service has running in it .
 
Old 12-12-2018, 07:30 PM   #15
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by catiewong View Post
As I will migrate this server to anohter server , I need to know what service is using in it so that I can setup such service to new server .

therefore , I would like to know all service has running in it .
Ahh.
Code:
netstat -tnlp
will show you all services that are listening.
Those are the services you'd need to set up on the new server...

Looking at/for ESTABLISHED connections will only tell you which services are being used at the time you take a snapshot. Theoretically that might (probably will) be a subset of the services you need to set up.

Last edited by scasey; 12-12-2018 at 07:32 PM.
 
1 members found this post helpful.
  


Reply



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
How to log internal-sftp chroot jailed users access log to /var/log/sftp.log file LittleMaster Linux - Server 0 09-04-2018 03:45 PM
Time out in Connection established state if no Data flows on that connection asurya Linux - Networking 2 04-10-2005 03:54 PM
How to detect that a dial-up Internet connection was established? zeppelin Programming 3 09-28-2003 05:11 AM
Will select fail if connection couldn't be established? ruchika Linux - Software 0 09-10-2003 11:39 AM
problems with established connection nbc Linux - Newbie 1 08-16-2001 01:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:08 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
Open Source Consulting | Domain Registration