Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 04-11-2014, 03:53 PM
M1NDN1NJ4 M1NDN1NJ4 is offline
Registered User
 
Join Date: Nov 2013
Posts: 32
VBScript to Python conversion

https://www.dropbox.com/s/sr5uz4lr98...python.vbs.txt

Hello,

I'd like to be able to run this script on my MacBook Pro, and was wondering if there was an easy way to convert this to Python?
Reply With Quote
  #2  
Old 04-11-2014, 04:02 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
Hello M1NDN1NJ4,

We are unable to view Dropbox, but it does not matter because there is not really an easy way.

If you based your script off one of our VBScript example scripts, we may have a Python version of the same script.

Please post it here, redacting sensitive info, using the code tags.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 04-11-2014, 11:27 PM
M1NDN1NJ4 M1NDN1NJ4 is offline
Registered User
 
Join Date: Nov 2013
Posts: 32
Here you go, I've attached the script to the post because it's too large to put in the post itself. There is a python script that's the same but, there's things I've added to this one that I don't know how to do in Python.

Thanks,

Ryan
Attached Files
File Type: txt My script to convert to python.vbs.txt (26.2 KB, 1564 views)
Reply With Quote
  #4  
Old 04-14-2014, 09:02 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
Hello Ryan,

I believe it will certainly be easier to start with the complete Python example script and then make the same additions/changes to it as you did to the original VB script example.

Python is extremely dependent on correct syntax and indenting so it will be much easier to assist you with the lines of code you need to add to the Python example than to convert the entire existing VB script to Python.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 04-14-2014, 09:20 PM
M1NDN1NJ4 M1NDN1NJ4 is offline
Registered User
 
Join Date: Nov 2013
Posts: 32
The python script I'd like to use is here:

https://forums.vandyke.com/showthrea...7294#post37294

Here are the following lines of code that I need added or changed to the python script from the text document I uploaded:

Line 136-140
Code:
        '   " /AUTH publickey /I ""full_path_to_private_key_file"" " & _
        strConnectString = _
            g_strFirewall & _
            " /TELNET " & _
            " " & g_strHost
Line 173-191
Code:
                ' Send each command one-by-one to the remote system:
                For Each strCommand In vCommands
                    crt.Screen.Send vbCRLF
					crt.Screen.WaitForString vbLF
					strHostname = crt.Screen.Readstring("#")
					strOrigPrompt = strPrompt
					strConfigPrompt = ")#"
					strPrivPrompt = "#"
					
					If strCommand = "configure terminal" Then strPrompt = strConfigPrompt
					If strCommand = "end" Then strPrompt = strPrivPrompt
					If strCommand = "exit" Then strPrompt = strPtivPrompt
                    If strCommand = "" Then Exit For
                    
                    ' Send the command text to the remote
                    g_objNewTab.Screen.Send strCommand & vbcr

                    ' Wait for the command to be echo'd back to us.
                    g_objNewTab.Screen.WaitForString strCommand
Line 241-244
Code:
                    strLogFile = Replace( _
                        g_strLogFileTemplate, _
                        "IPADDRESS", _
			strHostname)
Line 274-283
Code:
	            objFile.WriteLine "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
                    objFile.WriteLine "Results of command """ & strCommand & _
                    """ sent to " & strHostname & "- IP: " & g_strHost & ":"
		    objFile.WriteLine "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
					

                    ' Write out the results of the command and a separator
                    objFile.WriteLine strResult
                    objFile.WriteLine "#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#"
		    objFile.WriteLine "#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#"
Thanks,

Ryan

Last edited by M1NDN1NJ4; 04-14-2014 at 09:22 PM.
Reply With Quote
  #6  
Old 04-15-2014, 09:36 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
Hello Ryan,

I am confused in your version of the VBScript by your firewall settings. It appears to me you *do not* need to connect through a proxy.

The firewall option was not included in the Python script, but based on my take of your version of the VBScript, it should not be needed.

Note that line numbers listed below are always going to reflect the original script, if you add lines above our "working section", you will need to calculate the offset.

Again, indentation is important in Python, take note of surrounding lines and pattern-match. Python *is* case-sensitive also.


Line 367 in the Python script is where the connect string is being built.

So:

Code:
    strConnectString = "/ssh2 " + g_strHost
Becomes:
Code:
    strConnectString = "/telnet " + g_strHost
The "For loop" in the Python script begins at line 381.

Your additions would come before the line sending the first command (insert after blank at line 397):

Code:
                crt.Screen.Send("\r\n")
                crt.Screen.WaitForString("\r\n")
                strHostname = crt.Screen.Readstring("#")
                strOrigPrompt = strPrompt
                strConfigPrompt = ")#"
                strPrivPrompt = "#"
                
                if strCommand = "":
                    break
                if strCommand = "configure terminal":
                    strPrompt = strConfigPrompt
                if strCommand = "end":
                    strPrompt = strPrivPrompt
                if strCommand = "exit":
                    strPrompt = strPtivPrompt
                    # [BG]: Possible typo above, should it be strPrivPrompt?

Line 398's comment starts here (with the addition of these lines, 
comment would begin at line 415 now).
I reordered the above if statements, because if the command string is empty, no need for further checking.


You should be able to make the remaining changes.

Here is the syntax for Python's replace() function:

Code:
s = string.replace(s, old, new [, maxrep])
Quote:
string.replace(s, old, new[, maxreplace])
Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.

Here are some URLs that might help you research Python syntax:
http://docs.python.org/contents.html
http://www.tutorialspoint.com/python...uick_guide.htm
SecureCRT on Mac uses Python 2.5. The URLs provided above might be for newer versions of Python, so make sure to seek out info on functionality available in Python 2.5.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #7  
Old 03-08-2015, 11:54 PM
sumoncsecuet sumoncsecuet is offline
Registered User
 
Join Date: Mar 2015
Posts: 1
convert vb script to python

I need to convert this vbscript file to python:

Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set file = fileSystem.GetFile(WScript.Arguments(0))
convertedFileNameWriteTo=WScript.Arguments(1)
Call SplitFiles(file,fileSystem)

csvFileName = Replace(file, file.Name, "fileItem.txt")
Set fileObject = CreateObject("Scripting.FileSystemObject").OpenTextFile(convertedFileNameWriteTo,2,true)
fileObject.WriteLine(csvFileName)
fileObject.Close
Set fileObject = Nothing
'Wscript.Echo "File split completed for " & fileSystem.GetFileName(file) 'WScript.Arguments(0)
WScript.Quit 0

Sub SplitFiles(file, fileSystem)
Dim objSource
Dim strLine
'Dim objHeader
Dim objItem
Dim objFSO
Const ForReading = 1

Dim file1
file1 = Replace(file,fileSystem.GetFileName(file),"fileHeader.txt")
Dim file2
file2 = Replace(file,fileSystem.GetFileName(file),"fileItem.txt")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSource = objFSO.OpenTextFile(file, ForReading)
'Set objHeader = objFSO.CreateTextFile(file1, True)
Set objItem = objFSO.CreateTextFile(file2, True)

Dim SourceRecordKey
SourceRecordKey = 1
Dim previousLine
previousLine=""
Dim PreviousHline
PreviousHline=""
Do Until objSource.AtEndOfStream
strLine = objSource.ReadLine
If StrComp(Left(strLine,3),"TH|")=0 Or StrComp(Left(strLine,2),"H|")=0 Then 'InStr(1, strLine, "H|") = 1 Then
If previousLine="H" Then
objItem.WriteLine (PreviousHline)
End If
If StrComp(Left(strLine,3),"TH|")=0 Then
PreviousHline="SourceHeaderRecordKey" & "|" & strLine
else
PreviousHline=SourceRecordKey & "|" & strLine
End If
previousLine="H"
ElseIf StrComp(Left(strLine,3),"TI|")=0 Or StrComp(Left(strLine,2),"I|")=0 Then 'InStr(1, strLine, "I|") > 0 Then
PreviousHline=Replace(PreviousHline, "\r\n", "")
PreviousHline=Replace(PreviousHline, "\n", "")
PreviousHline=Replace(PreviousHline, "\r", "")
If StrComp(Left(strLine,3),"TI|")=0 Then
strLine=Replace(strLine, "|ID|", "|Item Opportunity ID|")
strLine=Replace(strLine, "|Product category ID|", "|Product category Item Opportunity ID|")
objItem.WriteLine (PreviousHline & "|" & "SourceItemRecordKey" & "|" & strLine)
else
objItem.WriteLine (PreviousHline & "|" & SourceRecordKey & "|" & strLine)
End If
previousLine="I"
'objItem.WriteLine (SourceRecordKey & "|" & strLine)
End If
SourceRecordKey= SourceRecordKey+1
Loop

'objHeader.Close
objItem.Close
objSource.Close

Set objSource = Nothing
'Set objHeader = Nothing
Set objItem = Nothing
Set objFSO = Nothing

End Sub


can you guys help me please?
Reply With Quote
  #8  
Old 03-09-2015, 08:13 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi sumoncsecuet,

Thanks for the post. We don't actually convert scripts, but we might be able to offer pointers if you are having difficulty with the SecureCRT scripting API.

I don't see any references to the SecureCRT scripting API in your script.

What VanDyke Software product are you using?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
Reply

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 05:15 AM.