Welcome to the VanDyke Software Forums

Join the discussion today!

Go Back   VanDyke Software Forums > Scripting

Thread Tools Rate Thread Display Modes
Old 11-01-2019, 03:41 AM
JoseLuis JoseLuis is offline
Registered User
Join Date: Oct 2019
Posts: 2
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.

Last edited by berdmann; 11-01-2019 at 12:51 PM.
Reply With Quote
Old 11-01-2019, 01:24 PM
berdmann berdmann is offline
VanDyke Technical Support
Join Date: Aug 2017
Posts: 386
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", "")

VanDyke Software
Technical Support
(505) 332-5730
Reply With Quote
Old 11-02-2019, 04:39 AM
JoseLuis JoseLuis is offline
Registered User
Join Date: Oct 2019
Posts: 2
Thank you very much. It is working.
Reply With Quote

filename , python , variable

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -6. The time now is 08:47 AM.