mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
Fix/spaces in program name (#608)
* fix: enforce word boundaries while checking state presence * fix: add tests for the special netstat case with space in process name --------- Co-authored-by: Kelly Brazil <kellyjonbrazil@gmail.com>
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
r"""jc - JSON Convert Linux netstat Parser"""
|
r"""jc - JSON Convert Linux netstat Parser"""
|
||||||
import string
|
import string
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
def normalize_headers(header):
|
def normalize_headers(header):
|
||||||
@ -38,7 +39,7 @@ def parse_network(headers, entry):
|
|||||||
]
|
]
|
||||||
|
|
||||||
# split entry based on presence of value in "State" column
|
# split entry based on presence of value in "State" column
|
||||||
contains_state = any(state in entry for state in LIST_OF_STATES)
|
contains_state = any(re.search(rf"\b{re.escape(state)}\b", entry) for state in LIST_OF_STATES)
|
||||||
split_modifier = 1 if contains_state else 2
|
split_modifier = 1 if contains_state else 2
|
||||||
entry = entry.split(maxsplit=len(headers) - split_modifier)
|
entry = entry.split(maxsplit=len(headers) - split_modifier)
|
||||||
|
|
||||||
|
1
tests/fixtures/ubuntu-18.04/netstat-sudo-lnp-space.json
vendored
Normal file
1
tests/fixtures/ubuntu-18.04/netstat-sudo-lnp-space.json
vendored
Normal file
File diff suppressed because one or more lines are too long
51
tests/fixtures/ubuntu-18.04/netstat-sudo-lnp-space.out
vendored
Normal file
51
tests/fixtures/ubuntu-18.04/netstat-sudo-lnp-space.out
vendored
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
Active Internet connections (only servers)
|
||||||
|
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
|
||||||
|
tcp 0 0 0.0.0.0:9898 0.0.0.0:* LISTEN 1178/pgpool
|
||||||
|
tcp 0 0 192.168.68.116:9102 0.0.0.0:* LISTEN 584/bareos-fd
|
||||||
|
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 600/sshd: /usr/sbin
|
||||||
|
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 1178/pgpool
|
||||||
|
tcp 0 0 0.0.0.0:5433 0.0.0.0:* LISTEN 1676/postgres
|
||||||
|
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 928/exim4
|
||||||
|
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 1817931/zabbix_agen
|
||||||
|
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 1187/pgpool: watchd
|
||||||
|
tcp6 0 0 :::9898 :::* LISTEN 1178/pgpool
|
||||||
|
tcp6 0 0 :::22 :::* LISTEN 600/sshd: /usr/sbin
|
||||||
|
tcp6 0 0 :::5432 :::* LISTEN 1178/pgpool
|
||||||
|
tcp6 0 0 :::5433 :::* LISTEN 1676/postgres
|
||||||
|
tcp6 0 0 :::10050 :::* LISTEN 1817931/zabbix_agen
|
||||||
|
udp 0 0 0.0.0.0:68 0.0.0.0:* 535/dhclient
|
||||||
|
udp 0 0 0.0.0.0:37300 0.0.0.0:* 1204/pgpool: heartb
|
||||||
|
udp 0 0 0.0.0.0:9694 0.0.0.0:* 1205/pgpool: heartb
|
||||||
|
udp 0 0 0.0.0.0:9694 0.0.0.0:* 1203/pgpool: heartb
|
||||||
|
udp 0 0 0.0.0.0:44649 0.0.0.0:* 1206/pgpool: heartb
|
||||||
|
udp 0 0 0.0.0.0:52868 0.0.0.0:* 494/rsyslogd
|
||||||
|
tcp 0 0 127.0.0.1:42351 0.0.0.0:* LISTEN 1112/containerd
|
||||||
|
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 885/systemd-resolve
|
||||||
|
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1127/sshd
|
||||||
|
tcp6 0 0 :::22 :::* LISTEN 1127/sshd
|
||||||
|
udp 0 0 127.0.0.53:53 0.0.0.0:* 885/systemd-resolve
|
||||||
|
udp 0 0 192.168.71.131:68 0.0.0.0:* 867/systemd-network
|
||||||
|
raw6 0 0 :::58 :::* 7 867/systemd-network
|
||||||
|
Active UNIX domain sockets (only servers)
|
||||||
|
Proto RefCnt Flags Type State I-Node PID/Program name Path
|
||||||
|
unix 2 [ ACC ] SEQPACKET LISTENING 20812 1/init /run/udev/control
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 33765 1723/systemd /run/user/1000/systemd/private
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 33808 1723/systemd /run/user/1000/gnupg/S.gpg-agent.ssh
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 33809 1723/systemd /run/user/1000/gnupg/S.dirmngr
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 33810 1723/systemd /run/user/1000/gnupg/S.gpg-agent.browser
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 33811 1723/systemd /run/user/1000/gnupg/S.gpg-agent
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 33812 1723/systemd /run/user/1000/gnupg/S.gpg-agent.extra
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 20655 1/init /run/systemd/private
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 20662 1/init /run/lvm/lvmetad.socket
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 20664 1/init /run/systemd/journal/stdout
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 20891 1/init /run/lvm/lvmpolld.socket
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 27473 1/init /run/acpid.socket
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 27443 1/init /run/snapd.socket
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 27445 1/init /run/snapd-snap.socket
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 27475 1/init /run/uuidd/request
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 27481 1/init /var/run/docker.sock
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 27489 1/init /var/run/dbus/system_bus_socket
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 27468 1/init /var/lib/lxd/unix.socket
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 30726 1112/containerd /run/containerd/containerd.sock
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 27436 1/init @ISCSIADM_ABSTRACT_NAMESPACE
|
||||||
|
unix 2 [ ACC ] STREAM LISTENING 25548 607/VGAuthService /var/run/vmware/guestServicePipe
|
@ -37,6 +37,9 @@ class MyTests(unittest.TestCase):
|
|||||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat-sudo-lnp.out'), 'r', encoding='utf-8') as f:
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat-sudo-lnp.out'), 'r', encoding='utf-8') as f:
|
||||||
ubuntu_18_4_netstat_sudo_lnp = f.read()
|
ubuntu_18_4_netstat_sudo_lnp = f.read()
|
||||||
|
|
||||||
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat-sudo-lnp-space.out'), 'r', encoding='utf-8') as f:
|
||||||
|
ubuntu_18_4_netstat_sudo_lnp_space = f.read()
|
||||||
|
|
||||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/netstat-sudo-aeep.out'), 'r', encoding='utf-8') as f:
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/netstat-sudo-aeep.out'), 'r', encoding='utf-8') as f:
|
||||||
centos_7_7_netstat_sudo_aeep = f.read()
|
centos_7_7_netstat_sudo_aeep = f.read()
|
||||||
|
|
||||||
@ -164,6 +167,9 @@ class MyTests(unittest.TestCase):
|
|||||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat-sudo-lnp.json'), 'r', encoding='utf-8') as f:
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat-sudo-lnp.json'), 'r', encoding='utf-8') as f:
|
||||||
ubuntu_18_4_netstat_sudo_lnp_json = json.loads(f.read())
|
ubuntu_18_4_netstat_sudo_lnp_json = json.loads(f.read())
|
||||||
|
|
||||||
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat-sudo-lnp-space.json'), 'r', encoding='utf-8') as f:
|
||||||
|
ubuntu_18_4_netstat_sudo_lnp_space_json = json.loads(f.read())
|
||||||
|
|
||||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/netstat-sudo-aeep.json'), 'r', encoding='utf-8') as f:
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/netstat-sudo-aeep.json'), 'r', encoding='utf-8') as f:
|
||||||
centos_7_7_netstat_sudo_aeep_json = json.loads(f.read())
|
centos_7_7_netstat_sudo_aeep_json = json.loads(f.read())
|
||||||
|
|
||||||
@ -317,6 +323,12 @@ class MyTests(unittest.TestCase):
|
|||||||
"""
|
"""
|
||||||
self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat_sudo_lnp, quiet=True), self.ubuntu_18_4_netstat_sudo_lnp_json)
|
self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat_sudo_lnp, quiet=True), self.ubuntu_18_4_netstat_sudo_lnp_json)
|
||||||
|
|
||||||
|
def test_netstat_sudo_lnp_ubuntu_18_4(self):
|
||||||
|
"""
|
||||||
|
Test 'sudo netstat -lnp' on Ubuntu 18.4 with a space in the process name(special case)
|
||||||
|
"""
|
||||||
|
self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat_sudo_lnp_space, quiet=True), self.ubuntu_18_4_netstat_sudo_lnp_space_json)
|
||||||
|
|
||||||
def test_netstat_sudo_aeep_centos_7_7(self):
|
def test_netstat_sudo_aeep_centos_7_7(self):
|
||||||
"""
|
"""
|
||||||
Test 'sudo netstat -aeep' on Centos 7.7
|
Test 'sudo netstat -aeep' on Centos 7.7
|
||||||
|
Reference in New Issue
Block a user