other_program_languages/c#

C# ssh client program

AI Coder 2022. 11. 24. 21:06

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

 

using Renci.SshNet;  //Renci.sshNet 을 사용한다. 


//방법 1
using (var sshClient = new SshClient("111.111.111.111.", "root", "root")) //IP, ID, PW
{
	sshClient.Connect();
    //비교적 간단한 linux command를 한번 실행시키고, 그 결과를 전부 
    //받아오기 위해서 사용할 수 있다. 
    var tel = sshClient.RunCommand("ls -l");
    string resultStr = tel.Result;
    sshClient.Disconnect();
}


//방법 2

using (var sshClient = new SshClient("111.111.111.111.", "root", "root")) //IP, ID, PW
{
	sshClient.Connect();
    Renci.SshNet.SshCommand sshCommand = sshClient.CreateCommand("/home/script/test.sh");
    var asynch = sshCommand.BeginExecute();
    var reader = new StreamReader(sshCommand.OutputStream);
    while (!asynch.IsCompleted)
    {
        //sh 파일과 같이 비교적 길게 실행되는 개체를 실행시키고, 
        //그 진행 상황을 주기적으로 가져와 뿌려주는 방법
        //test.sh 가 terminal 에 뿌리는 내용을 계속 가지고오기 때문에, 
        //test.sh 의 진행상황을 받아와서 Gui 에 뿌릴 수 있다. 
 		var result = reader.ReadToEnd();  
 		if (string.IsNullOrEmpty(result))
        	continue;
    }
    sshClient.Disconnect();
}

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

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