By telnet automatically download the configuration file cfg

Source code is as follows:

 

import telnetlib
import logging
import datetime
import csv
import time


def telnetfunc(enbid, host, user, passwd):

    try:
        tn = telnetlib.Telnet(host)
        tn.read_until(b'login: ')
        tn.write(user.encode('ascii') + b'\n')
    
        tn.read_until(b'Password: ')
        tn.write(passwd.encode('ascii') + b'\n')
        
        timestr = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        command = 'tftp -l /ata2/VER/CFG/cur.cfg -r %s-%s.cfg -p 10.204.195.129'%(enbid, timestr)
        tn.read_until(b'#')
        print('登录成功')
        tn.write(command.encode('ascii')+b'\n')
    
        tn.read_until(b'#')
        tn.close ()
        True return 

    the except: 
        logging.warning ( '% S Network failure' Host%) 
        return False 

DEF csvloop (filename): 
    with Open (filename, NEWLINE = '') AS CSV_File: 
        csv_table = csv.reader (CSV_File, DELIMITER = ' , ') 
        Next (csv_table) 
        for eNB in csv_table: 
            Print ( "% d uploading stations profile"% (-csv_table.line_num. 1), eNB [0]) 
            telnetfunc (eNB [2], eNB [. 3 ], eNB [. 4], eNB [. 5]) 
            the time.sleep (2) 

IF the __name__ == '__main__': 

    csvloop ( 'D: /yanfa/py/197IP.csv')

  

csv configuration file format:

 

Guess you like

Origin www.cnblogs.com/wangziyi0513/p/10980195.html
cfg