#!C:\Python27\python.exe
# coding: euc-kr
import time
import datetime
import sys, traceback
reload(sys)
sys.setdefaultencoding('euc-kr')
import telnetlib
import os
import re
import string
import socket
import pymysql
import Queue
import threading
import subprocess
from expect import *
from regparser import *
def v5724g_telnet(tn,telnid,telnpw,enpw, gLog,equipkind,description):
prompt = [r'\n(.*\#)']
if(not expect_send(tn,"ogin: ",telnid,gLog)) : raise ExpectError("Login")
if(not expect_send(tn,"assword: ",telnpw,gLog)) : raise ExpectError("Password")
if(not expect_send(tn,"\>",'en',gLog)) : raise ExpectError("Prompt")
if(enpw is None or enpw == '' or enpw == 'N/A') : enpw = telnpw
if(not expect_send(tn,["assword:","\#"],[enpw,""],gLog)) : raise ExpectError("Enpw")
if(not expect_only(tn,prompt,gLog)) : raise ExpectError("NoPageEnd")
prompt = re.escape(prompt[-1])
print prompt
if(not send_expect(tn,"terminal length 0",prompt,gLog)) : raise ExpectError("NoPage")
locLog = []
sys.stdout.write('!')
sys.stdout.flush()
if(not send_expect(tn,'show port statistics rmon 11/1',prompt,locLog)) : raise ExpectError("CRC")
if(not send_expect(tn,'show port statistics rmon 11/2',prompt,locLog)) : raise ExpectError("CRC")
if(not send_expect(tn,'show port statistics rmon 12/1',prompt,locLog)) : raise ExpectError("CRC")
if(not send_expect(tn,'show port statistics rmon 12/2',prompt,locLog)) : raise ExpectError("CRC")
## Config MODE INIT ###
# if(not send_expect(tn,"conf t","\(config\)\#",gLog)) : raise ExpectError("ConfigMode")
# if(not send_expect(tn,"module ddm 12/1 enable","\(config\)\#",gLog)) : raise ExpectError("ConfigMode")
# if(not send_expect(tn,"module ddm 12/2 enable","\(config\)\#",gLog)) : raise ExpectError("ConfigMode")
# if(not send_expect(tn,"exit",prompt,gLog)) : raise ExpectError("ConfigExit")
# if(not send_expect(tn,'show port module-info 12/1',prompt,locLog)) : raise ExpectError("OPTICALVALUE")
# if(not send_expect(tn,'show port module-info 12/2',prompt,locLog)) : raise ExpectError("OPTICALVALUE")
gLog.extend(locLog)
### EXIT ###
if(not send_expect(tn,"no terminal length",prompt,gLog)) : raise ExpectError("TermLength")
if(not send_only(tn,'exit',gLog)) : raise ExpectError("LastPrompt")
#print ''.join(locLog)
return ''.join(locLog)
class ExpectError(Exception):
def __init__(self,value):
self.value=value
def __str__(self):
return repr(self.value)
def expect_send(tn, expectStr, sendStr,logList):
if sendStr is None: sendStr = ''
if type(expectStr) is str : expectStr = [expectStr]
elif type(expectStr) is list : pass
(midx,mobj,output) = tn.expect(expectStr,10)
logList.append(output)
if mobj is not None :
if type(sendStr) is list :
tn.write(sendStr[midx]+'\r')
else :
tn.write(sendStr+'\r')
if len(mobj.groups())>0 : expectStr.append (mobj.group(1))
else :expectStr.append (mobj.group(0))
return True
# on Failure
peer = tn.get_socket().getpeername()
print peer[0] + " es expect fail!\n"
tn.close()
return False
def expect_only(tn, expectStr, logList):
if type(expectStr) is str : expectStr = [expectStr]
elif type(expectStr) is list : pass
(midx,mobj,output) = tn.expect(expectStr,10)
logList.append(output)
if mobj is not None :
#print "se matching object is %s %s" % (midx,mobj.groups())
if len(mobj.groups())>0 : expectStr.append (mobj.group(1))
else :expectStr.append (mobj.group(0))
return True
# on Failure
peer = tn.get_socket().getpeername()
print peer[0] + " eo expect fail!\n"
tn.close()
return False
def send_expect(tn, sendStr, expectStr,logList):
if sendStr is None: sendStr = ''
if type(expectStr) is list : expectStr.insert(0,r'--More--').insert(0,r'Press any key')
elif type(expectStr) is str : expectStr = [r'Press any key',r'--More--',expectStr]
tn.write(sendStr+'\r')
while True :
(midx,mobj,output)=tn.expect(expectStr,120)
logList.append(output)
if midx==0 or midx==1: tn.write(' ')
elif midx > 1 :
print "se matching object is %s %s" % (midx,mobj.group(0))
if len(mobj.groups())>0 : expectStr.append (mobj.groups()[-1])
else :expectStr.append (mobj.group(0))
return True
else :
break;
# on Failure
peer = tn.get_socket().getpeername()
print peer[0] + " se expect fail!\n"
tn.close()
return False
def send_only(tn, sendStr,logList):
if sendStr is None:
sendStr = ''
tn.write(sendStr+'\r')
logList.append(sendStr)
#tn.close()
return True