Nice programing

PuTTY를 사용하여 Windows에서 Linux에서 명령 실행 자동화

nicepro 2020. 11. 26. 19:52
반응형

PuTTY를 사용하여 Windows에서 Linux에서 명령 실행 자동화


내가 실행해야하는 시나리오가 창에서 (다른 파일 이름으로) 자주 리눅스 쉘 명령을. 이를 위해 PuTTY 및 WinSCP를 사용하고 있습니다 (로그인 이름 및 암호 필요). 파일은 WinSCP를 통해 Linux 시스템의 미리 정의 된 폴더에 복사 된 다음 PuTTY에서 명령이 실행됩니다. 프로그램을 통해 이것을 자동화 할 수있는 방법이 있습니까? 이상적으로는 Windows에서 파일을 마우스 오른쪽 버튼으로 클릭하고 파일을 원격 컴퓨터에 복사하는 명령을 실행하고 파일 이름을 인수로 사용하여 미리 정의 된 명령 (PuTTy에서)을 실행하고 싶습니다.


자동 로그인을위한 일반적인 방법에 보안 문제가있을 수 있습니다. 가장 쉬운 방법 중 하나가 아래에 설명되어 있습니다.

그리고 퍼티 UI에서 Connection> SSH> 명령을 실행하는 부분은 원격 명령을위한 필드가 있습니다.

4.17 SSH 패널

SSH 패널에서는 SSH 세션에만 적용되는 옵션을 구성 할 수 있습니다.

4.17.1 서버에서 특정 명령 실행

SSH에서는 서버에서 일반 셸 세션을 실행할 필요가 없습니다. 대신 단일 특정 명령 (예 : 메일 사용자 에이전트)을 실행하도록 선택할 수 있습니다. 이렇게하려면 "원격 명령"상자에 명령을 입력하십시오. http://the.earth.li/~sgtatham/putty/0.53/htmldoc/Chapter4.html

요컨대, 귀하의 답변은 아래 텍스트와 비슷할 수 있습니다.


Putty는 일반적으로 "plink"유틸리티와 함께 ​​제공됩니다.
이것은 본질적으로 Windows .exe로 구현 된 "ssh"명령 줄 명령입니다.
"명령 줄 도구 plink 사용"아래의 putty 매뉴얼에 잘 설명되어 있습니다.

다음과 같은 명령을 래핑하면됩니다.

plink root@myserver /etc/backups/do-backup.sh

.bat 스크립트에서.

세미콜론과 같은 공통 셸 구성을 사용하여 여러 명령을 실행할 수도 있습니다. 예 :

plink read@myhost ls -lrt /home/read/files;/etc/backups/do-backup.sh

TCL 스크립트를 작성하고 해당 Linux 시스템에 대한 SSH 세션을 설정하고 명령을 자동으로 실행할 수 있습니다. 짧은 자습서는 http://wiki.tcl.tk/11542확인 하십시오 .


세션을 시작할 때 putty 세션을 만들고 서버에서 스크립트를 자동로드 할 수 있습니다.

putty -load "sessionName" 

원격 명령에서 원격 스크립트를 가리 킵니다.


WinSCP를 사용하여 두 작업 (업로드 및 명령 실행)을 모두 수행 할 수 있습니다. 다음과 같은 WinSCP 스크립트를 사용하십시오.

option batch abort
option confirm off
open your_session
put %1%
call script.sh
exit

call명령 참조 :
https://winscp.net/eng/docs/scriptcommand_call

%1%구문 참조 :
https://winscp.net/eng/docs/scripting#syntax

그런 다음 다음과 같은 스크립트를 실행할 수 있습니다.

winscp.exe /console /script=script_path\upload.txt /parameter file_to_upload.dat

실제로 위 명령에 대한 바로 가기를 Windows 탐색기의 보내기 메뉴에 넣을 수 있으므로 파일을 마우스 오른쪽 단추로 클릭하고 WinSCP를 사용하여 보내기> 업로드 로 이동 한 다음 원격 명령 실행 (= 바로 가기 이름 ).

이를 위해 폴더로 이동 %USERPROFILE%\SendTo하여 다음 대상으로 바로 가기를 만듭니다.

winscp_path\winscp.exe /console /script=script_path\upload.txt /parameter %1

Explorer의 "보내기"메뉴에서 항목 만들기를 참조하십시오 .


암호:

using System;
using System.Diagnostics;
namespace playSound
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(args[0]);

            Process amixerMediaProcess = new Process();
            amixerMediaProcess.StartInfo.CreateNoWindow = false;
            amixerMediaProcess.StartInfo.UseShellExecute = false;
            amixerMediaProcess.StartInfo.ErrorDialog = false;
            amixerMediaProcess.StartInfo.RedirectStandardOutput = false;
            amixerMediaProcess.StartInfo.RedirectStandardInput = false;
            amixerMediaProcess.StartInfo.RedirectStandardError = false;
            amixerMediaProcess.EnableRaisingEvents = true;

            amixerMediaProcess.StartInfo.Arguments = string.Format("{0}","-ssh username@"+args[0]+" -pw password -m commands.txt");
            amixerMediaProcess.StartInfo.FileName = "plink.exe";
            amixerMediaProcess.Start();


            Console.Write("Presskey to continue . . . ");
            Console.ReadKey(true);
    }
}

}

샘플 commands.txt :

추신

Link: https://huseyincakir.wordpress.com/2015/08/27/send-commands-to-a-remote-device-over-puttyssh-putty-send-command-from-command-line/


Try MtPutty, you can automate the ssh login in it. Its a great tool especially if you need to login to multiple servers many times. Try it here

Another tool worth trying is TeraTerm. Its really easy to use for the ssh automation stuff. You can get it here. But my favorite one is always MtPutty.


Here is a totally out of the box solution.

  1. Install AutoHotKey (ahk)
  2. Map the script to a key (e.g. F9)
  3. In the ahk script, a) Ftp the commands (.ksh) file to the linux machine

    b) Use plink like below. Plink should be installed if you have putty.

plink sessionname -l username -pw password test.ksh

or

plink -ssh example.com -l username -pw password test.ksh

All the steps will be performed in sequence whenever you press F9 in windows.


In case you are using Key based authentication, using saved Putty session seems to work great, for example to run a shell script on a remote server(In my case an ec2).Saved configuration will take care of authentication.

C:\Users> plink saved_putty_session_name path_to_shell_file/filename.sh

Please remember if you save your session with name like(user@hostname), this command would not work as it will be treated as part of the remote command.

참고URL : https://stackoverflow.com/questions/6147203/automating-running-command-on-linux-from-windows-using-putty

반응형