#1
|
|||
|
|||
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? |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
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 |
#4
|
|||
|
|||
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 |
#5
|
|||
|
|||
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 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 Code:
strLogFile = Replace( _ g_strLogFileTemplate, _ "IPADDRESS", _ strHostname) 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 "#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#" Ryan Last edited by M1NDN1NJ4; 04-14-2014 at 09:22 PM. |
#6
|
|||
|
|||
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 Code:
strConnectString = "/telnet " + g_strHost 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). 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:
Here are some URLs that might help you research Python syntax: http://docs.python.org/contents.htmlSecureCRT 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 |
#7
|
|||
|
|||
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? |
#8
|
|||
|
|||
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? |
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|