Solved

I'd like ATTACHMATE macro code to get the current machine IP address

Posted on 2011-02-17
6
560 Views
Last Modified: 2013-11-10

I'd like to be able to get the IP address of a machine running ATTACMATE macros.
0
Comment
Question by:philkryder
  • 2
  • 2
6 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34939142
Hi, I don't know how Attachmate handles ActiveX objects, but try this.

Regards,

Rob.
Dim objNetwork As Object
Set objNetwork = CreateObject("WScript.Network")
Dim strMyIP As String
strMyIP = ResolveIP(objNetwork.ComputerName)

Function ResolveIP(computerName)
	Dim objShell As Object
	Dim objExec As Object
	Dim strOutput As String
	Dim RegEx As Object
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("ping " & computerName & " -n 1")
	strOutput = objExec.StdOut.ReadAll
	Set RegEx = New RegExp
	RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
	RegEx.Global = True
	If RegEx.Test(strOutput) Then
		ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
	Else
		ResolveIP = "IP Address could not be resolved"
	End If
End Function

Open in new window

0
 
LVL 1

Author Comment

by:philkryder
ID: 34955312
RobSampson -

Thanks!
That was "close enough"

Attachmate didn't seem to understand the "classy ness" of Regular expressions.
And,
I added a bit of a delay to make sure the exec had time to run.

ended up with this - worked fine - thanks again

================================

Function get_IP_address ()


Dim objNetwork As Object
Set objNetwork = CreateObject("WScript.Network")
Dim strMyIP As String
get_IP_address = ResolveIP(objNetwork.ComputerName)

end function

Function  ResolveIP(computerName as string)

        Const leftBracket as string = "["
        Const rightBracket as string = "]"
        Dim ws1 as string
        Dim n1 as long
        Dim n2 as long
       
      Dim objShell As Object
      Dim objExec As Object
      Dim strOutput As String
      Dim RegEx As Object
        Dim      strCommand as string
 
      Set objShell = CreateObject("WScript.Shell")
       
  '                         ping only ONCE to get the IP of the computer name
        strCommand  = ""
        strCommand  =         strCommand  &     "ping "  &     computerName  &    " -n 1"

      Set objExec = objShell.Exec(strCommand)
       
        n1 = 0
       
        Do Until objExec.Status
            n1 = n1 + 1
            Doevents '    Wscript.Sleep 250
            Doevents '    Wscript.Sleep 250
        Loop
 
      
        strOutput = objExec.StdOut.ReadAll
       
        n1 = instr(strOutput,leftBracket)
        n2 = instr(strOutput,rightBracket)
        If n2 > n1 then
            ws1 = mid(strOutput,n1+1,(n2-n1)-1)
        else
            ws1 = "NoIPaddressFound"
        end if
        ResolveIP = ws1
       
 End Function
0
 
LVL 1

Author Closing Comment

by:philkryder
ID: 34955322
very minor quibbles as noted in my comment - full points!
many thanks! Phil
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34956181
Great. Thanks Phil.

Rob.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
oracle query help 18 78
Which School? 2 33
iPhone 7 64GB 5 39
2v or 4v 4 21
Whether you believe the “gig economy,” as it has been dubbed, is the next big economic paradigm shift (https://www.theguardian.com/commentisfree/2015/jul/26/will-we-get-by-gig-economy) or an overstated trend (http://www.wsj.com/articles/proof-of-a-g…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now