I had written a code so that it can read data from a textfile and pop up IP address and send an email in VBScript.
The code ran successfully.
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Your File Name Here.txt", ForReading)
strSearchString = objFile.ReadAll
objFile.Close
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
Wscript.Echo strMatch.Value
Next
End If
The requirements have been changed and it is required to run the same process in unix server, so I have to write VBScript so all the above happens in unix.
No, you can't run VBScript on Linux/Unix. You need to re-implement your script in a language that is available on those platforms, like shell (bash, ksh, (t)csh, ...), Perl, Python, or Ruby.
Since your script seems to be extracting IP addresses from text files I'd say that a shell script would be your best choice here. On Linux distributions the default shell usually is bash
, so I suggest you start with the Bash Guide for Beginners. The tool you're looking for is grep
. Use it with the parameters -P
(Perl-style expressions) and -o
(show matches only):
grep -Po "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" /path/to/your.txt
Also, the regular expression could be shortened by grouping dot-number segments, e.g. like this:
grep -Po "\d{1,3}(\.\d{1,3}){3}" /path/to/your.txt
If you want to run this from a script, the file should look somewhat like this:
#!/bin/sh
grep -Po "\d{1,3}(\.\d{1,3}){3}" /path/to/your.txt
The shebang defines what interpreter should be used for executing the content.