public override void RemoveFiles(SynoReportViaSSH session, IList <ConsoleFileInfo> dsm_databases) { using (SshClient scr = new SshClient(session.Host, "root", session.Password)) { scr.Connect(); foreach (var db in dsm_databases) { RemoveFile(session, db, ConsoleCommandMode.Directly, scr); } scr.Disconnect(); } }
private void ExperimentalSudo(SynoReportViaSSH session, string command) { try { using (SshClient sshClient = new SshClient(session.ConnectionInfo)) { sshClient.Connect(); IDictionary <TerminalModes, uint> termkvp = new Dictionary <TerminalModes, uint> { { TerminalModes.ECHO, 53 } }; ShellStream shellStream = sshClient.CreateShellStream("xterm", 80, 24, 800, 600, 1024, termkvp); //Get logged in string rep = shellStream.Expect(new Regex(@"[$>]")); //expect user prompt //this.writeOutput(results, rep); // //send command shellStream.WriteLine("sudo " + command); rep = shellStream.Expect(new Regex(@"([$#>:])")); //expect password or user prompt //this.writeOutput(results, rep); //check to send password if (rep.Contains(":")) { //send password shellStream.WriteLine(session.Password); rep = shellStream.Expect(new Regex(@"[$#>]"), new TimeSpan(0, 0, 10)); //expect user or root prompt //this.writeOutput(results, rep); if (rep == null) { System.Diagnostics.Debug.WriteLine("sudo action failed?"); } else { System.Diagnostics.Debug.WriteLine("{0}\r\n{1}", command, rep); } } sshClient.Disconnect(); } }//try to open connection catch (Exception ex) { System.Console.WriteLine(ex.ToString()); throw; } }
public override void RemoveFiles(SynoReportViaSSH session, IList <ConsoleFileInfo> dsm_databases) { using (SshClient scr = new SshClient(session.ConnectionInfo)) { scr.Connect(); foreach (var db in dsm_databases) { RemoveFile(session, db, session.RmExecutionMode, scr); } scr.Disconnect(); } }
internal string RunCommand(SynoReportViaSSH connection, string command, ConsoleCommandMode mode = ConsoleCommandMode.Directly, SshClient session = null) { switch (mode) { case ConsoleCommandMode.Sudo: return(session.RunCommand(string.Format(sudo, command)).Result); case ConsoleCommandMode.InteractiveSudo: ExperimentalSudo(connection, command); return(string.Empty); default: return(session.RunCommand(command).Result); } }
public override void RemoveFiles(SynoReportViaSSH session, IList <ConsoleFileInfo> dsm_databases) { string scriptName = "SynoDuplicateFoldersRemoveSADB.sh"; if (dsm_databases.Count > 0) { string script = this.HomePath + scriptName; MemoryStream ms = null; //StreamWriter sw = null; ms = new MemoryStream(); using (StreamWriter sw = new StreamWriter(ms)) { sw.Write("#!/bin/bash\n"); foreach (var file in dsm_databases) { sw.Write(base.RemoveFileCommand(session, file)); sw.Write("\n"); } sw.Flush(); ms.Seek(0, SeekOrigin.Begin); using (ScpClient cp = new ScpClient(session.ConnectionInfo)) { //System.Windows.Forms.MessageBox.Show(script); cp.Connect(); cp.Upload(ms, script); cp.Disconnect(); ms = null; } using (SshClient scr = new SshClient(session.ConnectionInfo)) { scr.Connect(); RunCommand(session, "chmod +x " + script, session.RmExecutionMode, scr); RunCommand(session, "./" + scriptName, session.RmExecutionMode, scr); RunCommand(session, RemoveFileCommand(script), session.RmExecutionMode, scr); scr.Disconnect(); } } } }
internal void RemoveFile(SynoReportViaSSH connection, ConsoleFileInfo file, ConsoleCommandMode mode, SshClient session = null) { RunCommand(connection, RemoveFileCommand(connection, file), mode, session); }
internal string RemoveFileCommand(SynoReportViaSSH connection, ConsoleFileInfo file) { return(RemoveFileCommand(connection.SynoReportHome + file.Path)); }
public abstract void RemoveFiles(SynoReportViaSSH session, IList <ConsoleFileInfo> dsm_databases);
public abstract List <ConsoleFileInfo> GetDirectoryContentsRecursive(SshClient client, SynoReportViaSSH session, bool Disconnect = true);
internal void RemoveFile(SynoReportViaSSH connection, ConsoleFileInfo file, ConsoleCommandMode mode, SshClient session = null) { RunCommand(connection, string.Format("rm {0}", connection.SynoReportHome.Substring(0, connection.SynoReportHome.Length - "synoreport/".Length) + file.Path.Substring(1)), mode, session); }
public override List <ConsoleFileInfo> GetDirectoryContentsRecursive(SshClient client, SynoReportViaSSH session, bool Disconnect = true) { List <ConsoleFileInfo> result = new List <ConsoleFileInfo>(); var cmd2 = client.RunCommand("cd " + session.SynoReportHome + ";ls -latR --time-style=full-iso synoreport"); string[] result2 = cmd2.Result.Split('\n'); if (Disconnect == true) { client.Disconnect(); } int row = 0; while (row < result2.Count() && result2[row].Length > 0) { string folder = "/" + result2[row].Substring(0, result2[row].Length - 1); row++; row++; while (result2[row].Length > 0) { if (result2[row].StartsWith("d") == false) { //System.Diagnostics.Debug.WriteLine(result2[row]); string permission = result2[row].Substring(0, 11); string parse = result2[row].Substring(11).TrimStart(); string grp = parse.Substring(0, parse.IndexOf(' ', 0)); parse = parse.Substring(parse.IndexOf(' ', 0)).TrimStart(); string uid1 = parse.Substring(0, parse.IndexOf(' ', 0)); parse = parse.Substring(parse.IndexOf(' ', 0)).TrimStart(); string uid2 = parse.Substring(0, parse.IndexOf(' ', 0)); parse = parse.Substring(parse.IndexOf(' ', 0)).TrimStart(); long filesize = long.Parse(parse.Substring(0, parse.IndexOf(' ', 0))); parse = parse.Substring(parse.IndexOf(' ', 0)).TrimStart(); string ft = parse.Substring(0, 35); string file = parse.Substring(35).TrimStart(); DateTime ts; DateTime.TryParse(ft, out ts); result.Add(new ConsoleFileInfo(folder, file, ts.ToUniversalTime())); } row++; } row++; } return(result); }
public override List <ConsoleFileInfo> GetDirectoryContentsRecursive(SshClient client, SynoReportViaSSH session, bool Disconnect = true) { List <ConsoleFileInfo> result = new List <ConsoleFileInfo>(); var cmd2 = client.RunCommand("cd " + session.SynoReportHome + ";ls -lARe synoreport/"); string[] result2 = cmd2.Result.Split('\n'); if (Disconnect == true) { client.Disconnect(); } int row = 0; int chop = 57 + 12; while (row < result2.Count()) { string folder = "/" + result2[row].Substring(0, result2[row].Length - 1); row++; while (result2[row].Length > 0) { if (result2[row].StartsWith("d") == false) { //string file = folder + "/" + result2[row].Substring(chop); string ft = result2[row].Substring(chop - 25 + 4, 24 - 4); DateTime ts = default(DateTime); DateTime.TryParseExact(ft, "MMM d HH:mm:ss yyyy", new CultureInfo("en-US"), (DateTimeStyles)((int)DateTimeStyles.AssumeLocal + DateTimeStyles.AllowInnerWhite), out ts); result.Add(new ConsoleFileInfo(folder, result2[row].Substring(chop), ts.ToUniversalTime())); } row++; } row++; } return(result); }