none
ssh connection using c# , putty or plink RRS feed

  • 问题

  • I have spend three days on this no luck,  I need to open -ssh connection to a remote server and run few commands and get output.  I have used plink and save session in putty and write txt file for commands then open cmd.exe and run plink batch file. this is working if I view web page localhost but not in the web site....

    it's seems like a permision problem or connecting putty saved session...

    here is my code

    string strOutput;
                    
                    ProcessStartInfo psi = new ProcessStartInfo("cmd.exe"); //(@"C:\WINDOWS\system32\cmd.exe");     
                    
                    psi.UseShellExecute = false;
                    psi.ErrorDialog = false;
                    
                    psi.CreateNoWindow = true;
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    
                    psi.RedirectStandardError = true;
                    psi.RedirectStandardInput = true;
                    psi.RedirectStandardOutput = true;
                  
                    psi.WorkingDirectory = "C:\\Plink\\";
                   
                    Process plinkProcess = new Process();
                    plinkProcess.StartInfo = psi;
                    plinkProcess.Start();
                   
                    StreamWriter inputWriter = plinkProcess.StandardInput;
                    StreamReader outputReader = plinkProcess.StandardOutput;
                    StreamReader errorReader = plinkProcess.StandardError;
    
                    string filename = connectusername.Split('@')[0];
                    StreamWriter streamw = null;
                    string strfile = "C:\\Plink\\testfolder\\temp" + filename + ".txt";
                   
    
                    if (File.Exists(strfile))
                    {
                        File.Delete(strfile);
    
                    }
                    streamw = File.CreateText(strfile);
                  
                    streamw.WriteLine("cd pm/myfolder"); //change directory
                    streamw.WriteLine("echo hellow"); //command here 
                    streamw.WriteLine("exit");
                   
    
                    plinkProcess.StandardInput.WriteLine("PLINK -ssh hostname -l pcube -batch -m " + strfile);
    
                 
                    plinkProcess.StandardInput.Close();
                 
                   
                    strOutput = outputReader.ReadToEnd();
              
                    string errstr=  errorReader.ReadToEnd();
     
                    //logic to get outputtoo human readable
    
                    streamw.Close();
                    plinkProcess.Close();
                    outputReader.Close();
                    errorReader.Close();
                    //build_chart(dt, packageid);
                   
    

    I have take off the working directory and line and set system32/cmd same error Unable to connect to server, host does not exist

    any ideas what to do?

     


    2012年1月9日 13:35

答案