Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
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

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.

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

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
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
Reply

Tags
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 01:27 AM.