VanDyke Software Forums

VanDyke Software Forums (
-   Scripting (
-   -   Create filename with a variable python (

JoseLuis 11-01-2019 02:41 AM

Create filename with a variable python
Hell everyone. This is my first post hope I am doing it right. I am trying to read from Cisco devices the hostname and then open a txt file with the name of the host. This is the code I am using.
The crt version I am using is 7.2.1. Python is version 3.7.2


import os
def main():
        hostname = crt.Screen.ReadString("#",10)
        filename = os.path.join("C:/Users/" + str(hostname) + ".txt")
        fp = open(filename, "wb+")
        crt.Screen.Synchronous = False

The code above works fine if I substitute str(hostname) by any string. What am I doing wrong.
Many thanks for your support.

berdmann 11-01-2019 12:24 PM

Hi JoseLuis,

The first troubleshooting step is to determine if Synchronous is true for the Screen object. You can do this by inserting a line similar to the following as the first line of main():

crt.Dialog.MessageBox("sync: {0}".format(str(crt.Screen.Synchronous)))
If Synchronous is true the next step is to recognize that nothing gets displayed to the screen until a call to ReadString() or WaitFor*() is made. Because of this, the "\r" that you sent prior to the ReadString() call will be echoed by the remote shell, back to SecureCRT, such that it will be included in the data returned by the ReadString() call. In fact, what will be echoed by the remote would most likely be a "\r\n" sequence.

Neither "\r" nor "\n" are characters that will be found in your path or filename, which likely explains the failure that you are experiencing.

To sanitize the hostname that is read in of all of the potential "\r" and "\n" characters, you would want to use something like this once you have read in the hostname:

hostname = hostname.replace("\r", "")
hostname = hostname.replace("\n", "")

JoseLuis 11-02-2019 03:39 AM

Thank you very much. It is working.

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