VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Create filename with a variable python (https://forums.vandyke.com/showthread.php?t=13960)

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

Code:

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

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():
Code:

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:
Code:

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.