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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- coding: GBK -*-
 
import os
from socket import *
import struct
import binascii
import traceback
 
#----------------------дÈë
def WriteBYTE(buf, value):
    buf += struct.pack('B', value)
    return buf
 
def WriteWORD(buf, value):
    buf += struct.pack('H', value)
    return buf
 
def WriteDWORD(buf, value):
    buf += struct.pack('I', value)
    return buf
 
def WriteFloat(buf, value):
    buf += struct.pack('f', value)
    return buf
 
def WriteDouble(buf, value):
    buf += struct.pack('d', value)
    return buf
 
def WriteString(buf, len, value):
    buf += struct.pack('%ds'%len, value)
    return buf
 
 
#------------------------¶ÁÈ¡ 
def ReadBYTE(buf, pos):
    curValue = struct.unpack_from('B', buf, pos)
    pos += 1
    return curValue[0], pos
 
def ReadWORD(buf, pos):
    curValue = struct.unpack_from('H', buf, pos)
    pos += 2  
    return curValue[0], pos
 
def ReadDWORD(buf, pos):
    curValue = struct.unpack_from('I', buf, pos)  
    pos += 4
    return curValue[0], pos
 
def ReadFloat(buf, pos):
    curValue = struct.unpack_from('f', buf, pos)  
    pos += 4
    return curValue[0], pos
 
def ReadDouble(buf, pos):
    curValue = struct.unpack_from('d', buf, pos)  
    pos += 8
    return curValue[0], pos
 
def ReadString(buf, pos, _len):
    curValue = struct.unpack_from('%ds'%_len, buf, pos)
    pos += _len  
    return curValue[0], pos
 
def PackParse(data):
    buf = data
    recvdata = ''
    pos = 0
    try:
        PackHead, pos = ReadWORD(buf, pos)
        
        if PackHead != 0xCCFF:
            print 'PackHead not the 0xCCFF'
            return data
        
        packlen, pos = ReadDWORD(buf, pos)
        if packlen + pos != len(data):
            print 'PackLen Wrong!'
            return data
        
        curPack , pos = ReadDWORD(buf, pos) #SessionID
        curPack , pos = ReadWORD(buf, pos)  #PoolIndex
        curPack , pos = ReadBYTE(buf, pos)  #Type
        curPack, pos = ReadBYTE(buf, pos)   #DestServerType
        curPack, pos = ReadBYTE(buf, pos)   #SrcServerType
        curPack , pos = ReadString(buf, pos, len(data)-pos)
        recvdata = str(curPack)
            
    except:
        print traceback.format_exc()
        return data
    
    return recvdata
 
fullfilename = ''  #±£´æRoute·µ»ØµÄÏà¹ØÐÅÏ¢µÄÎļþ
BUFSIZ = 1024
 
def GetServerVersion(sClient, fResult):
    global fullfilename
    global BUFSIZ   
    
    data = ''
    data = WriteDWORD(data,0)   #SessionID = 0
    data = WriteWORD(data,0)    #PoolIndex = 0
    data = WriteBYTE(data,2)    #Type = atInner
    data = WriteBYTE(data,5)    #DestServerType = stRoute
    data = WriteBYTE(data,6)    #SrcServerType = stMap
    data = WriteBYTE(data,37)   #TMapServerToBalanceServer = mgServerVersion
    data = WriteDWORD(data,0)
    datalen = len(data)
    head = ''
    head = WriteDWORD(head,datalen)
    data = head + data 
    __PackHead = 0xCCFF
    PackHead = ''
    PackHead = WriteWORD(PackHead, __PackHead)
    data = PackHead + data
    
    recvdata = ''
    while True:
        print 'ÇëÇó°æ±¾ÖС£¡£¡£¡£'
        sClient.send(data)
        print '½ÓÊܰ汾ÖС£¡£¡£¡£'
        dataRecv = sClient.recv(BUFSIZ)
        if not dataRecv :
            continue
        
        resultData = PackParse(dataRecv)
        #Èç¹ûÊý¾Ý²»ÍêÕû£¬ÔòÖØ·¢ÔÙ½ÓÊÕ¡£
        if resultData == dataRecv:
            print 'Êý¾Ý²»ÍêÕû,ÖØ½Ó¡£¡£¡£¡£'
            continue
        try:
            #print str(resultData)
            #print fResult
            if str(resultData)=="":
                continue
            fResult.write(str(resultData))
            print "ÇëÇó°æ±¾½á¹û[%s]"%str(resultData)
        except:
            print "Write %s failed"%fullfilename
            os.system("pause")
            return
        print "Write SoftVersion OK"
        break
        
def GetLineID(sClient, fResult):
    global fullfilename
    global BUFSIZ    
    
    data = ''
    data = WriteDWORD(data,0)   #SessionID = 0
    data = WriteWORD(data,0)    #PoolIndex = 0
    data = WriteBYTE(data,2)    #Type = atInner
    data = WriteBYTE(data,5)    #DestServerType = stRoute
    data = WriteBYTE(data,6)    #SrcServerType = stMap
    data = WriteBYTE(data,41)   #TMapServerToBalanceServer = mgLineID
    data = WriteDWORD(data,0)
    datalen = len(data)
    head = ''
    head = WriteDWORD(head,datalen)
    data = head + data 
    __PackHead = 0xCCFF
    PackHead = ''
    PackHead = WriteWORD(PackHead, __PackHead)
    data = PackHead + data
    
    recvdata = ''
    while True:
        print 'ÇëÇó·ÖÏßIDÖС£¡£¡£¡£'
        sClient.send(data)
        print '½ÓÊÜ·ÖÏßIDÖС£¡£¡£¡£'
        dataRecv = sClient.recv(BUFSIZ)
        if not dataRecv :
            continue
        
        resultData = PackParse(dataRecv)
        #Èç¹ûÊý¾Ý²»ÍêÕû£¬ÔòÖØ·¢ÔÙ½ÓÊÕ¡£
        if resultData == dataRecv:
            print 'Êý¾Ý²»ÍêÕû,ÖØ½Ó¡£¡£¡£¡£'
            continue
        try:
            #print str(resultData)
            fResult.write(str(resultData))
            print "ÇëÇó·ÖÏßID½á¹û[%s]"%str(resultData)    
        except:
            print "Write %s failed"%fullfilename
            os.system("pause")
            return
        print "Write LineID OK"
        break   
        
       
        
    
def process(IP,Port):
    global fullfilename
    
    print IP,Port
    
    if not os.path.isdir(os.getcwd()+'//RouterInfoResult'):
        os.makedirs('RouterInfoResult')
 
    try:
        fullfilename = os.getcwd()+'\\RouterInfoResult\\Result.txt'
        fileSave = open(fullfilename,'w')
    except:
        print "Can not open %s in w mode"%fullfilename
        os.system("pause")
        return
    HOST = IP
    PORT = Port
    ADDR = (HOST, PORT)
 
    try:
        tcpCliSock = socket(AF_INET, SOCK_STREAM)
        tcpCliSock.connect(ADDR)
    except:
        print "Can't Connect To Route Server. IP = %s, Port = %s"%(IP,Port)
        os.system("pause")
        return
    
    GetServerVersion(tcpCliSock, fileSave) #ÇëÇó·þÎñÆ÷°æ±¾ÐÅÏ¢
    GetLineID(tcpCliSock, fileSave) #ÇëÇó·þÎñÆ÷·ÖÏßID
    
    fileSave.close()
    tcpCliSock.close()
    return
    
 
process(os.sys.argv[1], int(os.sys.argv[2]))