Execute Unix commands using putty in C# [duplicate]

孤人 提交于 2019-12-06 15:43:18

Wouldn't it be easier to use the command line Plink (PuTTY Link) instead of the GUI? I believe that it is more suitable for automated tasks.

See http://the.earth.li/~sgtatham/putty/0.63/htmldoc/Chapter7.html#plink for example usage.

I will quote the relevant part here, just in case the link goes bad:

7.2.2 Using Plink for automated connections

More typically Plink is used with the SSH protocol, to enable you to talk directly to a program running on the server. To do this you have to ensure Plink is using the SSH protocol. You can do this in several ways:

Use the -ssh option as described in section 7.2.1. Set up a PuTTY saved session that describes the server you are connecting to, and that also specifies the protocol as SSH. Set the Windows environment variable PLINK_PROTOCOL to the word ssh. Usually Plink is not invoked directly by a user, but run automatically by another process. Therefore you typically do not want Plink to prompt you for a user name or a password.

Next, you are likely to need to avoid the various interactive prompts Plink can produce. You might be prompted to verify the host key of the server you're connecting to, to enter a user name, or to enter a password.

To avoid being prompted for the server host key when using Plink for an automated connection, you should first make a manual connection (using either of PuTTY or Plink) to the same server, verify the host key (see section 2.2 for more information), and select Yes to add the host key to the Registry. After that, Plink commands connecting to that server should not give a host key prompt unless the host key changes.

To avoid being prompted for a user name, you can:

Use the -l option to specify a user name on the command line. For example, plink login.example.com -l fred. Set up a PuTTY saved session that describes the server you are connecting to, and that also specifies the username to log in as (see section 4.14.1). To avoid being prompted for a password, you should almost certainly set up public-key authentication. (See chapter 8 for a general introduction to public-key authentication.) Again, you can do this in two ways:

Set up a PuTTY saved session that describes the server you are connecting to, and that also specifies a private key file (see section 4.20.8). For this to work without prompting, your private key will need to have no passphrase. Store the private key in Pageant. See chapter 9 for further information. Once you have done all this, you should be able to run a remote command on the SSH server machine and have it execute automatically with no prompting:

Z:\sysosd>plink login.example.com -l fred echo hello, world hello, world

Z:\sysosd> Or, if you have set up a saved session with all the connection details:

Z:\sysosd>plink mysession echo hello, world hello, world

Z:\sysosd> Then you can set up other programs to run this Plink command and talk to it as if it were a process on the server machine.

The following code gives the solution

class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo startinfo = new ProcessStartInfo();
            startinfo.FileName = @"D:\Plink0.57\plink-057.exe";
            startinfo.Arguments = "-ssh mahi@192.168.37.129  -pw mahi";
            Process process = new Process();
            process.StartInfo = startinfo;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.Start();
            process.StandardInput.WriteLine("ls");
            process.WaitForExit();
            Console.ReadKey();
        }
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!