-
Notifications
You must be signed in to change notification settings - Fork 0
/
digi.py
executable file
·64 lines (61 loc) · 2.09 KB
/
digi.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
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import logging
import shutil
import telnetlib
import re
import time
import sys
import os
import getopt
from optparse import OptionParser
# Define the logging module
############################################################################################################
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-6s %(message)s',
datefmt='%m-%d-%Y %H:%M:%S',
filename='/tmp/digi-port-server.log',
filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(levelname)-6s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
############################################################################################################
def functionCheck():
# Connect to ....
tn = telnetlib.Telnet('192.168.254.254',23)
#tn.set_debuglevel(2) # Debug mode
# username / password
#login: root
#password:
#> help
tn.expect([re.compile(b"login: "),])
tn.write("root".encode('ascii') + b"\r\n")
tn.expect([re.compile(b"password: "),])
tn.write("dbps".encode('ascii') + b"\r\n")
time.sleep(.5)
# Settings
cmd_list = ('show config','show line','show profile')
for i in cmd_list:
if i == 'rn':
tn.write(b"\r\n")
time.sleep(2)
else:
print("start to run command "+ "\033[31m" + i + "\033[0m" )
tn.write(i.encode('ascii') + b"\r\n")
time.sleep(2)
result=tn.read_very_eager().decode('ascii')
tcpserver=re.compile('tcpsockets')
tcp_ser = 0
if tcpserver.search(result):
tcp_ser = 1
if tcp_ser:
print("\033[32mwe found the tcp server mode is setup now\033[0m")
#print(result)
logging.debug(result)
#print(tn.read_very_eager().decode('ascii'))
# result = tn.read_all().decode('ascii')
# logging.debug(result)
print("close the telnet session now")
tn.close()
functionCheck()