string un = "weipan";
        string pw = "weipan";        string ssh_mach = "plink -ssh 118.88.128.133 -l r -pw r";
        //创建用户并设置密码
        //sudo useradd -d /home/r -s /bin/bash -m r
        //sudo su root -c 'echo "r:r" | chpasswd'
        string add_user = "sudo useradd -d /home/" + un + " -s /bin/bash -m " + un;
        string add_pass = "sudo su root -c 'echo \"" + un + ":" + pw + "\" | chpasswd'";        string binStr = System.Web.Hosting.HostingEnvironment.MapPath("~/plink.exe");
        System.Diagnostics.Process exep = new System.Diagnostics.Process();
        exep.StartInfo.FileName = binStr;  //FileName 可执行程序文件名
        //exep.StartInfo.Arguments = cmdStr;  //Arguments 程序参数,以字符串形式输入 
        
        exep.StartInfo.CreateNoWindow = true;  //CreateNoWindow 是否不需要创建窗口 
        exep.StartInfo.UseShellExecute = false;   //UseShellExecute 是否需要系统shell调用程序
        //pro.StartInfo.RedirectStandardInput = true;
        exep.StartInfo.RedirectStandardInput = true;
        exep.StartInfo.RedirectStandardOutput = true;
        exep.StartInfo.Arguments = ssh_mach;
        exep.Start();
       
        exep.StandardInput.WriteLine(ssh_mach);
        exep.StandardInput.Flush();
        exep.StandardInput.WriteLine(add_user);
        exep.StandardInput.Flush();
        //exep.StandardInput.WriteLine("exit");
        exep.StandardInput.WriteLine(add_pass);
        exep.StandardInput.Flush();        TextBox1.Text = exep.StandardOutput.ReadToEnd().ToString();
        //exep.StandardOutput.Read();
        //exep.Exited
        exep.WaitForExit();//关键,等待外部程序退出后才能往下执行
        exep.Close();以上是我的代码,我想调用plink.exe(用来连接linux机器操作一些linux命令)这个文件,然后连接到linux机器上,调用一些命令,现在能连接到linxu机器上,但是无法执行linxu命令
PS:我这个东西做完需要放到linux机器上,所以请不要告诉我调用cmd.exe的方法!

解决方案 »

  1.   

    问题解决了,以下是正确可以连接的代码,红色部分为缺少的部分,希望对你们有帮助,具体的愿意可以去MSDN上看,
    http://social.msdn.microsoft.com/Forums/zh-CN/csharpgeneral/thread/c14d5871-28c9-4bfc-a56d-d3d3a50395f4  ProcessStartInfo pInfo = new ProcessStartInfo();
                    pInfo.Arguments = "-ssh username@" + su + " -pw passwd";
                    pInfo.FileName = Server.MapPath("~/plink.exe");
                    Process p = new Process();
                    p.StartInfo = pInfo;
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.CreateNoWindow = true;
                    p.Start();                StreamWriter myStreamWriter = p.StandardInput;
                    myStreamWriter.WriteLine("sudo useradd -d /home/wp -s /bin/bash -m wp");
                    myStreamWriter.WriteLine("sudo su root -c 'echo \"wp:wp\" | chpasswd'");
                   
                    myStreamWriter.WriteLine("exit");
                    StreamReader ss = p.StandardOutput;                pand = ss.ReadLine().ToString();
                    p.StandardInput.Flush();
                    p.WaitForExit();
                    p.Close();