PDA

View Full Version : بدست آوردن IP کامپیوتر از طریق اسکریپت



Rambod
سه شنبه 24 تیر 1382, 16:26 عصر
سلام
آیا میشه با VBScript یا JavaScript آی پیه Local Computer را بدست آورد؟

Vahid_Nasiri
چهارشنبه 25 تیر 1382, 01:25 صبح
Script to retrieve the current IP Address on the first network card.
'
' (c) 2002 A.J. Elsinga
' anne.jan@network-direct.com
'
' version 1.0


' ************************************************** **********
' *** Start of functions and procedures ***
' ************************************************** **********

Function GetIPAddress
' This function retrieves the IP Address from the registry
' It gets it from the CurrentControlSet, so even when using DHCP
' it returns the correct IP Address

' Declare variables

Dim key
Dim cTempIPAddress
Dim cIPAddress
dim cIPAddressKey


Set oSh = CreateObject("WScript.Shell")

cInterfacesKey="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Param eters\Interfaces\"
cNICSearch="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1\ServiceName"


' First check which network card to use
cNicServiceName=oSh.RegRead(cNICSearch)

' Now read the IP Address from that card
cIPAddressKey=cInterfaceskey + cNicServiceName+"\IPAddress"
cTempIPAddress=oSh.RegRead (cIPAddresskey)

' Split the items in the var tempIPAddress to the octets in array IPAddress
cIPAddress=split (cTempIPAddress(0),".",4)

' the IP addresss is now readable from ipaddress
' for example, to read the first octet, use: FirstOctet=IPAddress(0)

GetIPAddress=cIPAddress
End Function

Function GetIPOctet (nOctet)
' This function retrieves a given octet out of the IP Address
Dim IPAddress

IPAddress=GetIPAddress
GetIPOctet=IPAddress(nOctet-1)
End Function

Vahid_Nasiri
چهارشنبه 25 تیر 1382, 01:28 صبح
Please note, if you plan to run this script unattended from the task manager,
ShowMyIP.com requests that you limit your connections to once every thirty
minutes.

'Code begins here

' CurrentIP.vbs
' 1/22/2002
' Copyright Paul R. Sadowski <aa089(at)bfn.org>
'================================================= ======
'Change ONLY these Four items as needed!

'Set to False to throw an error when it can't connect, True to ignore connection errors
'best set to True when running from the task scheduler; False when running manually
Const NoError = False

'Set to False to use a messagebox or True to log to files
Const LogToFile = False

'Full Paths To the two logfiles:
Const LogFileIP = "c:\CurrentIP.txt"
Const LogFileName = "c:\CurrentHostName.txt"
'================================================= ======
' DO NOT ALTER BELOW THIS LINE!
'================================================= ======
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Const URL = "http://www.showmyip.com/"
Const WATCHFOR1 = "SIZE=""+8"">"
Const WATCHFOR2 = "<"
Const WATCHFOR3 = "<br>host/your name: </FONT><FONT FACE=""Verdana"">"

Dim Raw, IPText, NameText, WasError, x

If LogToFIle = True then
if LogFileIP = "" or LogFileName = "" then
Wscript.Echo "LogFileIP or LogFIleName is not set!" & vbCRLF & " Exiting!"
Wscript.Quit
end if
end if
GetRawData
GetIP
GetName
if LogToFIle = True then
WriteToFile
else
MsgBox "IP: " & IPText & vbCRLF & "Hostname: " & NameText
end if

Sub GetRawData
Err.Clear
Set WshShell = WScript.CreateObject("WScript.Shell")
If NoError = True then
On Error Resume Next
end if
Set Http = CreateObject("Microsoft.XmlHttp")
Http.open "GET", URL, FALSE
Http.send ""
Raw = http.responseText
If NoError = True then
On Error GoTo 0
end if
if Err.Number <> 0 then
if NoError = False then
MsgBox "Could not connect to server " & URL & vbCRLF & " exiting"
Wscript.Quit
else
WasError = True
end if
end if
Err.Clear
Set Http = Nothing
Set WshShell = Nothing
End Sub

Sub GetIP
x = Instr(Raw, WATCHFOR1)
if x <> 0 then
IPText = mid(Raw, x + Len(WATCHFOR1))
x = Instr(IPText, WATCHFOR2)
if x <> 0 then
IPText = mid(IPText, 1, x-1)
end if
end if
End Sub

Sub GetName
if WasError = True then
Exit Sub
end if
x = Instr(Raw, WATCHFOR3)
if x <> 0 then
NameText = mid(Raw, x + Len(WATCHFOR3))
x = Instr(NameText, WATCHFOR2)
if x <> 0 then
NameText = mid(NameText, 1, x-1)
end if
if Instr(NameText, ">") <> 0 then
do until x = 0
x = Instr(NameText, ">")
if x <> 0 then
NameText = mid(NameText, x+1)
end if
loop
end if
end if
End Sub

Sub WriteToFile
if LogFileIP <> "" and LogFileName <> "" and IPText <> "" then
Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.OpenTextFile(LogFileIP, ForWriting, True, TristateFalse)
f.WriteLine (IPText)
f.close

Set f = fs.OpenTextFile(LogFileName, ForWriting, True, TristateFalse)
f.WriteLine (NameText)
f.close

Set f = Nothing
Set fs = Nothing
end if
End Sub

Vahid_Nasiri
چهارشنبه 25 تیر 1382, 01:31 صبح
در ای اس پی :


<%
IPAddress = request.servervariables("REMOTE_ADDR") ' returns local address
response.write ("IP of local: " & IPAddress & "<br>")
IPAddress = request.servervariables("LOCAL_ADDR") ' returns web server
response.write ("IP of server: " & IPAddress & "<br>")
%>

Vahid_Nasiri
چهارشنبه 25 تیر 1382, 01:36 صبح
در وی بی :


Text1.Text = Winsock1.LocalIP

Vahid_Nasiri
چهارشنبه 25 تیر 1382, 01:40 صبح
بازهم وی بی!



'**************************************
' Name: Getting IP with VB4
' Description:Get your IP address.I've b
' een trying to trim this to get only the
' IP number and get rid of any non-essenti
' al code for doing that. If anyone can he
' lp me hack this into shape, I'd really a
' ppreciate it. moonboots@earthling.net
boots
' By: Newsgroup Posting
'
'This code is copyrighted and has' limited warranties.Please see http://w
' ww.Planet-Source-Code.com/vb/scripts/Sho
' wCode.asp?txtCodeId=716&lngWId=1'for details.'**************************************



Public Function hibyte(ByVal wParam As Integer)
hibyte = wParam \ &H100 And &HFF&
End Function


Public Function lobyte(ByVal wParam As Integer)
lobyte = wParam And &HFF&
End Function
Option Explicit


Sub Form_Load()
'Sockets Initialize
Dim WSAD As WSADATA
Dim iReturn As Integer
Dim sLowByte As String, sHighByte As String, sMsg As String
iReturn = WSAStartup(WS_VERSION_REQD, WSAD)


If iReturn <> 0 Then
MsgBox "Winsock.dll is Not responding."
End
End If
If lobyte(WSAD.wversion) < WS_VERSION_MAJOR _
Or (lobyte(WSAD.wversion) = WS_VERSION_MAJOR _
And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then
sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))
sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))
sMsg = "Windows Sockets version " & sLowByte & "." &
sHighByte
sMsg = sMsg & " is Not supported by winsock.dll "
MsgBox sMsg
End
End If


If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
sMsg = "This application requires a minimum of "
sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) _
& " supported sockets."
MsgBox sMsg
End
End If
End Sub


Private Sub Form_Unload(Cancel As Integer)
'Sockets Cleanup
Dim lReturn As Long
lReturn = WSACleanup()


If lReturn <> 0 Then
MsgBox "Socket Error " & Trim$(Str$(lReturn)) & " occurred
In Cleanup "
End
End If
End Sub


Private Sub Command1_click()
Dim hostname As String * 256
Dim hostent_addr As Long
Dim host As HOSTENT
Dim hostip_addr As Long
Dim temp_ip_address() As Byte
Dim i As Integer
Dim ip_address As String


If gethostname(hostname, 256) = SOCKET_ERROR Then
MsgBox "Windows Sockets Error " & Str(WSAGetLastError())
Exit Sub
Else
hostname = Trim$(hostname)
End If
hostent_addr = gethostbyname(hostname)


If hostent_addr = 0 Then
MsgBox "Winsock.dll is Not responding."
Exit Sub
End If
RtlMoveMemory host, hostent_addr, LenB(host)
RtlMoveMemory hostip_addr, host.hAddrList, 4
ReDim temp_ip_address(1 To host.hLength)
RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength


For i = 1 To host.hLength
ip_address = ip_address & temp_ip_address(i) & "."
Next
ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
MsgBox hostname
MsgBox ip_address
End Sub

Vahid_Nasiri
چهارشنبه 25 تیر 1382, 01:48 صبح
در سی شارپ :oops:


using System;
using System.Net;
namespace DNSName
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
IPHostEntry ipEntry = Dns.GetHostByName ("www.mindcracker.com");
IPAddress [] IpAddr = ipEntry.AddressList;
for (int i = 0; i < IpAddr.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, IpAddr[i].ToString ());
}
}
}
}



Get Local System's host name and IP
string strHostName = Dns.GetHostName ();

Rambod
چهارشنبه 25 تیر 1382, 09:55 صبح
بابا سرم گیج رفت!!! دستت درد نکنه! چند امتیاز اهدا کنم؟! :lol:

محمدامین شریفی
جمعه 13 آذر 1388, 18:36 عصر
سلام،
آیا بوسیله کتابخانه های جاوا اسکریپت هم میشه اینکار را انجام داد؟
گوگل که این امکان را میده،اجازه استفاده رو نمیده،از گجت هم نمی خوام استفاده کنم.
آیا کتابخانه جاوااسکریپتی را میشناسید که بدون نیاز به کنترل های سرورساید اینکار را انجام بده؟

eAmin
شنبه 14 آذر 1388, 02:11 صبح
سلام،
آیا بوسیله کتابخانه های جاوا اسکریپت هم میشه اینکار را انجام داد؟
گوگل که این امکان را میده،اجازه استفاده رو نمیده،از گجت هم نمی خوام استفاده کنم.
آیا کتابخانه جاوااسکریپتی را میشناسید که بدون نیاز به کنترل های سرورساید اینکار را انجام بده؟
سلام.

خیر جاوااسکریپت به تنهایی از عهده ی اینکار بر نمی یاد.

محمدامین شریفی
شنبه 14 آذر 1388, 09:42 صبح
سلام.

خیر جاوااسکریپت به تنهایی از عهده ی اینکار بر نمی یاد.
http://code.google.com/apis/ajax/documentation/#ClientLocation
آیا API شبیه به این سراغ دارید؟

eAmin
چهارشنبه 18 آذر 1388, 20:27 عصر
خیر، بنده تا حالا با هیچ API ی از جمله همونی رو که شما معرفی کردید کار نکردم و اطلاعی در این موارد ندارم.