other_program_languages/c#

c# telnet client program

MasterOfAI 2022. 11. 24. 21:15

해당 프로그램은 Windows 10 위에서 실행되는 C# 으로 만들어진 telnet client 로, Ubnunt 에서 실행되는 telnet server 로 접속하여, shell script 를 실행하고, 그 결과를 출력하는 작업을 진행한다. 

 

using System.Net;
using System.Net.Sockets;

//IP, Port (telnet default : 23)
var ipepBottom = new IPEndPoint(IPAddress.Parse("111.111.111.111"), 23);  
using (Socket client = new Socket(AddressFamily.InterNetwork, 
       SocketType.Stream, ProtocolType.Tcp))
{
	client.Connect(ipepBottom);
    
    //Task 부분은 telet 접속후 처음 호출되는 부분으로, Server 로 부터 값을 읽어서 logging 
    new Task(() =>
    {
    	while (true)
        {
        	var binary = new Byte[1024];
            client.Receive(binary);
            var data = Encoding.ASCII.GetString(binary).Trim('\0');
            if (String.IsNullOrWhiteSpace(data))
            	continue;
            //to log data here
        }

    }).Start();
    
    //Command 송부
    client.Send(Encoding.ASCII.GetBytes("ls -l \r\n"));
    
    //결과 읽어서 logging 
    while (true)
    {
        var binary = new Byte[1024];
        client.Receive(binary);
        var data = Encoding.ASCII.GetString(binary).Trim('\0');
        if (String.IsNullOrWhiteSpace(data))
            continue;
        //to log data here
    }
}

'other_program_languages > c#' 카테고리의 다른 글

c# sftp 사용 ubuntu 로 file 전송  (0) 2022.11.24
C# ping program  (0) 2022.11.24
C# ssh client program  (0) 2022.11.24