<s> """<STR_LIT>""" <EOL> import psutil <EOL> import logging <EOL> from grr . client import actions <EOL> from grr . lib . rdfvalues import client as rdf_client <EOL> class Netstat ( actions . ActionPlugin ) : <EOL> """<STR_LIT>""" <EOL> in_rdfvalue = None <EOL> out_rdfvalues = [ rdf_client . NetworkConnection ] <EOL> def Run ( self , unused_args ) : <EOL> netstat = [ ] <EOL> for proc in psutil . process_iter ( ) : <EOL> try : <EOL> netstat . append ( ( proc . pid , proc . connections ( ) ) ) <EOL> except ( psutil . NoSuchProcess , psutil . AccessDenied ) : <EOL> pass <EOL> for pid , connections in netstat : <EOL> for conn in connections : <EOL> res = rdf_client . NetworkConnection ( ) <EOL> res . pid = pid <EOL> res . family = conn . family <EOL> res . type = conn . type <EOL> try : <EOL> if conn . status : <EOL> res . state = conn . status <EOL> except ValueError : <EOL> logging . warn ( "<STR_LIT>" , <EOL> conn . status ) <EOL> res . local_address . ip , res . local_address . port = conn . laddr <EOL> if conn . raddr : <EOL> res . remote_address . ip , res . remote_address . port = conn . raddr <EOL> self . SendReply ( res ) </s>
