private void InstallAgentAndOnboard(SshClient sshClient, ref ServerListItem server) { string installcmd = server.Architecture == ServerArchitecture.x64 ? "sh /home/" + server.Username + "/" + x64Agent + " --install -w " + txtWorkspaceId.Text + " -s " + txtSharedKey.Text + "\n": "sh /home/" + server.Username + "/" + x86Agent + " --install -w " + txtWorkspaceId.Text + " -s " + txtSharedKey.Text + "\n"; IDictionary <Renci.SshNet.Common.TerminalModes, uint> termkvp = new Dictionary <Renci.SshNet.Common.TerminalModes, uint> { { Renci.SshNet.Common.TerminalModes.ECHO, 53 } }; ShellStream shell = sshClient.CreateShellStream("xterm", 800, 240, 1024, 720, 1024, termkvp); //ShellStream shell = sshClient.CreateShellStream("Tail", 0, 0, 0, 0, 1024); StreamWriter wr = new StreamWriter(shell); StreamReader rd = new StreamReader(shell); wr.AutoFlush = true; wr.WriteLine("su"); string rep = shell.Expect(new Regex(@"([$#>:])"), new TimeSpan(0, 0, 3)); //expect password or user prompt //check to send password if (rep.EndsWith("Password: "******"[$#>]"), new TimeSpan(0, 0, 3)); server.Log += rep; var cmd = sshClient.CreateCommand(installcmd); var result = cmd.BeginExecute(); using (var reader = new StreamReader(cmd.OutputStream, Encoding.UTF8, true, 1024, true)) { while (!result.IsCompleted || !reader.EndOfStream) { string line = reader.ReadLine(); if (line != null) { server.Log += line; } } } } }
private bool CheckPrereqs(SshClient sshClient, ref ServerListItem server) { server.Prereqs = new List <Package>(); Package glibc = QueryPackage(sshClient, server.Distro, "glibc"); if (glibc != null) { server.Prereqs.Add(glibc); } else { return(false); } Package openssl = QueryPackage(sshClient, server.Distro, "openssl"); if (openssl != null) { server.Prereqs.Add(openssl); } else { return(false); } Package curl = QueryPackage(sshClient, server.Distro, "curl"); if (curl != null) { server.Prereqs.Add(curl); } else { return(false); } Package pythonlibs = QueryPackage(sshClient, server.Distro, "python-libs"); if (pythonlibs != null) { server.Prereqs.Add(pythonlibs); } else { return(false); } Package pam = QueryPackage(sshClient, server.Distro, "pam"); if (pam != null) { server.Prereqs.Add(pam); } else { return(false); } //Log: All good... return(true); }
private void btnDeploy_Click(object sender, EventArgs e) { //Logic of operation. A little messy but I'm short on time... if (rdAutoDownload.Checked) { DownloadAgents(); } //Loop on the servers in our list foreach (var item in serverlistDataSource) { ServerListItem server = item as ServerListItem; //In the future we should also support cert login var connectionInfo = new ConnectionInfo(server.IPAddress, server.Port, server.Username, new PasswordAuthenticationMethod(server.Username, server.Password)); connectionInfo.Encoding = Encoding.GetEncoding("ISO-8859-1"); using (var sshClient = new SshClient(connectionInfo)) { sshClient.Connect(); server.Distro = CheckDistro(sshClient); server.Architecture = CheckArchitecture(sshClient); if (chkInstallPrerequisites.Checked) { if (!CheckPrereqs(sshClient, ref server)) { //Log: Error, prereqs not ready continue; } TransferPrereqs(sshClient, server.Distro); InstallPrereqs(sshClient, server.Distro); } if (rdAutoDownload.Checked) { TransferAgents(sshClient, server.Architecture); } if (chkInstallAgent.Checked && chkJoinLogAnalytics.Checked && chkUseProxy.Checked) { InstallAgentAndOnboardAndSetProxy(); } else if (chkInstallAgent.Checked && chkJoinLogAnalytics.Checked) { InstallAgentAndOnboard(sshClient, ref server); } else if (!chkInstallAgent.Checked && chkJoinLogAnalytics.Checked && chkUseProxy.Checked) { SwitchWorkspaceAndSetProxy(); } else if (!chkInstallAgent.Checked && chkJoinLogAnalytics.Checked && !chkUseProxy.Checked) { SwitchWorkspace(); } else if (!chkInstallAgent.Checked && !chkJoinLogAnalytics.Checked && chkUseProxy.Checked) { SetProxy(); } sshClient.Disconnect(); } } }