 |
|
|
 |
 |
|
ไม่ได้ครับ ตาม tcp/ip model http จะทำให้อยู่ที่ใช้บนสุดของ model (process layer) ซึ่งจะคุยกันโดยใช้ ip address เท่านั้น
โดยมันจะสนกว่า client เครื่องนั้นมี mac address ใด เพราะมันถูกแปลงเป็น ip address
ตั้งแต่อยู่ที internet layer แล้ว ดังนั้นเราไม่สามารถเอาค่า mac address จากเครื่อง client
ได้ เพราะ mac adress จะอยู่ใน layer ต่ำกว่า layer ของ http

|
 |
 |
 |
 |
Date :
2010-01-08 19:50:20 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
code โปรแกรม getmacaddress.cs ครับ
ใช้SDK complier ครับ
using System;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
namespace Yours.Truly
{
public class GetMacAddressFromIPAddress
{
private const int PING_TIMEOUT = 1000;
public GetMacAddressFromIPAddress()
{
}
private string GetMacAddress(string ipAddress)
{
string macAddress = string.Empty;
if (!IsHostAccessible(ipAddress)) return null;
try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
Process process = new Process();
processStartInfo.FileName = "nbtstat";
processStartInfo.RedirectStandardInput = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.Arguments = "-a " + ipAddress;
processStartInfo.UseShellExecute = false;
process = Process.Start(processStartInfo);
int Counter = -1;
while (Counter <= -1)
{
Counter = macAddress.Trim().ToLower().IndexOf("mac address", 0);
if (Counter > -1)
{
break;
}
macAddress = process.StandardOutput.ReadLine();
}
process.WaitForExit();
macAddress = macAddress.Trim();
}
catch (Exception e)
{
Console.WriteLine("Failed because:" + e.ToString());
}
return macAddress;
}
#region Getting MAC from ARP
[DllImport("iphlpapi.dll", ExactSpelling = true)]
static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr,ref uint PhyAddrLen);
private string GetMACAddressFromARP(string hostNameOrAddress)
{
if (!IsHostAccessible(hostNameOrAddress)) return null;
IPHostEntry hostEntry = Dns.GetHostEntry(hostNameOrAddress);
if (hostEntry.AddressList.Length == 0)
return null;
byte[] macAddr = new byte[6];
uint macAddrLen = (uint) macAddr.Length;
if (SendARP((int) hostEntry.AddressList[0].Address, 0, macAddr,
ref macAddrLen) != 0)
return null;
StringBuilder macAddressString = new StringBuilder();
for (int i = 0; i < macAddr.Length; i++)
{
if (macAddressString.Length > 0)
macAddressString.Append(":");
macAddressString.AppendFormat("{0:x2}", macAddr[i]);
}
return macAddressString.ToString();
}
#endregion Getting MAC from ARP
private static bool IsHostAccessible(string hostNameOrAddress)
{
Ping ping = new Ping();
PingReply reply = ping.Send(hostNameOrAddress, PING_TIMEOUT);
return reply.Status == IPStatus.Success;
}
[STAThread]
static void Main(string[] args)
{
bool macRequired = true;
while (macRequired)
{
Console.Write("Enter the IP : ");
string ipAddress = Console.ReadLine();
StringComparer cp = StringComparer.OrdinalIgnoreCase;
if (cp.Compare(ipAddress, "Exit") == 0) break;
GetMacAddressFromIPAddress getMacAddress = new GetMacAddressFromIPAddress();
Console.WriteLine("Please wait while I try to find the MAC address...");
string MacAddress = getMacAddress.GetMacAddress(ipAddress);
Console.WriteLine(MacAddress);
}
}
}
}
|
 |
 |
 |
 |
Date :
2010-01-09 13:48:48 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
no.2 asp.net ไม่ใช่ win form นะ
แนะนำให้ใช้ ActiveX
|
 |
 |
 |
 |
Date :
2010-01-09 14:09:12 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อธิบายเพิ่มครับ
จาก code ที่ให้มีวิธี getmacaddress client 2 function คือ funtion ที่ getโดยใช้ Protocol ARP และ
function ของ window คือ nbtstat code ที่ให้ใช้งาน function nbtstat ของ window ครับ
เหมือนกับเราเปิด dos ขึ้นมาแล้วพิมพ์ คำสั่ง nbtstat -a ตามด้วย IP address client จะไผลดังนี้ครับ
ข้อมูลที่ได้ คือ hostname,workgroup,mac address ครับ
C:\Documents and Settings\Manop Muangpia>nbtstat -a 192.168.1.39
Wireless Network Connection:
Node IpAddress: [192.168.1.53] Scope Id: []
NetBIOS Remote Machine Name Table
Name Type Status
---------------------------------------------
MONZA-PC <20> UNIQUE Registered
MONZA-PC <00> UNIQUE Registered
MSHOME <00> GROUP Registered
MSHOME <1E> GROUP Registered
MAC Address = 00-21-27-C2-17-3F
C:\Documents and Settings\Manop Muangpia>
|
 |
 |
 |
 |
Date :
2010-01-09 14:21:42 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
C# เขียนได้ทั้ง Win form และ web forrm ครับ
|
 |
 |
 |
 |
Date :
2010-01-09 14:31:32 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วจะต้องทำอย่างไรครับ ที่ให้ server สามารถเข้า access เครื่อง client ให้ส่ง mac ไปให้ 
|
 |
 |
 |
 |
Date :
2010-01-09 14:44:17 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือ ถ้า server เป็น windows ก็สามารถใช้ คำสั่ง nbtstat เพื่อร้องขอข้อมูลเครื่องใน network ได้ครับเราแค่เอามาใช้ผ่านทาง webform แทนที่จะพิมพ์ใน dos แทนครับ
ตัวอย่าง webform
Code (C#)
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Ip address :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<br />
<asp:Literal ID="Literal1" runat="server"></asp:Literal></div>
</form>
</body>
</html>
-----------------
defalut.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string macAddress = string.Empty;
ProcessStartInfo processStartInfo = new ProcessStartInfo();
Process process = new Process();
processStartInfo.FileName = "nbtstat";
processStartInfo.RedirectStandardInput = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.Arguments = "-a " + TextBox1.Text;
processStartInfo.UseShellExecute = false;
process = Process.Start(processStartInfo);
int Counter = -1;
while (Counter <= -1)
{
Counter = macAddress.Trim().ToLower().IndexOf("mac address", 0);
if (Counter > -1)
{
break;
}
macAddress = process.StandardOutput.ReadLine();
}
process.WaitForExit();
macAddress = macAddress.Trim();
Literal1.Text = macAddress.ToString();
}
}
|
 |
 |
 |
 |
Date :
2010-01-09 15:28:14 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เหมือนสั่งให้ client รัน command ของ dos เลย
ถ้าอย่างนั้น ถ้าผมเปลี่ยน command จาก "nbtstat" เป็น "format c:"
คราวนี้เวลา client เรียกหน้าเว็บนี้จาก server เครื่อง client ไม่เดี้ยงหมดเลยเหรอครับ
ปล.1 ผมอยากรู้ว่ามันทำได้จริงหรือ ถ้าทำได้มันอันตรายมากๆ
ปล.2 ผมยอมรับว่าถ้าเป็น winform สามารถทำได้แน่นอน เพราะมันสั่งรันในเครื่องตัวเอง แต่ webapp มันไม่เหมือนกันนะ
|
 |
 |
 |
 |
Date :
2010-01-09 16:34:48 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มาตอบคุณ tungman ครับ
คำสั่งนี้ รันที่เครื่อง server ครับ เหมือนคำสั่ง ping อะคับเช่นเครื่องเราเครื่อง A ping ไปเครื่อง B
เครื่อง B ไม่ได้ run โปรแกรมอะไรแต่เราก็ได้ message ตอบกลับจากเครื่อง B หลักการเดียวกัน
ครับ ปรกติอุปกรณ์ networkเขามี protocolที่ทำการแลกเปลี่ยนข้อมูลในระบบ network กันอยู่แล้ว
webform สามารถสร้าง โปรแกรมที่สามารถดู resource ของเครื่อข่าย server ได้ครับ เช่นเรา
สร้างโปรแกรม มาแล้ว up ขึ้น host ที่ไหนซักที่ เราสามารถดูได้ ชื่อserver,dns,ip,maxadress
sub netmark, แต่ถึงขั้น ให้ server format drive นี่ผมไม่แน่ใจครับ อย่างที่คุณ tungman บอก
ถ้าทำได้นี่เป็นเรื่องแน่ครับ ผมก็เพิ่งเริ่มศึกษา network programming ได้ไม่นาน ยังงูๆปลาๆ
อยู่ครับ
|
 |
 |
 |
 |
Date :
2010-01-09 21:46:50 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อืมพอจะเข้าใจแล้วครับ ต้องขอโทษด้วยตอนแรกคิดว่าจะใช้ http ไป request mac จากเครื่อง client
ก็เลยบอกว่าไม่ได้ ถ้าทำแบบนี้ก็ได้แหละครับ ให้ server gen network command ไป request ที่ client
คิดๆ ดูก็เคยเห็นโปรเจ็คทำนองนี้อยู่บ้างเหมือนกัน คือ ใช้เว็บสั่ง soft reset อุปกรณ์ network หรือ upload
visio ขึ้น server แล้วอ่าน sulotion ให้ gen command ไป config router ในเครือข่ายตาม solution
โดยไม่ต้องมานั่ง config ทีละตัว 
งั้นต้องขอโทษอีกทีครับ และยินดีที่ได้แลกเปลี่ยนความรู้ครับ 
|
 |
 |
 |
 |
Date :
2010-01-09 22:57:51 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ
|
 |
 |
 |
 |
Date :
2011-01-15 23:02:00 |
By :
+++ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมชอบกระทู้นี้จัง +1000000 เลยครับ ได้ความรู้เยอะเลย ขอบคุณทุกท่านครับ ^^
|
 |
 |
 |
 |
Date :
2011-07-26 10:41:29 |
By :
niphan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ชอบเหมือนกัน ได้ความรู้มากๆเลย
|
 |
 |
 |
 |
Date :
2011-07-26 11:40:14 |
By :
stricken |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ชอบมากเหมือนกันคับ
|
 |
 |
 |
 |
Date :
2011-10-22 21:54:39 |
By :
termja |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Run บน SharpDevelop 4.1 ได้ไหมครับ
|
 |
 |
 |
 |
Date :
2012-01-23 14:45:03 |
By :
acomscie |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code
<html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script id="clientEventHandlersJS" language="javascript">
function Button1_onclick()
{
var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True' And Not Description like '%wireless%' And Not Description like '%virtual%'" );
var e = new Enumerator (properties);
document.write("<table border=1>");
dispHeading();
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("<tr>");
document.write("<td>" + p.Caption + "</td>");
document.write("<td>" + p.IPFilterSecurityEnabled + "</td>");
document.write("<td>" + p.IPPortSecurityEnabled + "</td>");
document.write("<td>" + p.IPXAddress + "</td>");
document.write("<td>" + p.IPXEnabled + "</td>");
document.write("<td>" + p.IPXNetworkNumber + "</td>");
document.write("<td>" + p.MACAddress + "</td>");
document.write("<td>" + p.WINSPrimaryServer + "</td>");
document.write("<td>" + p.WINSSecondaryServer + "</td>");
document.write("</tr>");
}
document.write("</table>");
}
function dispHeading()
{
document.write("<thead>");
document.write("<td>Caption</td>");
document.write("<td>IPFilterSecurityEnabled</td>");
document.write("<td>IPPortSecurityEnabled</td>");
document.write("<td>IPXAddress</td>");
document.write("<td>IPXEnabled</td>");
document.write("<td>IPXNetworkNumber</td>");
document.write("<td>MACAddress</td>");
document.write("<td>WINSPrimaryServer</td>");
document.write("<td>WINSSecondaryServer</td>");
document.write("</thead>");
}
</script>
</head>
<body>
<INPUT id="Button1" type="button" value="Button" name="Button1" language="javascript" onclick="return Button1_onclick()">
</body>
</html>
|
 |
 |
 |
 |
Date :
2013-05-30 16:46:28 |
By :
Alpha TH |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|