C# - 간단하게 만들어 보는 리눅스의 nc(netcat) 프로그램
리눅스 도구에 보면 nc가 있는데,
nc - Unix, Linux Command
; https://www.tutorialspoint.com/unix_commands/nc.htm
윈도우에는 없군요. ^^ 검색해 보면 다음의 링크가 있지만,
netcat
; https://eternallybored.org/misc/netcat/
그래도 기본적인 기능이라면 C#으로 다음과 같이 쉽게 만들 수 있습니다.
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace netcat
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 2)
{
return;
}
if (Console.IsInputRedirected == false)
{
Console.WriteLine("No input redirected");
return;
}
int port = int.Parse(args[1]);
using (TcpClient client = new TcpClient(args[0], port))
{
using (NetworkStream ns = client.GetStream())
using (BinaryReader br = new BinaryReader(Console.OpenStandardInput()))
{
while (true)
{
byte[] buffer = br.ReadBytes(512);
if (buffer == null || buffer.Length == 0)
{
break;
}
ns.Write(buffer, 0, buffer.Length);
}
}
}
}
}
}
빌드한 후 이런 식으로 테스트할 수 있고,
C:\temp> echo "Hello!" | netcat localhost 9900
C:\temp> type data.txt | netcat localhost 9900
그럼 pipeline으로 연결된 "Hello!" 문자열을, 그리고 "data.txt" 파일의 내용을 netcat이 전달받아 "localhost:9900" TCP 서버에 전송합니다.
Github에도 소스 코드 및 빌드된 바이너리를 올려두었습니다.
stjeong / Utilities / netcat
; https://github.com/stjeong/Utilities/tree/master/netcat
netcat.zip
; https://github.com/stjeong/Utilities/blob/master/Binaries/netcat.zip
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]