summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdi Feschiyan <edi.feschiyan@konsulko.com>2020-04-02 03:01:17 +0300
committerEdi Feschiyan <edi.feschiyan@konsulko.com>2020-04-02 03:01:17 +0300
commit5552456d88ab3496435810d88c1a202c992553ed (patch)
treeafe9c18f23b1545d26246cbe69554a719f5ba626
parentc2435b3b07f7dc9125180dc77bfddbee9399ef57 (diff)
working implementation of finding out a socket and port
-rw-r--r--gps.py72
1 files changed, 47 insertions, 25 deletions
diff --git a/gps.py b/gps.py
index fa121e5..6847a8b 100644
--- a/gps.py
+++ b/gps.py
@@ -1,12 +1,9 @@
import asyncio
import os
-import re
-from socket import htons
import asyncssh
from aglbaseservice import AGLBaseService
-import collections
-from collections import namedtuple
from parse import *
+import re
class GPSService(AGLBaseService):
def __init__(self, ip, port):
@@ -27,37 +24,62 @@ async def main(loop):
port = os.environ.get('AGL_TGT_PORT', '30011')
# gpss = await GPSService(ip=addr, port=port)
- async with asyncssh.connect(addr,username='root') as c:
+ async with asyncssh.connect(addr, username='root') as c:
# find the name of the service since it is dynamically generated every time
- servicename = await c.run(f"systemctl | grep agl-service-gps | awk '{{print $1}}'", check=False)
- print(f"Found service name: {servicename.stdout}")
+ #TODO CHANGE ME to use the name of the service dynamically after cleaning this crap here
+ servicestr = 'agl-service-gps'
+ servicename = await c.run(f"systemctl | grep {servicestr} | awk '{{print $1}}'", check=False)
+ #TODO decide what to do if the service is not started - scan for disabled units/run service via afm-util
+ print(f"Found service name: {servicename.stdout.strip()}")
# get the pid
pid = await c.run(f'systemctl show --property MainPID --value {servicename.stdout}')
- print(f'Service PID: {pid.stdout}')
+ print(f'Service PID: {pid.stdout.strip()}')
# get all sockets in the process' fd directory and their respective inodes
sockets = await c.run(f'find /proc/{pid.stdout.strip()}/fd/ | xargs readlink | grep socket')
- inodes = re.findall('socket:\[(.*)\]', sockets.stdout)
+ inodes = frozenset(re.findall('socket:\[(.*)\]', sockets.stdout))
+
print(f"Socket inodes: {inodes}")
alltcp = await c.run('cat /proc/net/tcp')
- fieldsstr = ' '.join(alltcp.stdout.strip().splitlines()[0].strip().split()) + ' misc'
+ # fieldsstr = ' '.join(alltcp.stdout.strip().splitlines()[0].strip().split()) + ' sref_cnt memloc rto pred_sclk ack_quick congest slowstart'
+
# https://www.kernel.org/doc/Documentation/networking/proc_net_tcp.txt
- # ['sl', 'local_address', 'rem_address', 'st', 'tx_queue:rx_queue', 'tr:tm->when', 'retrnsmt', 'uid', 'timeout', 'inode', 'sref_cnt', 'memloc', 'rto', 'pred_sclk', 'ackquick', 'congest', 'slowstart' ]
- # '0: 00000000:753E 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1001 0 20062 1 0000000095c038d6 100 0 0 10 0'
- # {'sl':'0', # connection state
- # 'local_address': '00000000:753E',
- # 'rem_address':'00000000:0000',
- # 'st':'0A',
- # 'tx_queue':'00000000:00000000',
- # 'rx_queue':'00:00000000',
- # 'tr':'00000000', 'tm->when':'1001', 'retrnsmt': '0',
- # '20062 1 0000000095c038d6 100 0 0 10 0
- fields = fieldsstr.split()
-
- tcpsockets = alltcp.stdout.splitlines()[1:]
- print(tcpsockets)
+ # ['sl', 'local_address', 'rem_address', 'st', 'tx_queue:rx_queue', 'tr:tm->when', 'retrnsmt', 'uid',
+ # '0: 00000000:753E 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1001
+
+ # 'timeout', 'inode', 'sref_cnt', 'memloc', 'rto', 'pred_sclk', 'ackquick', 'congest', 'slowstart' ]
+ # 0 20062 1 0000000095c038d6 100 0 0 10 0'
+ # fields = fieldsstr.split()
+
+ fieldsstr = '{sl}: {local_address} {rem_address} {st} {tx_queue}:{rx_queue} {tr}:{tmwhen} {retrnsmt} {uid}' \
+ ' {timeout} {inode} {sref_cnt} {memloc} {rto} {pred_sclk} {ackquick} {congest} {slowstart}'
+ tcpsockets = [' '.join(l.split()) for l in alltcp.stdout.splitlines()[1:]]
+
+ # seen once an irregular line "65: 0D80A8C0:D5BE 8410A6BC:0050 06 00000000:00000000 03:000000F8 00000000 0 0 0 3 0000000083dad9fb"
+ # parsing could break at some point, because returns None and cannot be parsed
+
+ parsedtcpsockets = [parse(fieldsstr, l) for l in tcpsockets if l is not None]
+ socketinodesbythisprocess = [l for l in parsedtcpsockets if l is not None and l.named['inode'] in inodes]
+ # got dem sockets
+ # expecting >1 because the process could be listening on 8080, all api services' ports are in 30000 port range
+ for s in socketinodesbythisprocess:
+ _, port = tuple(parse('{}:{}', s['local_address']))
+ port = int(port,16)
+ if port > 30000:
+ print(f'found port {port}')
+ break
+
+
+
+ #thesocketswearelookingfor = list(filter(lambda x: ( l for l in parsed if l.named['inode'] in inodes), inodes ))
+
+ # result = parse(fieldsstr, l)
+ # if isinstance(Result, result):
+ # result.named['inode'] in inodes
+ #
+ # print(result)
# print(' '.join(alltcp.stdout.strip().splitlines()[1].strip().split()))
# result = findall('{}: {}:{} {} {} {} {} {} {} ')
@@ -65,8 +87,8 @@ async def main(loop):
# serviceport = await c.run(f'journalctl -u {servicename.stdout}')
# print(serviceport.stdout)
# matches = re.findall('Listening interface \*:(.*) \[',serviceport.stdout)
- # print(matches)
+ print("breaketh pointeth h're")
# print(await gpss.location())
# listener = loop.create_task(gpss.listener())