Saturday, February 28, 2015

Boeing MBD FTP Download Script

Here is the Boeing MBD FTP Download Script.
Make sure you note the instructions in the header.


Author: Ed Hammond
Date: Nov 1, 2008
Purpose: Search pickup.log file for new jobs to download, download them, decrypt with GPG, unzip with PowerArchiver.
Usage: This script will parse the Boeing SNET pickup.log file and output todays files in pickup-out.txt.
Requirments: Install PowerArchiver (or modify script for winzip) and GNU GPG. Add the path to both programs to your PATH system variable.
Requirments: Dont forget to import your PGP keys and get the IDEA plugin for GPG.

-----------------------------------------------------------------------------
Start setting user variables
-----------------------------------------------------------------------------
Update these areas with your information, put this VBscript in the outputpath1 folder and then run the script.

Output file name and directory
WARNING: Your outputpath1 variable cannot have spaces in it or the command line stuff will break. (Im to busy to fix that right now)

Location of script and also this will be the working directory where everything will get dumped.
outputpath1 = "z:scripts"

FTP account info and your PGP passcode
tdiAccount = ""
tdiPassword = ""
PGPpasscode = ""

-----------------------------------------------------------------------------
Finished setting user variables
-----------------------------------------------------------------------------
outputpath = outputpath1 & ""
outputfname = outputpath & "pickup-out.txt"

Enter log file name
fname = outputpath & "pickup.log"

The header variable will help us remove the top line of the log file.
header = "BOEING"

-----------------------------------------------------------------------------
Start FTP Function

------------------------------------------------------------------------------

Function FTPDownload(sSite, sUsername, sPassword, sLocalPath, sRemotePath, _
sRemoteFile, sExt)
This script is provided under the Creative Commons license located
at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
be used for commercial purposes with out the expressed written consent
of NateRice.com

Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2

Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
Set oFTPScriptShell = CreateObject("WScript.Shell")

sRemotePath = Trim(sRemotePath)
sLocalPath = Trim(sLocalPath)

----------Path Checks---------
Here we will check the remote path, if it contains
spaces then we need to add quotes to ensure
it parses correctly.
If InStr(sRemotePath, " ") > 0 Then
If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then
sRemotePath = """" & sRemotePath & """"
End If
End If

Check to ensure that a remote path was
passed. If its blank then pass a ""
If Len(sRemotePath) = 0 Then
Please note that no premptive checking of the
remote path is done. If it does not exist for some
reason. Unexpected results may occur.
sRemotePath = ""
End If

If the local path was blank. Pass the current
working direcory.
If Len(sLocalPath) = 0 Then
sLocalpath = oFTPScriptShell.CurrentDirectory
End If

If Not oFTPScriptFSO.FolderExists(sLocalPath) Then
destination not found
FTPDownload = "Error: Local Folder Not Found."
Exit Function
End If

sOriginalWorkingDirectory = oFTPScriptShell.CurrentDirectory
oFTPScriptShell.CurrentDirectory = sLocalPath
--------END Path Checks---------

build input file for ftp command
sFTPScript = sFTPScript & "USER " & sUsername & vbCRLF
sFTPScript = sFTPScript & sPassword & vbCRLF
sFTPScript = sFTPScript & "cd " & sRemotePath & vbCRLF

check to see if we should download in ASCII mode
If sExt = "I01" Then
sFTPScript = sFTPScript & "ascii" & vbCRLF
Else
sFTPScript = sFTPScript & "binary" & vbCRLF
End If

sFTPScript = sFTPScript & "prompt n" & vbCRLF
sFTPScript = sFTPScript & "mget " & sRemoteFile & vbCRLF
sFTPScript = sFTPScript & "quit" & vbCRLF & "quit" & vbCRLF & "quit" & vbCRLF


sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
sFTPTempFile = sFTPTemp & "" & oFTPScriptFSO.GetTempName
sFTPResults = sFTPTemp & "" & oFTPScriptFSO.GetTempName

Write the input file for the ftp command
to a temporary file.
Set fFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
fFTPScript.WriteLine(sFTPScript)
fFTPScript.Close
Set fFTPScript = Nothing

oFTPScriptShell.Run "%comspec% /c FTP -n -s:" & sFTPTempFile & " " & sSite & _
" > " & sFTPResults, 0, TRUE

Wscript.Sleep 1000

Check results of transfer.
Set fFTPResults = oFTPScriptFSO.OpenTextFile(sFTPResults, ForReading, _
FailIfNotExist, OpenAsDefault)
sResults = fFTPResults.ReadAll
fFTPResults.Close

oFTPScriptFSO.DeleteFile(sFTPTempFile)
oFTPScriptFSO.DeleteFile (sFTPResults)

If InStr(sResults, "226 Transfer complete.") > 0 Then
FTPDownload = True
ElseIf InStr(sResults, "File not found") > 0 Then
FTPDownload = "Error: File Not Found"
ElseIf InStr(sResults, "cannot log in.") > 0 Then
FTPDownload = "Error: Login Failed."
Else
FTPDownload = "Error: Unknown."
End If

Set oFTPScriptFSO = Nothing
Set oFTPScriptShell = Nothing
End Function

dim results
sLocalPath = outputpath
sRemotePath = ""

-----------------------------------------------------------------------------
Get pickup.log from Boeing FTP

------------------------------------------------------------------------------

Get pickup.log

pickuplog = FTPDownload("tdi.boeing.com", tdiAccount, tdiPassword, sLocalPath, sRemotePath, "pickup.log", "log")


dim a

Looking for todays date as this format: Oct 31
If the date is less than 10, then use 2 spaces.
if Day(Date) < 10 then
searchdate = " " & Day(Date)
Else
searchdate = " " & Day(Date)
End if

This is the date format used by the log to pick out only files added today.
spam = MonthName(Month(Date),true) & searchdate


*** String searching for

Const ForReading = 1, ForWriting = 2, ForAppending = 8

**** Open pickup log file for appending
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.OpenTextFile(outputfname, ForAppending, TRUE)
set fso = CreateObject("Scripting.FileSystemObject")
if not fso.fileExists(outputfname) then
Set MyFile = fso.CreateTextFile(outputfname, True)
MyFile.Close
End if

found = "0"

if fso.fileExists(fname) then
set txtstream = fso.Opentextfile(fname)

Do while not (txtStream.atEndofStream)
text2 = txtStream.ReadLine
text = text2 & vbCrlf

*** Checks pickup.log for uploads from today
if (InStr(text,spam)>1) then

if (InStr(text,header)<1) then
a=Split(text)
Take the first part of the file name and add a wildcard
formattedA = a(0) & "*"
found = "1"
results = FTPDownload("tdi.boeing.com", tdiAccount, tdiPassword, sLocalPath, sRemotePath, formattedA, a(1))
objOutputFile.WriteLine(text2)
End if
End if
loop
end if

if found = "0" then
objOutputFile.WriteLine(spam & " - Nothing found today.")
WScript.Quit [exitcode]
End if

strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & " ootcimv2")
Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name=" & outputpath1 & "} Where " _
& "ResultClass = CIM_DataFile")

Set objShell = CreateObject("WScript.Shell")

For Each objFile In FileList
strNewName = objFile.Drive & objFile.Path & _
objFile.FileName & "." & objFile.Extension

If objFile.Extension = "ZIP" Then
strReName = objFile.Drive & objFile.Path & _
objFile.FileName & "-2.ZIP"
gpgString = "cmd /c echo " & PGPpasscode & Chr(124) & "gpg --load-extension Libidea --passphrase-fd 0 --decrypt --output " & strReName & " " & strNewName
objShell.Run(gpgString)
objOutputFile.WriteLine("Decrypted: " & strNewName)
WScript.Sleep(8000)
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.DeleteFile(strNewName)
End If

If objFile.Extension = "I01" Then
strReName = objFile.Drive & objFile.Path & _
objFile.FileName & ".igs"
Set objShell = CreateObject("WScript.Shell")
gpgString = "cmd /c echo " & PGPpasscode & Chr(124) & "gpg --load-extension Libidea --passphrase-fd 0 --decrypt --output " & strReName & " " & strNewName
objShell.Run(gpgString)
objOutputFile.WriteLine("Decrypted: " & strNewName)
WScript.Sleep(8000)
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.DeleteFile(strNewName)
End If

If objFile.Extension = "C31" Then
strReName = objFile.Drive & objFile.Path & _
objFile.FileName & ".igs"
Set objShell = CreateObject("WScript.Shell")
Wscript.Echo gpgString
gpgString = "cmd /c echo " & PGPpasscode & Chr(124) & "gpg --load-extension Libidea --passphrase-fd 0 --decrypt --output " & strReName & " " & strNewName
objShell.Run(gpgString)
objOutputFile.WriteLine("Decrypted: " & strNewName)
WScript.Sleep(8000)
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.DeleteFile(strNewName)
End If
Next


-----------------------------------------------------------------------------
Unzip MBD Parts
We run unzip first and then look for tar and gz archives twice, deleteing them after we unzip
If the archives are really large you may need to increast the sleep time
Add Powerarchiver to your path. C:Program FilesPowerArchiver
------------------------------------------------------------------------------


unzipped = "0"

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name=" & outputpath1 & "} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In FileList
strNewName = objFile.Drive & objFile.Path & _
objFile.FileName & "." & objFile.Extension

If objFile.Extension = "ZIP" Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run("powerarc -e " & strNewName)
objOutputFile.WriteLine("Unzipped: " & strNewName)
WScript.Sleep(8000)
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.DeleteFile(strNewName)
unzipped = "1"
End If
Next
WScript.Sleep(2000)
if unzipped = "1" then

Set objWMIService = GetObject("winmgmts:\" & strComputer & " ootcimv2")
Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name=" & outputpath1 & "} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In FileList
strNewName = objFile.Drive & objFile.Path & _
objFile.FileName & "." & objFile.Extension


If objFile.Extension = "tar" Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run("powerarc -e " & strNewName)
objOutputFile.WriteLine("Unzipped: " & strNewName)
WScript.Sleep(8000)
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.DeleteFile(strNewName)
unzipped = "2"
End If

If objFile.Extension = "gz" Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run("powerarc -e " & strNewName)
objOutputFile.WriteLine("Unzipped: " & strNewName)
WScript.Sleep(8000)
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.DeleteFile(strNewName)
unzipped = "2"
End If
Next
end if
WScript.Sleep(2000)
if unzipped = "2" then

Set objWMIService = GetObject("winmgmts:\" & strComputer & " ootcimv2")
Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name=" & outputpath1 & "} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In FileList
strNewName = objFile.Drive & objFile.Path & _
objFile.FileName & "." & objFile.Extension


If objFile.Extension = "tar" Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run("powerarc -e " & strNewName)
objOutputFile.WriteLine("Unzipped: " & strNewName)
WScript.Sleep(8000)
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.DeleteFile(strNewName)
unzipped = "3"
End If

If objFile.Extension = "gz" Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run("powerarc -e " & strNewName)
objOutputFile.WriteLine("Unzipped: " & strNewName)
WScript.Sleep(8000)
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.DeleteFile(strNewName)
unzipped = "3"
End If
Next
end if

Let do some clean up on the txt0001.txt log files that no one reads

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name=" & outputpath1 & "} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In FileList
strNewName = objFile.Drive & objFile.Path & _
objFile.FileName & "." & objFile.Extension
Wscript.Echo strNewName

If Instr(1, objFile.FileName, "txt0001") > 0 Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.DeleteFile(strNewName)
End If
Next

*** Close up everything
objOutputFile.Close
Set objFileSystem = Nothing

Pop up the log file so you know something happend.
objShell.Run("notepad.exe " & outputfname)






for details click below

No comments:

Post a Comment