Repository: incubator-trafodion Updated Branches: refs/heads/master 9cc1e835f -> 4f8bbf7e5
fix 1795: findPort.py may find some ports that are in CLOSE_WAIT state Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/57a5527f Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/57a5527f Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/57a5527f Branch: refs/heads/master Commit: 57a5527f31fe36b708cb49cca00e25d89fbe762d Parents: 7a63602 Author: Qifan Chen <[email protected]> Authored: Mon Feb 1 00:36:12 2016 +0000 Committer: Qifan Chen <[email protected]> Committed: Mon Feb 1 00:36:12 2016 +0000 ---------------------------------------------------------------------- core/sqf/sql/scripts/findPort.py | 40 +++++++++---- core/sqf/sql/scripts/lsof.awk | 110 ++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/57a5527f/core/sqf/sql/scripts/findPort.py ---------------------------------------------------------------------- diff --git a/core/sqf/sql/scripts/findPort.py b/core/sqf/sql/scripts/findPort.py index 6179a89..4d505f3 100644 --- a/core/sqf/sql/scripts/findPort.py +++ b/core/sqf/sql/scripts/findPort.py @@ -63,23 +63,39 @@ args = parser.parse_args() # exits and prints help if args are incorrect exitCode = 0 -# get the set of lines to process ( netstat | grep localhost ) -p1 = subprocess.Popen(["netstat"], stdout=subprocess.PIPE) -p2 = subprocess.Popen(["grep","localhost"], stdin=p1.stdout, stdout=subprocess.PIPE, close_fds=True) - -# process the lines, looking for port numbers - -pattern = r'localhost:(?P<portNumber>[0-9]{5})' -matcher = re.compile(pattern) +retcode = subprocess.call("sudo -ll lsof", shell=True) inUseRanges = sets.Set() -for line in p2.stdout: - result = matcher.findall(line) - for occurrance in result: - rangeInUse = int(occurrance)/200 +if retcode == 0: + # sudo lsof can be run successfully + # get the ips in use to process ( sudo lsof -i | awk -f lsof.awk ) + print "Use sudo lsof to get port numbers"; + p1 = subprocess.Popen(["sudo", "lsof", "-i"], stdout=subprocess.PIPE) + p2 = subprocess.Popen(["awk", "-f", "lsof.awk"], stdin=p1.stdout, stdout=subprocess.PIPE, close_fds=True) + + for ip in p2.stdout: + rangeInUse = int(ip)/200 inUseRanges.add(rangeInUse) +else : + # no sudo permission on lsof + # get the set of lines to process ( netstat | grep localhost ) + print "Use netstat to get port numbers"; + p1 = subprocess.Popen(["netstat"], stdout=subprocess.PIPE) + p2 = subprocess.Popen(["grep","localhost"], stdin=p1.stdout, stdout=subprocess.PIPE, close_fds=True) + + # process the lines, looking for port numbers + + pattern = r'localhost:(?P<portNumber>[0-9]{5})' + matcher = re.compile(pattern) + + + for line in p2.stdout: + result = matcher.findall(line) + for occurrance in result: + rangeInUse = int(occurrance)/200 + inUseRanges.add(rangeInUse) # avoid recommending low ranges; our lowest recommendation # will be the first unused range above the lowest in-use range http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/57a5527f/core/sqf/sql/scripts/lsof.awk ---------------------------------------------------------------------- diff --git a/core/sqf/sql/scripts/lsof.awk b/core/sqf/sql/scripts/lsof.awk new file mode 100644 index 0000000..c42958f --- /dev/null +++ b/core/sqf/sql/scripts/lsof.awk @@ -0,0 +1,110 @@ +# @@@ START COPYRIGHT @@@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# @@@ END COPYRIGHT @@@ +# +############################################################################## +# +# An AWK script to find ports in the output of command lsof -i. +# +############################################################################## +# +# +# +# For the following ouput, +# +# COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME +# java 372 zellerh 325u IPv6 539757798 0t0 TCP localhost:41761->localhost:eforward (ESTABLISHED) +# java 372 zellerh 330u IPv6 539758057 0t0 TCP *:4200 (LISTEN) +# master 2354 root 12u IPv4 38168 0t0 TCP localhost:smtp (LISTEN) +# master 2354 root 13u IPv6 38170 0t0 TCP localhost:smtp (LISTEN) +# dhclient 4620 root 6u IPv4 2628139 0t0 UDP *:bootpc +# sshd 10514 root 3r IPv4 574815707 0t0 TCP adev04.esgyn.com:ssh->cpe-24-27-32-126.austin.res.rr.com:59216 (ESTABLISHED) +# sshd 10535 qfc 3u IPv4 574815707 0t0 TCP adev04.esgyn.com:ssh->cpe-24-27-32-126.austin.res.rr.com:59216 (ESTABLISHED) +# sshd 10714 root 3r IPv4 574816896 0t0 TCP adev04.esgyn.com:ssh->cpe-24-27-32-126.austin.res.rr.com:59217 (ESTABLISHED) +# sshd 10735 qfc 3u IPv4 574816896 0t0 TCP adev04.esgyn.com:ssh->cpe-24-27-32-126.austin.res.rr.com:59217 (ESTABLISHED) +# sshd 13507 root 3r IPv4 569638562 0t0 TCP adev04.esgyn.com:ssh->76-244-44-66.lightspeed.sntcca.sbcglobal.net:55554 (ESTABLISHED) +# sshd 13540 hegdean 3u IPv4 569638562 0t0 TCP adev04.esgyn.com:ssh->76-244-44-66.lightspeed.sntcca.sbcglobal.net:55554 (ESTABLISHED) +# python2.6 14346 hegdean 4u IPv4 552343030 0t0 TCP adev04.esgyn.com:46083 (LISTEN) +# Xvnc-core 15343 qfc 0u IPv6 449977085 0t0 TCP *:6049 (LISTEN) +# Xvnc-core 15343 qfc 1u IPv4 449977086 0t0 TCP *:6049 (LISTEN) +# +# +# the script will identify and print out following port numbers. +# +#41761 +#4200 +#59216 +#59216 +#59217 +#59217 +#55554 +#55554 +#46083 +#6049 +#6049 + + + +# find port in string of form <do-not-care>:[0-9]{4,5}<do-not-care> +function reportPort(val) +{ + parts = split(val, iparray ,":"); + + if ( parts > 1 ) { +# printf "%s\n", iparray[2] + # get the port number after ':' + ippos = match(iparray[2], "[0-9][0-9][0-9][0-9]*"); + nonip_pos = match(iparray[2], "[^0-9]"); + + if ( ippos > 0 ) { + if ( nonip_pos > 0 ) + ip=substr(iparray[2], ippos, nonip_pos - ippos + 1); + else + ip=substr(iparray[2], ippos); + + printf "%s\n", ip; + } + } +} + +{ + x=split($0,a,"TCP"); + + # Identify <string-may-contain-ip> in string of form + # <do-not-care>TCP<string-may-cpontain-ip> + if ( x > 1 ) { + + #<string-may-cpontain-ip> may be one of the following two forms: + # <ip-string> -> <ip-string>, or + # <ip-string> + # + # <ip-string> is something like <do-not-care>:<port><do-not-care> + # <port> is [0-9]{4,} + # + # check if exists -> + y = split(a[2],b,"->"); + + if (y>1) { + reportPort(b[1]); + reportPort(b[2]); + } else + reportPort(a[2]); + } +}
