Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 675
  • Last Modified:

Extract strings from multiple text files

Hi,

I have an application that works based on an "optionfile.app" on each server. This is actually a txt file that can be opened with notepad. The path of the optionfile is the same in all servers, something like "C:\App\optionfile.app".

I need a script to pull two specific lines from every optionfile.app from every server. Assume that the optionfile is something like this:

Communication   tcpip
Port   1500
TCPaddress    10.0.0.4
ServerName     server1
Passwordaccess     password

From every optionfile, I need to extract these two lines:

TCPaddress    10.0.0.4
ServerName     server1

and output to a txt or csv file, does not matter. I've done some research that I can extract this with VB.net and using RegEx but this will run on every server (Windows 2003, 2008 both x86 and x64) so I don't think a .NET solution is reliable.

Any help and direction appreciated. Thanks.
0
Batuhan Cetin
Asked:
Batuhan Cetin
  • 5
  • 3
  • 3
  • +1
4 Solutions
 
käµfm³d 👽Commented:
Why would .NET be unreliable? I believe it comes with or can be installed on 2003 and I know it comes with 2008.

In any event, I don't think you need regex for something like this--a simple InStr should suffice. Here is a VBScript example:
Dim inFilename
Dim outFilename
Dim fso
Dim inFile
Dim outFile
Dim line
Dim output
Dim nothingFound


inFilename = "C:\App\optionfile.app"
outFilename = "C:\App\searchresults.txt"


nothingFound = True

Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.OpenTextFile(outFilename, 2, True)   ' Change to 8 for appending to output instead of overwriting

If fso.FileExists(inFilename) Then

    Set inFile = fso.OpenTextFile(inFilename, 1)

    While Not inFile.AtEndOfStream
        line = inFile.ReadLine
        
        If InStr(line, "TCPaddress") > 0 Or InStr(line, "ServerName") > 0 Then
            outFile.WriteLine(line)
            nothingFound = False
        End If
    Wend
    
    If nothingFound Then
        outFile.WriteLine("No results found!")
    End If
    
    inFile.Close
    outFile.Close
Else
	outFile.WriteLine("Could not locate input file!")
End If

Open in new window

0
 
Batuhan CetinAuthor Commented:
Thanks kaufmed, this is working exactly as I want on a single server. I need to pull this info from many servers, so I need to modify "inFilename" parameter to meet the following;

1) Because I will get this info from multiple servers, the path must be smething like;

inFilename = "\\" & serverlist & "\c$\App\optionfile.app"

and serverlist will be a txt file that includes server names each per line. VBS must connect to each machine from that list at a time.

2) Also on some servers the name of the optionfile.app file is modified like "optionfileV1.app" or "old_optionfile.app". These are renamed by server admins so I can't tell exactly. So I will need to search for;

*optionfile*.app

How can I do this? Thanks.
0
 
Bill PrewCommented:
Don't know if you are interested, but here's a little BAT file that should do the job.  When I tested it here it produced the output below.  Edit the two SET's near the top as needed.

Server:[server1], File:[\\server1\c$\apps\optionfileV1.app]
TCPaddress    10.0.0.4
ServerName     server1
Server:[server2], File:[\\server2\c$\apps\optionfileV2.app]
TCPaddress    10.0.0.5
ServerName     server2

~bp
@echo off
set ServersFile=c:\temp\EE26497242\servers.txt
set OutputFile=c:\temp\EE26497242\output.txt
if exist "%OutputFile%" del "%OutputFile%"
for /F "usebackq tokens=*" %%A in ("%ServersFile%") do (
  for %%B in ("\\%%~A\c$\apps\*optionfile*.app") do (
    echo Server:[%%~A], File:[%%~B]>>"%OutputFile%"
    findstr /i /c:"tcpaddress" /c:"servername" "%%~B">>"%OutputFile%"
  )
)

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Batuhan CetinAuthor Commented:
Wow, thanks that's wonderful. I don't know a lot about batch scripting so I didn't understand much of the code but it works :)

However I will wait for kaufmed if he can show me a way to do it with VBS, then split the points. Thanks again.
0
 
Batuhan CetinAuthor Commented:
Oh billprew, I was messing around with your script and I realized the script hangs (most probably retrying in background) when it can't reach a server. How can I get past this? I need it to continue to the following server if it can't reach the current server. The reasons might be the server is shutdown, the service account does not have permission to access "C$" on that machine, cannot resolve DNS name, etc..

Can we set something like a timeout? Each *.app file takes at most 10-15 sec to be processed. Can we tell the script to continue to next server if it can't get the app file in that time? Preferebly write an error message ("Access Failed") to the output file?
0
 
Batuhan CetinAuthor Commented:
After I wrote my last comment, I see that the script actually moved on to the next server, after waiting about 5-10 minutes.

This is good news but if possible a timeout like 10 sec and an error message would be useful.
0
 
Batuhan CetinAuthor Commented:
I'm sending multiple comments but the problem appears to be something totally different from a timeout issue.

In my first run against the server list, it get the info from the first server in the list, waited for 10 minutes, then jumped to the 428nd server in the list, continued and finished successfully from there on.

In my second run, after getting the info from 1st server, it jumped to the 3rd server, then some server else.

It seems to skip servers randomly. There are servers that the script got the lines at the first run, but not the second and vice versa.

I also tried to run the script against a single server which seems to be failed in both bulk runs and it worked!

Do you have any idea of this random jumps?
0
 
Bill PrewCommented:
Probably the next step would be to add a test as a server is read from the text file to try and make sure it's a valid server, is online, and that the share exists.  We can add some code to use PING etc to do some basic checking on the server being up.

~bp
0
 
käµfm³d 👽Commented:
>>  However I will wait for kaufmed if he can show me a way to do it with VBS

My post was VBS  :)
0
 
käµfm³d 👽Commented:
I may have misinterpreted that comment  :\

I any event, I believe this is what you are after. I don't have any network shares to test on, but I believe it to be accurate. I must admit, though, billprew's solution is pretty slick--especially considering it's a batch file and only takes 10 lines...  Nice  :)
Dim serverFilename
Dim inFilename
Dim outFilename
Dim fso
Dim serverFile
Dim inFile
Dim outFile
Dim line
Dim output
Dim nothingFound

serverFilename = "C:\serverlist.txt"

nothingFound = True

Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.OpenTextFile(outFilename, 2, True)   ' Change to 8 for appending to output instead of overwriting

If fso.FileExists(serverFilename) Then
	Set serverFile = fso.OpenTextFile(serverFilename, 1)
	
	While Not serverFile.AtEndOfStream
		inFilename = serverFile.ReadLine()
		
		If fso.FileExists(inFilename) Then
			Set inFile = fso.OpenTextFile(inFilename, 1)
			
			outFile.WriteLine(inFilename)

			While Not inFile.AtEndOfStream
				line = inFile.ReadLine

				If InStr(line, "TCPaddress") > 0 Or InStr(line, "ServerName") > 0 Then
					outFile.WriteLine(VbTab & line)
					nothingFound = False
				End If
			Wend

			If nothingFound Then
				outFile.WriteLine(VbTab & "No results found!")
			End If

			inFile.Close
		Else
			outFile.WriteLine(VbTab & "Could not locate input file!")
		End If
	Wend
Else
	outFile.WriteLine("Could not locate server list file!")
End If

outFile.Close

Open in new window

0
 
Bill PrewCommented:
Let's see if this helps a bit...

~bp
@echo off
set ServersFile=c:\temp\EE26497242\servers.txt
set OutputFile=c:\temp\EE26497242\output.txt
if exist "%OutputFile%" del "%OutputFile%"
for /F "usebackq tokens=*" %%A in ("%ServersFile%") do (
  ping /n 1 %%A | find "TTL">nul
  if %ERRORLEVEL% EQU 0 (
    for %%B in ("\\%%~A\c$\apps\*optionfile*.app") do (
      echo Server:[%%~A], File:[%%~B]>>"%OutputFile%"
      findstr /i /c:"tcpaddress" /c:"servername" "%%~B">>"%OutputFile%"
    )
  )
)

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now