-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell2udp.py
32 lines (24 loc) · 931 Bytes
/
shell2udp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import socket
from subprocess import Popen
from argparse import ArgumentParser
def serve():
parser = ArgumentParser()
parser.add_argument('-p', '--port', default=8080, type=int)
parser.add_argument('command', nargs='+')
args = parser.parse_args()
if len(args.command) % 2:
args.command = [''] + args.command
routes = {}
for path, command in zip(args.command[0::2], args.command[1::2]):
print(f'echo {path} > /dev/udp/localhost/{args.port}', '#', command)
routes[path.encode('UTF-8')] = command
routes[f'{path}\n'.encode('UTF-8')] = command
with socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) as s:
s.bind(('', args.port))
while(True):
bytes, addr = s.recvfrom(1024)
print(bytes, addr)
if bytes in routes:
Popen(routes[bytes], shell=True)
if __name__ == '__main__':
serve()