Initial commit
This commit is contained in:
Executable
+58
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run commands in the stick's root shell over telnet (idle-tolerant, waits for prompt).
|
||||
Usage: gpon_cmd.py [--idle S] [--cap S] 'cmd1' 'cmd2' ..."""
|
||||
import socket, sys, time, re
|
||||
from gpon_env import HOST, PORT, USER, PASS
|
||||
args=sys.argv[1:]; idle=8.0; cap=120.0; cmds=[]
|
||||
while args:
|
||||
a=args.pop(0)
|
||||
if a=='--idle': idle=float(args.pop(0))
|
||||
elif a=='--cap': cap=float(args.pop(0))
|
||||
else: cmds.append(a)
|
||||
|
||||
def strip_iac(data,outbuf):
|
||||
res=bytearray(); i=0
|
||||
while i<len(data):
|
||||
b=data[i]
|
||||
if b==0xFF and i+1<len(data):
|
||||
c=data[i+1]
|
||||
if c in (251,252,253,254) and i+2<len(data):
|
||||
o=data[i+2]
|
||||
if c==253: outbuf.append(bytes([255,252,o]))
|
||||
elif c==251: outbuf.append(bytes([255,254,o]))
|
||||
i+=3; continue
|
||||
elif c==250:
|
||||
j=data.find(b'\xff\xf0',i+2); i=len(data) if j<0 else j+2; continue
|
||||
else: i+=2; continue
|
||||
res.append(b); i+=1
|
||||
return bytes(res)
|
||||
|
||||
s=socket.create_connection((HOST,PORT),timeout=8)
|
||||
def rx(idle,cap,stop=None):
|
||||
s.settimeout(0.5); buf=bytearray(); last=time.time(); start=time.time()
|
||||
while True:
|
||||
try:
|
||||
d=s.recv(16384)
|
||||
if not d: break
|
||||
ob=[]; buf.extend(strip_iac(d,ob))
|
||||
for o in ob: s.sendall(o)
|
||||
last=time.time()
|
||||
if stop and stop(buf): break
|
||||
except socket.timeout:
|
||||
if time.time()-last>idle: break
|
||||
if time.time()-start>cap: break
|
||||
return buf.decode('latin-1','replace')
|
||||
def send(x): s.sendall(x.encode()+b'\r\n')
|
||||
|
||||
o=rx(2,8)
|
||||
if re.search(r'(ogin|sername)',o): send(USER); o+=rx(2,6)
|
||||
if re.search(r'assword',o,re.I): send(PASS); o+=rx(2,6)
|
||||
send('system'); rx(1.5,5)
|
||||
send('shell'); rx(1.5,5)
|
||||
promptend=lambda b: b.rstrip().endswith(b'shell>')
|
||||
for c in cmds:
|
||||
send(c)
|
||||
out=rx(idle,cap,stop=promptend)
|
||||
print("\n$ %s\n%s"%(c,out))
|
||||
try: s.close()
|
||||
except Exception: pass
|
||||
Reference in New Issue
Block a user