Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

scripting gethostbyname

184 views
Skip to first unread message

roger

unread,
Oct 30, 2005, 12:12:53 AM10/30/05
to
Here's a 3 line perl script that prints the IP address of a named host:


use Socket;
$packed = gethostbyname("$ARGV[0]") or die "unknown host: $ARGV[0]\n";
print inet_ntoa($packed);


Use like so (host name can be found via DNS or in local hosts file)


WINSERVER:rsr>perl gethostbyname.pl winserver
192.168.0.4

WINSERVER:rsr>perl gethostbyname.pl msnews.microsoft.com
207.46.246.16


How does one do that with windows scripting?

Thanks very much.

mayayana

unread,
Oct 30, 2005, 10:32:04 AM10/30/05
to
You can't do that in VBScript or javascript.
Perl is using some kind of component
("use Socket"). To call Winsock functions
in any other script you need to do the
same thing: Wrap the function in a component
that "pre-digests" it to a form that the script
can handle.

--
mayaya...@mindXXspring.com
(Remove Xs for return email.)
roger <r...@nospam.nospam> wrote in message
news:Xns96FED7E31A76...@207.46.248.16...

Michael Reese

unread,
Oct 30, 2005, 10:29:28 PM10/30/05
to
Perhaps I don't get the question, but is this what you are looking for?

Watch for word wrap
----------------------------------------------------------
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")

strComputer = "www.google.com"
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_PingStatus " & _
"Where Address = '" & strComputer & "'")

For Each objItem In colItems
WScript.Echo "Address: " & objItem.Address
WScript.Echo "ProtocolAddress: " & objItem.ProtocolAddress
Next
---------------------------------------------------------------------------
You can modify the strComputer line to the host that you wish to find,
or set up the script to accept arguments. This only works from an XP
or 2003 machine because the win32_PingStatus class was not available
until then. If you want this to work from a previous version, this
article may be helpful.

http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0914.mspx

Michael Reese
Information Systems Technician

Steven Cheng[MSFT]

unread,
Oct 30, 2005, 10:48:39 PM10/30/05
to
Hi Roger,

As mayayana has mentioned, in wscript (vbscript or jscript) we do not have
buildin components like the win32 api gethostbyname or the Dns class in
.net framework. And if you'd like to call some platform API, the easiest
means would be create a custom COM component which wrapper the win32 api
call, and then use that COM component in your script code.

In addition, for network related operation, we can also consider WMI:

#WMI Tasks:Networking
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
wmi_tasks__networking.asp

or sometimes we can also use script code to perform another commandline
execution like "ping", ,"NETSTAT -A ...." and flush the result into file
and parse the content....

Hope helps. thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| From: "mayayana" <mayaya...@mindXXspring.com>
| Newsgroups: microsoft.public.scripting.vbscript
| References: <Xns96FED7E31A76...@207.46.248.16>
| Subject: Re: scripting gethostbyname
| Lines: 39
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 5.00.2615.200
| X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
| Message-ID: <UH59f.2284$m81...@newsread1.news.atl.earthlink.net>
| Date: Sun, 30 Oct 2005 15:32:04 GMT
| NNTP-Posting-Host: 4.154.234.10
| X-Complaints-To: ab...@earthlink.net
| X-Trace: newsread1.news.atl.earthlink.net 1130686324 4.154.234.10 (Sun,
30 Oct 2005 07:32:04 PST)
| NNTP-Posting-Date: Sun, 30 Oct 2005 07:32:04 PST
| Organization: EarthLink Inc. -- http://www.EarthLink.net
| Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli
ne.de!border2.nntp.dca.giganews.com!nntp.giganews.com!elnk-atl-nf1!newsfeed.
earthlink.net!stamper.news.atl.earthlink.net!newsread1.news.atl.earthlink.ne
t.POSTED!4321fba5!not-for-mail
| Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.scripting.vbscript:27836
| X-Tomcat-NG: microsoft.public.scripting.vbscript

roger

unread,
Nov 1, 2005, 12:07:53 AM11/1/05
to
Thanks for this example
.
It will just about do what I need, the only minor problem
being that the host must actually be pingable in order to
get the address.

For my immediate purposes, that's probably fine.

If there is any other WMI or other mechanism to just
get the host address from the name, that would ideal for
what I'm doing.

Thanks a lot.

roger

"Michael Reese" <mnr...@hotmail.com> wrote in
news:1130729368....@g43g2000cwa.googlegroups.com:

> Perhaps I don't get the question, but is this what you are looking
> for?
>
> Watch for word wrap
> ----------------------------------------------------------
> On Error Resume Next
> Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
>
> strComputer = "www.google.com"
> Set colItems = objWMIService.ExecQuery _
> ("Select * from Win32_PingStatus " & _
> "Where Address = '" & strComputer & "'")
>
> For Each objItem In colItems
> WScript.Echo "Address: " & objItem.Address
> WScript.Echo "ProtocolAddress: " & objItem.ProtocolAddress
> Next
> -----------------------------------------------------------------------

> ---- You can modify the strComputer line to the host that you wish to

0 new messages