VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Python writing to file in SecureCRT (https://forums.vandyke.com/showthread.php?t=13872)

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(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

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 03:54 PM.