-
Notifications
You must be signed in to change notification settings - Fork 3
/
telnet.py
40 lines (32 loc) · 1.06 KB
/
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
import anyio, asynctelnet
from anyio import connect_tcp
async def shell(tcp):
async with asynctelnet.TelnetClient(tcp, client=True) as stream:
gotstatus = False
while gotstatus is False:
statusreq = bytearray("input.harbor.status\n", "utf-8")
await stream.send(statusreq)
outp = await stream.receive(1024)
if not outp:
# End of File
status = None
break
elif "connected" in str(outp, "utf-8"):
gotstatus = True
status = str(outp, "utf-8")
# display all server output
# print(str(outp,'utf-8'), flush=True)
return status
# EOF
print()
async def main():
print("Connecting to telnet server...")
try:
async with await connect_tcp("localhost", 1234) as client:
status = await shell(client)
print("telnet status:", status)
return status
except:
print("Failed to connect to server")
return None
# anyio.run(main)