#1
|
|||
|
|||
Prompt for file save location
Hey folks,
I'm trying to write a script that snags output from a shell (like a show runn or whatever) and save it to a file of my choosing. Easy enough to point the output_dir where you need it, but I'd like the script to prompt the user (me) for a file path to browse to. I know I can do this with a simple prompt "enter file path: ". I'd like to be able to browse folders instead. I've found the crt.Dialog.FileSaveDialog() to be pretty close. The only problem: It wants you to specify an actual file name. I want to be able to specify the FOLDER. In tkinter this can be done with a tk.askdirectory(). Anybody know a solution to this problem? |
#2
|
|||
|
|||
Hi garrettwilliams,
Currently, FileSaveDialog is the closest you're likely to get to the functionality which you desire. I have created an entry for you in our feature request database. We will post something here should a version of SecureCRT become available with a FolderBrowserDialog scripting object. If you would like to be notified via e-mail, please send an e-mail to support@vandyke.com referencing this forum thread, #14374.
__________________
Thanks, --Cameron VanDyke Software Technical Support support@vandyke.com (505) 332-5730 Last edited by cboyack; 12-18-2020 at 11:04 AM. Reason: Clarity |
#3
|
|||
|
|||
For anyone looking for the same thing:
The beta version, 9.0, supports using your local python3 interpreter (instead of the built into securecrt 2.7). Using that, you can import tkinter, or anything else. Easy peezy. |
#4
|
|||
|
|||
Hi garrettwilliams,
Thanks for the idea! The possibilities really do start expanding now that python 3 can be utilized with SecureCRT's v9.0 beta!
__________________
Thanks, --Cameron VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#5
|
|||
|
|||
I ran into a gotcha with tkinter. I can issue an 'askdirectory' command. (It populates a directory search dialog, saves the directory you choose into a variable). This will work once.
The second time you run this script, you'll see an error "main thread is not in main loop". This will happen every time you run it, unless you close secureCRT and reopen. This error is typically seen using tkinter in a multithreaded environment. Can anyone shed light on how that might apply to running in secureCRT? There must be some threading going on below the surface that I don't see. Which surprised me because it should be running off the Python 3 imterpreter on the local machine. Just thinking out loud here. # $language = "python3" # $interface = "1.0" from tkinter import filedialog directory = filedialog.askdirectory() |
#6
|
|||
|
|||
Thanks for the update!
It looks like there's a bug where using tkinter in a script only works for the first time, and SecureCRT has to be restarted in between scripts that invoke it (due to that multithreading issue, as SecureCRT runs scripts on a separate thread). I've asked our DEV/QA team to look into this issue further. I don't yet have any ETA as to when/if the problem might be resolved, but I will post here as updates become available. If you prefer direct email notification, send an email to support@vandyke.com and include Feature Request - Forum Thread #14374 in the subject line or use this form from the support page of our website.
__________________
Thanks, --Cameron VanDyke Software Technical Support support@vandyke.com (505) 332-5730 Last edited by cboyack; 12-30-2020 at 08:37 AM. Reason: Grammar |
#7
|
|||
|
|||
For anyone interested in the final solution, I used PyQt5 instead of Tkinter, seems to work just fine. Thanks for the help!
Code:
# $language = "python3" # $interface = "1.0" import os import re from PyQt5.QtWidgets import QFileDialog, QApplication import datetime #Scraping the prompt off the screen to use later in script. screenrow = crt.Screen.CurrentRow x = crt.Screen.Get(screenrow,1,screenrow,30).strip(" ") prompt = str(x) #Not sure what this does but I think it's important from other scripts I've seen. Just throwing it in here. :) *shrugs* crt.Screen.Synchronous = True #Issuing show terminal command so we can capture the current term length and width. crt.Screen.Send("sh term\n") crt.Screen.WaitForString("Session Timeout") #Defines the part of the screen we're gonna look at, drops it in term_info. screenrow = crt.Screen.CurrentRow - 1 term_info = crt.Screen.Get(screenrow, 1, screenrow, 30).strip(" ") #Length in group 1, Width in Group 2 y'know. Cpature them variables n stuff. re_string = r'Length: (\d+) lines, Width: (\d+)' term_length = re.search(re_string,term_info).group(1) term_width = re.search(re_string,term_info).group(2) #Wait for the prompt to come back up before we get silly. crt.Screen.WaitForString(prompt) #Determine file name and location. Prompts for user to browse to output directory. app = QApplication([]) out_dir = QFileDialog.getExistingDirectory(None, "select directory") time_n_date = str(datetime.datetime.now()).replace('.','-') time_n_date = time_n_date.replace(':','-') time_n_date = time_n_date.replace(' ','-') time_n_date = ''.join(list(time_n_date)[:-7]) filename = out_dir + '\\' + ''.join(list(prompt)[:-1]) + '-' + time_n_date + '.txt' crt.Dialog.MessageBox(filename) #Setting the term length to 0 so we don't gotta handle the --More-- stuff when we go through the running config. crt.Screen.Send("term length 0\n") crt.Screen.WaitForString(prompt) #We're gonna wait for either a new line, or our prompt back, and take different action on each. wait_strs = ['\n', prompt] with open(filename, "w+") as fp: #Send our show run, but don't start the while loop until we see that new line (indicating the command was recieved/processed) crt.Screen.Send("sh runn\n") crt.Screen.WaitForString('\n') while True: #Look for either a new line or the prompt in the first 30 characters of the current line. nextline = crt.Screen.ReadString(wait_strs, 30) if crt.Screen.MatchIndex == 1: #If we see a new line, we'll capture what's on that line and send it to our file. nextline = nextline.strip(" ") fp.write(nextline + "\n") elif crt.Screen.MatchIndex == 2: #If we see the prompt instead, we'll break out of the loop. Running config should be all read. Voila. break Last edited by cboyack; 12-30-2020 at 08:35 AM. Reason: Please use the [CODE] and [/CODE] tags to denote a script |
#8
|
|||
|
|||
Hi again garretwilliams!
Thanks for sharing your solution! I'm glad that you've found something that works in your environment! Regarding the tkinter issue itself, we don't yet have any ETA as to when/if the problem might be resolved, but we will be posting updates as they become available ![]()
__________________
Thanks, --Cameron VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|