View Single Post
  #1  
Old 08-08-2019, 08:31 AM
v777201 v777201 is offline
Registered User
 
Join Date: Aug 2019
Posts: 1
Python writing to file in SecureCRT

New to Python and scripting in general. I'm writing a script that I am connecting to a host, looping through some commands and disconnecting. Then connecting to the next host and running through the same commands...I would like to capture the results of those commands to a file. I create a file and open it for capture but I can't figure out what is needed to get the data to save to the file. Code is below.

import SecureCRT
import csv
import time
from datetime import datetime

strPrompt = ">"
commands = ["show alarms\r", "exit\r"]


def main():
with open ("C:/_user_Scripts/Python_Files/OLTs.csv") as f: # open host file
OLTReader = csv.DictReader(f)
for row in OLTReader:
outfile_name = str(datetime.now().strftime('%Y%m%d%H%M%S')) + "_log_%s" %(row["CLLI"]) #name capture file
crt.Screen.Send("telnet " + row["IP_Address"] + '\r') # connect to device
with open ("C:/_user_Scripts/Python_Files/userpw.csv") as userpw:
userpw = csv.DictReader(userpw)
for row in userpw: # Login to device calling user/pw from file
crt.Screen.WaitForString("sername:")
crt.Screen.Send(row["User"] + '\r')
crt.Screen.WaitForString("assword:")
crt.Screen.Send(row["Password"] + '\r')
with open(outfile_name, "w") as file: # open log file
for command in commands: # loop through commands and write output to file
#file.write()
crt.Screen.Send(command)
crt.Screen.WaitForString(strPrompt)
time.sleep(0.5)
main()

any input you can provide is greatly appreciated. the file.write() is commented out and where i'm getting hemmed up.

thanks
Reply With Quote