Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# telnet

Status
Not open for further replies.

johnv20

Programmer
Sep 26, 2001
292
US
Hi,
I need to write a very simple app to use telnet to connect to a router & retrieve some mac address information - does anyone have any clue how to do this..

Sorry if this is very simple but I have no clue on how to go about this....
 
You should be able to launch telnet as a process and capture the input/output streams.

look into the Process class.
 
I wouldn't recommend running the telnet client to do this - that'd be a bit convoluted. You should use sockets to connect to the telnet server (on port 23 usually) and communicate with it that way. Parsing the server's telnet responses won't be easy, but you could save the data to a file and figure out how to locate the important data.

Look up the TcpClient class and see if that does what you need. You'll have to connect to the server, set up a stream, then read and write from that stream to interact with it.
 
Are you getting anywhere with this. I do not know much about it, but I do think it is interesting.
Code:
//source
//[URL unfurl="true"]http://www-new.experts-exchange.com/Programming/Languages/C_Sharp/Q_21735460.html?qid=21735460&qid=21735460[/URL]
using System;
class GetMacAddress
{    
    [System.Runtime.InteropServices.DllImport("iphlpapi.dll", ExactSpelling=true)]
    public static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref int PhyAddrLen);

    public static string GetMacAddressByIP(System.Net.IPAddress ipAddress)
    {
        byte[] macBytes = new byte[6];
        int length = 6;
        SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macBytes, ref length);
        return BitConverter.ToString(macBytes, 0, 6);
    }
    
    static void Main(string[] args)
    {
       Console.WriteLine("GetMacAddress");
       System.Net.IPAddress ipa = System.Net.IPAddress.Parse("192.168.1.1");
       Console.WriteLine(GetMacAddressByIP(ipa));
    }
}
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top