Skip to content

Instantly share code, notes, and snippets.

@klange
Last active August 17, 2017 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klange/1b87a4ae2b9ebcf9ba0d302c551498d7 to your computer and use it in GitHub Desktop.
Save klange/1b87a4ae2b9ebcf9ba0d302c551498d7 to your computer and use it in GitHub Desktop.
import sys
import os
import termios
import tty
from fswait import fswait
telnet_mode = False
i = 1
while sys.argv[i].startswith('-'):
if sys.argv[i] == '-t':
# Telnet mode
telnet_mode = True
i += 1
if telnet_mode:
fd = sys.stdin.fileno()
new = termios.tcgetattr(fd)
new[3] &= ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, new)
tty.setraw(fd)
with open(f'/dev/net/{sys.argv[i]}:{sys.argv[i+1]}','br+') as f:
while 1:
r = fswait([f, sys.stdin])
if r == 0:
data = os.read(f.fileno(), 1024)
os.write(sys.stdout.fileno(), data)
sys.stdout.flush()
elif r == 1:
data = os.read(sys.stdin.fileno(), 1024)
if not data:
break
if data == '\003':
break
if telnet_mode:
data = data.replace(b'\n',b'\r\n')
os.write(f.fileno(), data)
f.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment