[medusa] kill open port

Lucifer lucifer@e...
Thu, 15 Mar 2001 16:00:44 -0400


Thank you Sam· It will be useful if this happens again.

On Wed, 14 Mar 2001 14:20:01 -0800, Sam Rushing wrote:
> Lucifer wrote:
> 
> > I'm using Linux Sam. Maybe your script would help.
> 
> Better late than never. 8^)
> (just checked it, appears to still work on a 2.4 kernel)
> 
> [root@s... rushing]# python ./who_owns.py -s 890 udp
> inode: 17987
> Name: rpc.statd
> State: S (sleeping)
> Pid: 1137
> PPid: 1
> TracerPid: 0
> Uid: 0 0 0 0
> Gid: 0 0 0 0
> FDSize: 32
> Groups: 0 1 2 3 4 6 10 11
> VmSize: 1176 kB
> VmLck: 0 kB
> VmRSS: 0 kB
> VmData: 36 kB
> VmStk: 12 kB
> VmExe: 20 kB
> VmLib: 1080 kB
> SigPnd: 0000000000000000
> SigBlk: 0000000000000000
> SigIgn: 0000000000000000
> SigCgt: 0000000000004003
> CapInh: 0000000000000000
> CapPrm: 00000000fffffeff
> CapEff: 00000000fffffeff
> 
> -Sam
> 
> 
> 
> 
> 
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 
> 
> 
> #!/usr/local/bin/python
> # -*- Mode: Python; tab-width: 4 -*-
> 
> import os
> import regex
> import string
> import sys
> 
> # linux-specific.
> # find out what process is listening on a particular socket
> 
> def inode_of_socket (port, type='tcp', server_only=1, remote=0):
> target_port = string.upper ('%04x' % (string.atoi (port)))
> lines = open ('/proc/net/%s' % type).readlines()
> inode = 0
> for line in lines[1:]:
> fields = string.split (line)
> if remote:
> index = 2
> else:
> index = 1
> [addr, port] = string.split (fields[index], ':')
> if port == target_port:
> if (not server_only) or (addr == '00000000'):
> inode = string.atoi (fields[9])
> break
> return inode
> 
> pid_regex = regex.compile ('\([0-9]+\)')
> 
> def process_of_inode (inode):
> #inode = '[0000]:%d' % inode
> inode = 'socket:[%d]' % inode
> cwd = os.getcwd()
> try:
> pids = filter (
> lambda x: pid_regex.match (x) == len(x),
> os.listdir('/proc')
> )
> pids.remove (str(os.getpid()))
> for pid in pids:
> fd_dir = '/proc/%s/fd' % pid
> if os.path.isdir (fd_dir):
> try:
> os.chdir (fd_dir)
> except OSError:
> # hmm, a zombie?
> if string.split (open('/proc/%s/stat' % pid).read())[2] == 'Z':
> continue
> links = map (
> os.readlink,
> os.listdir ('.')
> )
> if inode in links:
> return pid
> finally:
> os.chdir (cwd)
> return 0
> 
> def getopt (s):
> import sys
> if s in sys.argv:
> sys.argv.remove (s)
> return 1
> else:
> return 0
> 
> if __name__ == '__main__':
> if len(sys.argv) < 2:
> print 'Usage: %s [-r|-s] <port> [tcp|udp]' % sys.argv[0]
> else:
> remote = getopt ('-r')
> server = getopt ('-s')
> if len(sys.argv) < 3:
> socket_type='tcp'
> else:
> socket_type=sys.argv[2]
> port = sys.argv[1]
> inode = inode_of_socket (port, socket_type, server_only=server, remote=remote)
> if not inode:
> print "Couldn't find inode for socket on %s port %s" % (
> socket_type,
> port
> )
> sys.exit(0)
> else:
> print 'inode: %d' % inode
> process = process_of_inode (inode)
> if not process:
> print "Couldn't find process for inode %d" % inode
> else:
> print open('/proc/%s/status' % process).read()
> 
>