2015-05-07 22 views
1

再次,尋求幫助。但是,在我開始之前,這裏會有很多文字,請爲此抱歉。 我有大約500個IP地址與設備2x分類.xlsx書 我想: 遠程登錄到設備。檢查設備(通過認證的提示)1型或2型Python 3,從文件問題中提取信息

如果設備是1型 - 得到它的固件版本在2個分區中的Excel文件 寫: 列1 - IP地址 列2 - 設備類型 列3 - 固件版本 第4列 - 保留分區中的固件版本。

如果2型 - 在Excel文件寫: - IP地址 柱2 - 塔1的設備類型

如果設備是向下,或設備3型(未知) - 寫在Excel文件: 第1列 - IP地址 第2列 - 結果(EOF,TIMEOUT)

我做了什麼:我能夠telnet到設備,檢查設備類型,在excel中寫入2列(1列IP地址,2列是設備類型,或EOF/TIMEOUT結果) 而且,我正在將會話中的完整日誌以IP_ADDRESS.txt格式寫入文件以供將來診斷。

我不明白要做什麼?我無法理解如何獲取固件版本,並將其放在3,4列上。 我無法理解如何實時使用當前的日誌會話,所以我決定將日誌從主文件(IP_ADDRESS.txt)複製到temp.txt中以使用它。 我無法理解如何提取我需要的信息。 文件輸出例如:

Trying 10.40.81.167... 

    Connected to 10.40.81.167. 

    Escape character is '^]'. 



    #################################### 
    #         # 
    # RADIUS authorization disabled # 
    # Enter local login/password  # 
    #         # 
    #################################### 
    bt6000 login: admin 
    Password: 
    Please, fill controller information at first time (Ctrl+C to abort): 
    ^C 
    Controller information filling canceled. 
    ^[email protected]# firmware info 
    Active boot partition: 1 
    Partition 0 (reserved): 
      Firmware:  Energomera-2.3.1 
      Version:  10117 
    Partition 1 (active): 
      Firmware:  Energomera-2.3.1_01.04.15c 
      Version:  10404M 
    Kernel version: 2.6.38.8 #2 Mon Mar 2 20:41:26 MSK 2015 
    STM32: 
      Version:  bt6000 10083 
      Part Number: BT6024 
      Updated:  27.04.2015 16:43:50 
    [email protected]# 

我需要值 - 後「Energomera」詞語,像2.3.1保留分區,並且爲2.3.1_01.04.15c活動分區。 我試圖使用字符串數字和excract字符串,但沒有任何一種好的結果。

下面我的腳本的完整代碼。

import pexpect 
    import pxssh 
    import sys   #hz module 
    import re   #Parser module 
    import os   #hz module 
    import getopt 
    import glob      #hz module 
    import xlrd      #Excel read module 
    import xlwt      #Excel write module 
    import telnetlib    #telnet module 
    import shutil 

    #open excel book 
    rb = xlrd.open_workbook('/samba/allaccess/Energomera_Eltek_list.xlsx') 
    #select work sheet 
    sheet = rb.sheet_by_name('IPs') 
    #rows number in sheet 
    num_rows = sheet.nrows 
    #cols number in sheet 
    num_cols = sheet.ncols 
    #creating massive with IP addresses inside 
    ip_addr_list = [sheet.row_values(rawnum)[0] for rawnum in range(sheet.nrows)] 
    #create excel workbook with write permissions (xlwt module) 
    wb = xlwt.Workbook() 
    #create sheet IP LIST with cell overwrite rights 
    ws = wb.add_sheet('IP LIST', cell_overwrite_ok=True) 
    #create counter 
    i = 0 
    #authorization details 
    port = "23"        #telnet port 
    user = "admin"       #telnet username 
    password = "12345"     #telnet password 

    #firmware ask function 
    def fw_info(): 
     print('asking for firmware') 
     px.sendline('firmware info') 
     px.expect('bt6000#') 

    #firmware update function 
    def fw_send(): 
     print('sending firmware') 
     px.sendline('tftp server 172.27.2.21') 
     px.expect('bt6000') 
     px.sendline('firmware download tftp firmware.ext2') 
     px.expect('Updating') 
     px.sendline('y') 
     px.send(chr(13)) 
     ws.write(i, 0, host) 
     ws.write(i, 1, 'Energomera') 

    #if eltek found - skip, write result in book 
    def eltek_found(): 
     print(host, "is Eltek. Skipping") 
     ws.write(i, 0, host) 
     ws.write(i, 1, 'Eltek') 

    #if 23 port telnet conn. refused - skip, write result in book 
    def conn_refuse(): 
     print(host, "connection refused") 
     ws.write(i, 0, host) 
     ws.write(i, 1, 'Connection refused') 

    #auth function 
    def auth(): 
     print(host, "is up! Energomera found. Starting auth process") 
     px.sendline(user) 
     px.expect('assword') 
     px.sendline(password) 

    #start working with ip addresses in ip_addr_list massive 
    for host in ip_addr_list: 
    #spawn pexpect connection 
     px = pexpect.spawn('telnet ' + host) 
     px.timeout = 35 
     #create log file with in IP.txt format (10.1.1.1.txt, for example) 
     fout = open('/samba/allaccess/Energomera_Eltek/{0}.txt'.format(host),"wb") 
     #push pexpect logfile_read output to log file 
     px.logfile_read = fout 
     try: 
      index = px.expect (['bt6000', 'sername', 'refused']) 
      #if device tell us bt6000 - authorize   
      if index == 0: 
       auth() 
       index1 = px.expect(['#', 'lease']) 
       #if "#" - ask fw version immediatly 
       if index1 == 0: 
        print('seems to controller ID already set') 
        fw_info() 
       #if "Please" - press 2 times Ctrl+C, then ask fw version 
       elif index1 == 1: 
        print('trying control C controller ID') 
        px.send(chr(3)) 
        px.send(chr(3)) 
        px.expect('bt6000') 
        fw_info() 
    #firmware update start (temporarily off) 
    #   fw_send() 

    #Eltek found - func start 
      elif index == 1: 
       eltek_found() 
    #Conn refused - func start 
      elif index == 2: 
       conn_refuse() 
       #print output to console (test purposes) 
       print(px.before) 
      px.send(chr(13)) 
    #Copy from current log file to temp.txt for editing 
      shutil.copy2('/samba/allaccess/Energomera_Eltek/{0}.txt'.format(host), '/home/bark/expect/temp.txt') 
    #EOF result - skip host, write result to excel 
     except pexpect.EOF: 
      print(host, "EOF") 
      ws.write(i, 0, host) 
      ws.write(i, 1, 'EOF') 
      #print output to console (test purposes) 
      print(px.before) 
    #Timeout result - skip host, write result to excel 
     except pexpect.TIMEOUT: 
      print(host, "TIMEOUT") 
      ws.write(i, 0, host) 
      ws.write(i, 1, 'TIMEOUT') 
      #print output to console (test purposes) 
      print(px.before) 
      #Copy from current log file to temp.txt for editing 
      shutil.copy2('/samba/allaccess/Energomera_Eltek/{0}.txt'.format(host), '/home/bark/expect/temp.txt') 
      #count +1 to correct output for Excel 
     i += 1 
    #workbook save 
    wb.save('/samba/allaccess/Energomera_Eltek_result.xls')   

您有任何建議或想法,傢伙,我該怎麼做? 任何幫助,不勝感激。

回答

2

您可以使用regular expressions

例如:

>>> import re 
>>> 
>>> str = """ 
... Trying 10.40.81.167... 
... 
...  Connected to 10.40.81.167. 
... 
...  Escape character is '^]'. 
... 
... 
... 
...  #################################### 
...  #         # 
...  # RADIUS authorization disabled # 
...  # Enter local login/password  # 
...  #         # 
...  #################################### 
...  bt6000 login: admin 
...  Password: 
...  Please, fill controller information at first time (Ctrl+C to abort): 
...  ^C 
...  Controller information filling canceled. 
...  ^[email protected]# firmware info 
...  Active boot partition: 1 
...  Partition 0 (reserved): 
...    Firmware:  Energomera-2.3.1 
...    Version:  10117 
...  Partition 1 (active): 
...    Firmware:  Energomera-2.3.1_01.04.15c 
...    Version:  10404M 
...  Kernel version: 2.6.38.8 #2 Mon Mar 2 20:41:26 MSK 2015 
...  STM32: 
...    Version:  bt6000 10083 
...    Part Number: BT6024 
...    Updated:  27.04.2015 16:43:50 
...  [email protected]# 
... """ 
>>> re.findall(r"Firmware:.*?([0-9].*)\s", str) 
['2.3.1', '2.3.1_01.04.15c'] 

>>> reserved_firmware = re.search(r"reserved.*\s*Firmware:.*?([0-9].*)\s", str).group(1) 
>>> reserved_firmware 
'2.3.1' 
>>> active_firmware = re.search(r"active.*\s*Firmware:.*?([0-9].*)\s", str).group(1) 
>>> active_firmware 
'2.3.1_01.04.15c' 
>>> 
+0

工作就像一個魅力。萬分感謝!看更多的正則表達式手冊頁,非常有趣的可能性。 –