Use netstat on sunos: fixups

Make it DRYer, should no longer fail lint.
This commit is contained in:
Nahum Shalman 2014-09-02 13:39:54 -04:00
parent de884cf7eb
commit cc62191b8e

View File

@ -920,6 +920,44 @@ def _parse_tcp_line(line):
return ret
def _sunos_remotes_on(port, which_end):
'''
SunOS specific helper function.
Returns set of ipv4 host addresses of remote established connections
on local or remote tcp port.
Parses output of shell 'netstat' to get connections
[root@salt-master ~]# netstat -f inet -n
TCP: IPv4
Local Address Remote Address Swind Send-Q Rwind Recv-Q State
-------------------- -------------------- ----- ------ ----- ------ -----------
10.0.0.101.4505 10.0.0.1.45329 1064800 0 1055864 0 ESTABLISHED
10.0.0.101.4505 10.0.0.100.50798 1064800 0 1055864 0 ESTABLISHED
'''
remotes = set()
try:
data = subprocess.check_output(['netstat', '-f', 'inet', '-n'])
except subprocess.CalledProcessError:
log.error('Failed netstat')
raise
lines = data.split('\n')
for line in lines:
if 'ESTABLISHED' not in line:
continue
chunks = line.split()
local_host, local_port = chunks[0].rsplit('.', 1)
remote_host, remote_port = chunks[1].rsplit('.', 1)
if which_end == 'remote_port' and int(remote_port) != port:
continue
if which_end == 'local_port' and int(local_port) != port:
continue
remotes.add(remote_host)
return remotes
def remotes_on_local_tcp_port(port):
'''
Returns set of ipv4 host addresses of remote established connections
@ -938,33 +976,7 @@ def remotes_on_local_tcp_port(port):
remotes = set()
if salt.utils.is_sunos():
'''
[root@salt-master ~]# netstat -f inet -n
TCP: IPv4
Local Address Remote Address Swind Send-Q Rwind Recv-Q State
-------------------- -------------------- ----- ------ ----- ------ -----------
10.0.0.101.4505 10.0.0.1.45329 1064800 0 1055864 0 ESTABLISHED
10.0.0.101.4505 10.0.0.100.50798 1064800 0 1055864 0 ESTABLISHED
'''
try:
data = subprocess.check_output(['netstat', '-f', 'inet', '-n'])
except subprocess.CalledProcessError as exc:
log.error('Failed netstat')
raise
lines = data.split('\n')
for line in lines:
if 'ESTABLISHED' not in line:
continue
chunks = line.split()
local_host, local_port = chunks[0].rsplit('.', 1)
remote_host, remote_port = chunks[1].rsplit('.', 1)
if int(local_port) != port:
continue
remotes.add(remote_host)
return remotes
return _sunos_remotes_on(port, 'local_port')
try:
data = subprocess.check_output(['lsof', '-i4TCP:{0:d}'.format(port), '-n'])
@ -1013,33 +1025,7 @@ def remotes_on_remote_tcp_port(port):
remotes = set()
if salt.utils.is_sunos():
'''
[root@salt-master ~]# netstat -f inet -n
TCP: IPv4
Local Address Remote Address Swind Send-Q Rwind Recv-Q State
-------------------- -------------------- ----- ------ ----- ------ -----------
10.0.0.101.4505 10.0.0.1.45329 1064800 0 1055864 0 ESTABLISHED
10.0.0.101.4505 10.0.0.100.50798 1064800 0 1055864 0 ESTABLISHED
'''
try:
data = subprocess.check_output(['netstat', '-f', 'inet', '-n'])
except subprocess.CalledProcessError as exc:
log.error('Failed netstat')
raise
lines = data.split('\n')
for line in lines:
if 'ESTABLISHED' not in line:
continue
chunks = line.split()
local_host, local_port = chunks[0].rsplit('.', 1)
remote_host, remote_port = chunks[1].rsplit('.', 1)
if int(remote_port) != port:
continue
remotes.add(remote_host)
return remotes
return _sunos_remotes_on(port, 'remote_port')
try:
data = subprocess.check_output(['lsof', '-i4TCP:{0:d}'.format(port), '-n'])