private String sudoImdtCmd(String cmd, long timeOut = 2000000, procCmdRsltDlg procCmdRslt = null) { if (isConnected != true) { return(null); } Action act; String tmpRead; StringBuilder result = new StringBuilder(); shell.Read(); shell.WriteLine("sudo " + cmd); Thread.Sleep(200); shell.WriteLine(Password); while (true) { tmpRead = shell.ReadLine(new TimeSpan(timeOut)); if (tmpRead == null) { break; } if (procCmdRslt != null) { procCmdRslt(tmpRead); } result.Append(tmpRead); act = new Action(() => { TbxTest.AppendText(tmpRead); TbxTest.AppendText("\n"); TbxTest.ScrollToEnd(); }); this.Dispatcher.Invoke(act); } if (result.Length > 0) { result.Remove(0, cmd.Length); } return(result.ToString()); }
private String sudoWaitCmd(String cmd, procCmdRsltDlg procCmdRslt = null) { if (isConnected != true) { return(null); } try { Action act; String tmpRead; StringBuilder result = new StringBuilder(); shell.Read(); shell.WriteLine("sudo " + cmd); //result.Append(shell.Read()); //if(result.ToString().Contains("password for")) //{ // shell.WriteLine(Password); //} while (true) { tmpRead = shell.Read(); result.Append(tmpRead); act = new Action(() => { TbxTest.AppendText(tmpRead); TbxTest.AppendText("\n"); TbxTest.ScrollToEnd(); }); this.Dispatcher.Invoke(act); // password if (result.ToString().EndsWith("password for " + Username + ": ")) { shell.WriteLine(Password); Thread.Sleep(500); continue; } if (result.ToString().EndsWith("密码: ")) { shell.WriteLine(Password); Thread.Sleep(500); continue; } // end mcl = newLineRegex.Matches(result.ToString()); if (mcl.Count > 0) { break; } Thread.Sleep(100); } return(result.ToString()); } catch (Exception ex) { throw new Exception(String.Format("执行命令失败,原因:{0}", ex.Message)); } }
private String doWaitCmd(String cmd, procCmdRsltDlg procCmdRslt = null) { if (isConnected != true) { return(null); } Action act; String tmpRead; String newLineTmp = ""; String[] tmpStr; StringBuilder result = new StringBuilder(); tmpRead = shell.Read(); shell.WriteLine(cmd); Thread.Sleep(10); while (true) { tmpRead = shell.Read(); // result.Append(tmpRead); tmpStr = tmpRead.Split('\n'); if (tmpStr.Length > 1) { if (procCmdRslt != null) { procCmdRslt(newLineTmp + tmpStr[0]); } mcl = newLineRegex.Matches(newLineTmp + tmpStr[0]); if (mcl.Count > 0) { break; } for (int i = 0; i < tmpStr.Length - 1; i++) { if (procCmdRslt != null) { procCmdRslt(tmpStr[i]); } mcl = newLineRegex.Matches(tmpStr[i]); if (mcl.Count > 0) { break; } } newLineTmp = tmpStr[tmpStr.Length - 1]; } else { newLineTmp += tmpStr[0]; } act = new Action(() => { TbxTest.AppendText(tmpRead); TbxTest.ScrollToEnd(); }); this.Dispatcher.Invoke(act); Thread.Sleep(1); } return(result.ToString()); }