VanDyke Software Forums

VanDyke Software Forums (
-   Scripting (
-   -   Python writing to file in SecureCRT (

v777201 08-08-2019 08:31 AM

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('%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.Send(row["User"] + '\r')
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

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


bgagnon 08-08-2019 08:36 AM

Hi v777201,

Sounds to me like you are unnecessarily re-inventing the wheel. :)

See the example script in this forum post.

All times are GMT -6. The time now is 09:04 AM.