private sshCommandOutput run(string command) { sshCommandOutput result = new sshCommandOutput(); try { if (_sshClient == null) { logMessage("Thread " + Thread.CurrentThread.ManagedThreadId + " opening new ssh connection to " + cred.remoteHost); ConnectionInfo ConnNfo = new ConnectionInfo(cred.remoteHost, cred.username, new AuthenticationMethod[] { new PasswordAuthenticationMethod(cred.username, cred.password) }); ConnNfo.Timeout = TimeSpan.FromSeconds(60); _sshClient = new SshClient(ConnNfo); _sshClient.KeepAliveInterval = TimeSpan.FromMilliseconds(30000d); _sshClient.ConnectionInfo.RetryAttempts = 3; //_sshClient.ErrorOccurred += ... FIXME (only for async calls) logMessage("Ssh client connected to " + cred.remoteHost); logMessage("retry attempts =" + _sshClient.ConnectionInfo.RetryAttempts); logMessage("KeepAliveInterval = " + _sshClient.KeepAliveInterval); } if (!_sshClient.IsConnected) { _sshClient.Connect(); } #if true SshCommand o = _sshClient.RunCommand(command); result.ExitStatus = o.ExitStatus; result.Error = o.Error; result.Result = o.Result; #else var cmdStub = _sshClient.CreateCommand(command); var asyncResult = cmdStub.BeginExecute(null, null); while (!asyncResult.IsCompleted) { Thread.Sleep(100); } cmdStub.EndExecute(asyncResult); result.ExitStatus = cmdStub.ExitStatus; result.Error = cmdStub.Error; result.Result = cmdStub.Result; #endif _sshClient.Disconnect(); } catch (Exception e) { logMessage(e.Message); result.ExitStatus = 1; result.Error = e.Message; result.Result = ""; } return(result); }
public bool runCommand(string cmd) { logMessage("Thread " + Thread.CurrentThread.ManagedThreadId + " executing command \"" + cmd + "\""); output = run(cmd); if (output.ExitStatus != 0) { logMessage((output.ExitStatus == 0 ? "" : "Return status=" + output.ExitStatus + "\n") + (output.Result != "" ? "stdout=" + output.Result : "") + (output.Error != "" ? "stderr=" + output.Error : "")); } return(output.ExitStatus == 0); }
/// <summary> /// Return a list of apsim versions (in standalone singularity containers) that peter has built. /// </summary> /// <returns>The remote versions.</returns> public List <string> getRemoteVersions() { List <string> result = new List <string>(); output = run("ls /home/uqpdevo1/*.sapp"); if (output.ExitStatus == 0) { foreach (string sapp in output.Result.Split(new[] { '\n' }, StringSplitOptions.None)) { if (sapp.IndexOf('/') >= 0) { string nicename = sapp.Substring(sapp.LastIndexOf('/') + 1); result.Add(nicename.Substring(0, nicename.Length - 5)); } } } //Console.WriteLine ("remote=" + output.Result + "\nStatus=" + output.ExitStatus); return(result); }