VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   VBS to Python (https://forums.vandyke.com/showthread.php?t=14555)

CustomX 06-03-2021 09:37 AM

VBS to Python
 
Hi, I'm new on Mac platform, but I had been using SecureCRT for a couple of years.

I have a lot of scripts than I use in SecureCRT, thats scripts basically are plain text scripts, for database administration propuse but was called by VBS function.

I'm not programmer, and I am not familiar with python language.

Somebody can help me to translate this block of code to python in order to allow to use my personal scripts in securecrt in mac?

Thanks a lot in advance regarding this and sorry in advance if this not a proper request in this thread.

Code:

# $language = "VBScript"
# $interface = "1.0"

' Script to open a text file and read it line by
' line to a server.

' Constants used by OpenTextFile()
'
Const ForReading = 1
Const ForWriting = 2


Sub Main
  crt.Screen.Synchronous = True
 
  Dim fso, file
 
  Set fso = CreateObject("Scripting.FileSystemObject")

  ' Open a file for writing. The last True parameter causes the file
  ' to be created if it doesn't exist.
  ' strFile = crt.Arguments(0)
  strFile = "F:\SecureCRT\sqlScripts\DBA_Feature_usage.sql"
  Set file = fso.OpenTextFile(strFile, ForReading, False)
 
  Do While file.AtEndOfStream <> True

    str = file.Readline
    crt.Screen.Send str & Chr(13)
   
  Loop

  crt.Screen.Synchronous = False

End Sub


cboyack 06-03-2021 01:33 PM

I've moved your post to a new thread since this is a new issue.

Regarding your request for script translation services, VanDyke Software does not offer script translation as part of our technical support offering. If any other members of the community want to chime in on this and provide translation on their own, they are welcome to do so.

We actually have an example script (SendFile.py) on our Example Python Scripts for SecureCRT® for Windows and Mac webpage that essentially performs the task that you're looking for via a python script.

You may want to trim that particular script a bit, since the cat command at the start of the script isn't needed, and neither is the EOF, since you won't be using cat command. Of course, you'll also want to modify the file to which the for option pointed, but aside from that, the script opens a file and reads it, line by line, to the remote system.

gregg 06-05-2021 10:23 AM

This should be an adequate replacement for the mentioned script.

Code:

# $language = "Python3"
# $interface = "1.0"

# Script to open a text file and read it line by
# line to a server.

DEFAULT_FILENAME = r"F:\SecureCRT\sqlScripts\DBA_Feature_usage.sql"

def main():
    # filename = crt.Dialog.Prompt("Filename to read:", "Filename", DEFAULT_FILENAME, False)
    filename = crt.Dialog.FileOpenDialog("Text file", "Open", DEFAULT_FILENAME, "SQL Files (*.sql)|*.sql|All Files (*.*)|*.*|")

    if not filename:
        # escape pressed or no filename entered
        return

    crt.Screen.Synchronous = True
    try:
        # Open a file for reading.
        with open(filename, "rt") as fh:
            for line in fh:
                crt.Screen.Send(line.rstrip() + "\r")
    except FileNotFoundError:
        crt.Dialog.MessageBox("Could not open file %s" % filename, "File not found")
    finally:
        crt.Screen.Synchronous = False
   
main()



All times are GMT -6. The time now is 03:15 PM.