private void InstallPrereqs(SshClient sshClient, ServerDistro distro) { string prereqsPath = (distro == ServerDistro.RPM) ? rpmPackages : debianPackages; List <string> files = ListPrereqsFiles(prereqsPath); foreach (string file in files) { string path = "/home/" + sshClient.ConnectionInfo.Username + "/" + file.Replace(@"\", "/"); string cmdText = distro == ServerDistro.RPM ? "yum install -y " + path: "apt install -y " + path; var cmd = sshClient.CreateCommand(cmdText); 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) { //Log: result } } } cmd.EndExecute(result); } }
private void TransferPrereqs(SshClient sshClient, ServerDistro distro) { string prereqsPath = (distro == ServerDistro.RPM) ? rpmPackages : debianPackages; using (var sftpClient = new SftpClient(sshClient.ConnectionInfo)) { sftpClient.Connect(); List <string> files = ListPrereqsFiles(prereqsPath); foreach (string file in files) { using (var fileStream = new FileStream(file, FileMode.Open)) { Console.WriteLine("Uploading {0} ({1:N0} bytes)", file, fileStream.Length); sftpClient.BufferSize = 4 * 1024; // bypass Payload error large files string path = file.Remove(file.IndexOf(Path.GetFileName(file))).Replace(@"\", "/"); string filename = Path.GetFileName(file); string[] dirs = path.TrimEnd('/').Split('/'); foreach (var dir in dirs) { try { if (!sftpClient.Exists(dir)) { sftpClient.CreateDirectory(dir); } sftpClient.ChangeDirectory(dir); } catch { //Log: Directory already exists } } if (sftpClient.Exists(filename)) { sftpClient.UploadFile(fileStream, filename); } sftpClient.ChangePermissions(filename, 755); sftpClient.ChangeDirectory("/home/" + sshClient.ConnectionInfo.Username); } } } ////chmod +x for the file to be executable //var cmd = sshClient.CreateCommand("chmod +x ./" + Path.GetFileName(agentPath)); //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) // { // //Log: result // } // } //} //cmd.EndExecute(result); }
private Package QueryPackage(SshClient sshClient, ServerDistro distro, string package) { string query = string.Empty; switch (distro) { case ServerDistro.RPM: query = "rpm -qa --queryformat '%{NAME};%{VERSION};%{RELEASE};%{ARCH}\n' | grep '^" + package + ";'"; break; case ServerDistro.Debian: query = "dpkg-query -W -f='${binary:Package};${Version};${Version};${Architecture}\n' " + package; break; default: //Log: Might wanna try both return(null); } var cmd = sshClient.CreateCommand(query); 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) { cmd.EndExecute(result); return(Package.Parse(line)); } } } cmd.EndExecute(result); //Log: Package unavailable return(null); }