-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheasy_telnet.py
55 lines (45 loc) · 1.25 KB
/
easy_telnet.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Easy telnet client for Pycom.
Fire up a telnet connection between your pc and Lopy/Wipy with a simple
script.
No need to install additional programs (besides Python itself).
"""
import telnetlib
import time
import sys
import msvcrt
__author__ = "Rob Braggaar"
__license__ = "CC-BY-SA"
sys.ps1 = ''
sys.ps2 = ''
# timeout value for blocking operations [s]
TO = 5
# host, 192.168.4.1 by default
host = "192.168.4.1"
# username and password for telnet
username = "micro"
password = "python"
# create telnet object
tel = telnetlib.Telnet(host, port=23, timeout=TO)
# login process
print tel.read_until("Login as: ")
print username
tel.write(username + "\r\n")
print tel.read_until("Password: ", timeout=TO)
print ''
time.sleep(1)
tel.write(password + "\r\n")
time.sleep(.5)
print tel.read_until(">>> ", timeout=TO).strip('>>> ')
# receive commands from the user as input
# send and execute commands to the pycom device and return the result
while True:
indent = ' '
cmd = raw_input('>>> ')
if len(cmd) > 1:
while cmd[-1] == ':':
cmd += '\n' + indent + raw_input('... ' + indent)
indent += ' '
tel.write(cmd + '\r\n')
time.sleep(.5)
print (tel.read_until(">>> ", timeout=1).strip('>>> ' + cmd).strip('\r\n'))