예제 #1
0
        public void Test_Scp_File_Upload_Download()
        {
            RemoveAllFiles();

            using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
            {
                scp.Connect();

                string uploadedFileName = Path.GetTempFileName();
                string downloadedFileName = Path.GetTempFileName();

                this.CreateTestFile(uploadedFileName, 1);

                scp.Upload(new FileInfo(uploadedFileName), Path.GetFileName(uploadedFileName));

                scp.Download(Path.GetFileName(uploadedFileName), new FileInfo(downloadedFileName));

                //  Calculate MD5 value
                var uploadedHash = CalculateMD5(uploadedFileName);
                var downloadedHash = CalculateMD5(downloadedFileName);

                File.Delete(uploadedFileName);
                File.Delete(downloadedFileName);

                scp.Disconnect();

                Assert.AreEqual(uploadedHash, downloadedHash);
            }
        }
예제 #2
0
        public void Test_Scp_File_20_Parallel_Upload_Download()
        {
            using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
            {
                scp.Connect();

                var uploadFilenames = new string[20];
                for (int i = 0; i < uploadFilenames.Length; i++)
                {
                    uploadFilenames[i] = Path.GetTempFileName();
                    this.CreateTestFile(uploadFilenames[i], 1);
                }

                Parallel.ForEach(uploadFilenames,
                    (filename) =>
                    {
                        scp.Upload(new FileInfo(filename), Path.GetFileName(filename));
                    });

                Parallel.ForEach(uploadFilenames,
                    (filename) =>
                    {
                        scp.Download(Path.GetFileName(filename), new FileInfo(string.Format("{0}.down", filename)));
                    });

                var result = from file in uploadFilenames
                             where
                                 CalculateMD5(file) == CalculateMD5(string.Format("{0}.down", file))
                             select file;

                scp.Disconnect();

                Assert.IsTrue(result.Count() == uploadFilenames.Length);
            }
        }
예제 #3
0
 public void DownloadTest2()
 {
     ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
     ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
     string filename = string.Empty; // TODO: Initialize to an appropriate value
     Stream destination = null; // TODO: Initialize to an appropriate value
     target.Download(filename, destination);
     Assert.Inconclusive("A method that does not return a value cannot be verified.");
 }
예제 #4
0
 public void DownloadTest()
 {
     ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
     ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
     string directoryName = string.Empty; // TODO: Initialize to an appropriate value
     DirectoryInfo directoryInfo = null; // TODO: Initialize to an appropriate value
     target.Download(directoryName, directoryInfo);
     Assert.Inconclusive("A method that does not return a value cannot be verified.");
 }
예제 #5
0
        public void Test_Scp_Directory_Upload_Download()
        {
            RemoveAllFiles();

            using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
            {
                scp.Connect();

                var uploadDirectory = Directory.CreateDirectory(string.Format("{0}\\{1}", Path.GetTempPath(), Path.GetRandomFileName()));
                for (int i = 0; i < 3; i++)
                {
                    var subfolder = Directory.CreateDirectory(string.Format(@"{0}\folder_{1}", uploadDirectory.FullName, i));
                    for (int j = 0; j < 5; j++)
                    {
                        this.CreateTestFile(string.Format(@"{0}\file_{1}", subfolder.FullName, j), 1);
                    }
                    this.CreateTestFile(string.Format(@"{0}\file_{1}", uploadDirectory.FullName, i), 1);
                }

                scp.Upload(uploadDirectory, "uploaded_dir");

                var downloadDirectory = Directory.CreateDirectory(string.Format("{0}\\{1}", Path.GetTempPath(), Path.GetRandomFileName()));

                scp.Download("uploaded_dir", downloadDirectory);

                var uploadedFiles = uploadDirectory.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
                var downloadFiles = downloadDirectory.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

                var result = from f1 in uploadedFiles
                             from f2 in downloadFiles
                             where
                             f1.FullName.Substring(uploadDirectory.FullName.Length) == f2.FullName.Substring(downloadDirectory.FullName.Length) &&
                             CalculateMD5(f1.FullName) == CalculateMD5(f2.FullName)
                             select f1;

                var counter = result.Count();

                scp.Disconnect();

                Assert.IsTrue(counter == uploadedFiles.Length && uploadedFiles.Length == downloadFiles.Length);
            }
        }
예제 #6
0
    public static string get_mac(string host, string name, string pass)
    {
        ScpClient scp = new ScpClient(host, 22, name, pass);

        scp.Connect();
        FileInfo file = new FileInfo("C:\\fw\\Mac.txt");

        scp.Download("/etc/board.info", file);
        scp.Disconnect();
        FileStream file_s = file.OpenRead();
        string     data;
        Regex      re = new Regex(@"hwaddr=(\w+)\n");

        using (StreamReader sr = new StreamReader(file_s))
        {
            data = sr.ReadToEnd();
        }
        var match = re.Match(data);

        return(match.Groups[1].Value);
    }
예제 #7
0
        public static async Task CopyFileToInstance(AmazonEC2Client ec2Client, string instanceId, string InstanceFqdn, string keyFile, string username, string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                password = await GetPassword(ec2Client, instanceId, keyFile);
            }
            if (string.IsNullOrWhiteSpace(InstanceFqdn))
            {
                InstanceFqdn = await GetFqdn(ec2Client, instanceId);
            }
            using (var client = new ScpClient(InstanceFqdn, 22, username, password))
            {
                client.RemotePathTransformation = RemotePathTransformation.ShellQuote;
                client.Connect();

                using (var ms = new MemoryStream())
                {
                    client.Download("/home/sshnet/file 123", ms);
                }
            }
        }
예제 #8
0
 public void SPC_DownloadFolder(string strfile, string path)
 {
     try
     {
         string    path2     = path + Path.GetFileName(strfile);
         ScpClient scpClient = new ScpClient("127.0.0.1", "root", "alpine");
         try
         {
             ((Thread)(object)scpClient).Start();
             scpClient.Download(strfile, new DirectoryInfo(path2));
             ((Thread)(object)scpClient).Start();
         }
         finally
         {
             ((Thread)(object)scpClient)?.Start();
         }
     }
     catch (Exception ex)
     {
         ERROR = ((TextReader)(object)ex).ReadToEnd();
     }
 }
예제 #9
0
        private Stream doAction()
        {
            m_sResponse = new MemoryStream();

            if (m_method == KeePassLib.Serialization.IOConnection.WrmDeleteFile)
            {
                throw new ArgumentException("scp not support DELETE method");
            }
            else if (m_method == KeePassLib.Serialization.IOConnection.WrmMoveFile)
            {
                throw new ArgumentException("scp not support MOVE method");
            }
            else if (m_sReqStream == null && m_method != "POST")
            {
                //  m_lSize = m_scpClient.GetAttributes(m_uriResponse.LocalPath).Size;
                m_scpClient.Download(m_uriResponse.LocalPath, m_sResponse);
                // Debug.Assert(m_sResponse.Length != m_lSize);
            }
            else if (m_method == "POST")
            {
                if (m_sReqStream == null)
                {
                    throw new ArgumentNullException("m_sReqStream");
                }
                m_lSize = 0;

                m_scpClient.Upload(m_sReqStream, m_uriResponse.LocalPath);
            }
            else
            {
                throw new Exception("mode not support");
            }

            string strTempFile = Path.GetTempFileName();

            File.WriteAllBytes(strTempFile, ((MemoryStream)m_sResponse).ToArray());

            return(m_sResponse.Length > 0 ? File.Open(strTempFile, FileMode.Open) : m_sResponse);
        }
예제 #10
0
        public override bool Run()
        {
            if (Method == EDumpMethod.scp)
            {
                WriteInfo("Connecting ...");

                using (ScpClient SSH = new ScpClient(SSHHost.Address.ToString(), SSHHost.Port, User, Password))
                {
                    SSH.Connect();
                    WriteInfo("Connected successful");

                    WriteInfo("Executing", "SCP download", ConsoleColor.Cyan);
                    SSH.Download(RemotePath, DumpPath);

                    DumpPath.Refresh();
                    WriteInfo("Download successful", StringHelper.Convert2KbWithBytes(DumpPath.Length), ConsoleColor.Cyan);
                    return(true);
                }
            }

            return(base.Run());
        }
예제 #11
0
        /// <summary>
        /// Download the robot output log from the active VM
        /// </summary>
        /// <returns>True if successful</returns>
        public static Task <bool> FetchLogFile()
        {
            return(Task.Run(() =>
            {
                if (ClientManager.Instance.RunCommand(CHECK_LOG_EXISTS_COMMAND).ExitStatus != 0)
                {
                    UserMessageManager.Dispatch("No user program log file found to download", EmulationWarnings.WARNING_DURATION);
                    return false;
                }

                string folder = SFB.StandaloneFileBrowser.OpenFolderPanel("Log file destination", "C:\\", false);
                if (folder == null)
                {
                    return true;
                }
                else
                {
                    try
                    {
                        using (ScpClient client = new ScpClient(DEFAULT_HOST, programType == UserProgram.Type.JAVA ? DEFAULT_SSH_PORT_JAVA : DEFAULT_SSH_PORT_CPP, USER, PASSWORD))
                        {
                            client.Connect();
                            Stream localLogFile = File.Create(folder + "/log.log");
                            client.Download(REMOTE_LOG_NAME, localLogFile);
                            localLogFile.Close();
                            client.Disconnect();
                        }
                    }
                    catch (Exception)
                    {
                        UserMessageManager.Dispatch("Failed to download user program log file", EmulationWarnings.WARNING_DURATION);
                        return false;
                    }
                }
                return true;
            }));
        }
예제 #12
0
        public void CollectHW()
        {
            var client = new ScpClient("ieng6.ucsd.edu", UserName, PWD);

            client.Connect();
            int i = 0;

            foreach (var student in StudentList)
            {
                string homePath = (Environment.OSVersion.Platform == PlatformID.Unix ||
                                   Environment.OSVersion.Platform == PlatformID.MacOSX)
                    ? Environment.GetEnvironmentVariable("HOME")
                    : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
                Directory.CreateDirectory(homePath + "/FA178Apsa" + Psa);
                Console.Write("Downloading from " + student.Name + "...");
                var cmd = Client.RunCommand("cp ../" + student.Name + "/..psa" + Psa + ".tar.gz .");
                if (cmd.Error.Length == 0)
                {
                    Client.RunCommand("mkdir " + student.Name);
                    Client.RunCommand("tar xf ..psa" + Psa + ".tar.gz -C " + student.Name);
                    //Client.RunCommand("rm " + student.Name + "/*.png");
                    //Client.RunCommand("rm " + student.Name + "/*.jpg");
                    //Client.RunCommand("rm " + student.Name + "/*.jpeg");
                    Directory.CreateDirectory(homePath + "/FA178Apsa" + Psa + "/" + student.Name);
                    var dir = new DirectoryInfo(homePath + "/FA178Apsa" + Psa + "/" + student.Name);
                    client.Download(student.Name, dir);
                    Client.RunCommand("rm -r " + student.Name);
                    Client.RunCommand("rm *.tar.gz");
                    i++;
                    Console.WriteLine("Succeed      Total HW uploaded is " + i);
                }
                else
                {
                    Console.WriteLine("Failed      Total HW uploaded is " + i);
                }
            }
        }
예제 #13
0
        public void Test_Scp_10MB_Stream_Upload_Download()
        {
            RemoveAllFiles();

            using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
            {
                scp.Connect();

                string uploadedFileName   = Path.GetTempFileName();
                string downloadedFileName = Path.GetTempFileName();

                this.CreateTestFile(uploadedFileName, 10);

                //  Calculate has value
                using (var stream = File.OpenRead(uploadedFileName))
                {
                    scp.Upload(stream, Path.GetFileName(uploadedFileName));
                }

                using (var stream = File.OpenWrite(downloadedFileName))
                {
                    scp.Download(Path.GetFileName(uploadedFileName), stream);
                }

                //  Calculate MD5 value
                var uploadedHash   = CalculateMD5(uploadedFileName);
                var downloadedHash = CalculateMD5(downloadedFileName);

                File.Delete(uploadedFileName);
                File.Delete(downloadedFileName);

                scp.Disconnect();

                Assert.AreEqual(uploadedHash, downloadedHash);
            }
        }
예제 #14
0
        public void Test_Scp_File_20_Parallel_Upload_Download()
        {
            using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
            {
                scp.Connect();

                var uploadFilenames = new string[20];
                for (int i = 0; i < uploadFilenames.Length; i++)
                {
                    uploadFilenames[i] = Path.GetTempFileName();
                    this.CreateTestFile(uploadFilenames[i], 1);
                }

                Parallel.ForEach(uploadFilenames,
                                 (filename) =>
                {
                    scp.Upload(new FileInfo(filename), Path.GetFileName(filename));
                });

                Parallel.ForEach(uploadFilenames,
                                 (filename) =>
                {
                    scp.Download(Path.GetFileName(filename), new FileInfo(string.Format("{0}.down", filename)));
                });

                var result = from file in uploadFilenames
                             where
                             CalculateMD5(file) == CalculateMD5(string.Format("{0}.down", file))
                             select file;


                scp.Disconnect();

                Assert.IsTrue(result.Count() == uploadFilenames.Length);
            }
        }
예제 #15
0
        public FileInfo GetFileAsLocal(string remote_path, string local_path)
        {
            CallerInformation here = this.Here();
            Stopwatch         sw   = new Stopwatch();

            sw.Start();
            ScpClient c = this.CreateScpClient();

            c.BufferSize = 16 * 16384;
            if (c == null)
            {
                return(null);
            }
            try
            {
                FileInfo f = new FileInfo(local_path);
                c.Download(remote_path, f);
                sw.Stop();
                Debug(here, "Downloaded remote file {0} to {1} via SCP in {2} ms.", remote_path, f.FullName, sw.ElapsedMilliseconds);
                return(f);
            }
            catch (Exception e)
            {
                Error("Exception thrown attempting to download file {0} from {1} to {2} via SCP.", remote_path, this.HostName, remote_path);
                Error(here, e);
                return(null);
            }
            finally
            {
                this.DestroyScpClient(c);
                if (sw.IsRunning)
                {
                    sw.Stop();
                }
            }
        }
예제 #16
0
        public static void Main()
        {
            int sampleFrequency = 5;
            int counter         = 0;
            int logSize;
            int usageLogSize;
            int powerLogSize;
            int iterate     = 1;
            int beforePause = 10000;
            int afterPause  = beforePause;

            string line;

            string host              = "10.0.0.2";
            string userName          = "******";
            string password          = "******";
            string serialPortAddress = "COM3";

            string[] benchmarks = new string[] { "./NVIDIA_CUDA-6.5_Samples/bin/armv7l/linux/release/gnueabihf/scan" /*"cd darknet-2/ && ./image_yolov3.sh"*//*, "./shooting/build/shooting.x"*/ };

            List <double> powerLog;
            StreamReader  boardLog;

            // creating local log file (for power consumption)
            powerLogFile = new System.IO.StreamWriter("powerdata.log");

            // creating serial connection
            SerialPort serialPort = new SerialPort(serialPortAddress);

            serialPort.BaudRate      = 115200;
            serialPort.Parity        = Parity.None;
            serialPort.StopBits      = StopBits.One;
            serialPort.DataBits      = 8;
            serialPort.Handshake     = Handshake.None;
            serialCounter            = 0;
            serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            Console.WriteLine("connecting to serial port " + serialPortAddress);

            serialPort.Open();

            Console.WriteLine("connected (sync)");

            previousSampleTime = DateTime.Now;

            Console.WriteLine("connecting to board on " + host + " under user " + userName);

            //
            // board connection
            //

            Task.Factory.StartNew(() => {
                using (var client = new SshClient(host, userName, password)) {
                    // saving logs on tk1
                    client.Connect();

                    Console.WriteLine("connected (async)");

                    Console.WriteLine("started to store log data on board  at {0:mm.ss.ffffff}", DateTime.Now);

                    client.RunCommand(
                        "echo " + password + " | sudo -S ./tegrastats " +
                        ((int)(1000 * (1.0 / sampleFrequency))).ToString() +
                        " --logfile usagedata.log"
                        );

                    client.Disconnect();
                }
            });

            //
            // multimeter connection
            //

            Task.Factory.StartNew(() => {
                while (true)
                {
                    if (serialPort.IsOpen)
                    {
                        serialPort.WriteLine("VAL1?");
                    }
                    System.Threading.Thread.Sleep((int)(1000.0 / sampleFrequency));
                }
            });

            if (beforePause > 0)
            {
                System.Threading.Thread.Sleep(afterPause);
            }

            do
            {
                iterate--;

                counter = 0;
                Task[] tasks = new Task[benchmarks.Length];

                foreach (string benchmark in benchmarks)
                {
                    //
                    // running benchmarks
                    //

                    tasks[counter] = Task.Factory.StartNew(() => {
                        Console.WriteLine("running benchmark " + benchmark);

                        using (var client = new SshClient(host, userName, password)) {
                            // calling benchmark
                            client.Connect();
                            var output = client.RunCommand(benchmark);

                            // awaiting termination
                            Console.BackgroundColor = ConsoleColor.DarkGreen;
                            Console.Write(output.Result);
                            Console.BackgroundColor = ConsoleColor.Black;

                            Console.WriteLine("benchmark " + benchmark + " done");

                            client.Disconnect();
                        }
                    });

                    counter++;
                }

                Task.WaitAll(tasks);
            } while (iterate != 0);

            if (afterPause > 0)
            {
                // after pause for additional logging
                Console.WriteLine("logging another " + afterPause + " ms");

                System.Threading.Thread.Sleep(afterPause);
            }

            //
            // terminating / deleting
            //

            using (var client = new SshClient(host, userName, password)) {
                // stopping logs on tk1
                client.Connect();

                var output = client.RunCommand("echo " + password + " | sudo -S ./tegrastats --stop");

                Console.BackgroundColor = ConsoleColor.DarkGreen;
                Console.Write(output.Result);
                Console.BackgroundColor = ConsoleColor.Black;

                Console.WriteLine("log on board terminated at {0:mm.ss.ffffff}", DateTime.Now);

                client.Disconnect();
            }

            serialPort.Close();
            powerLogFile.Close();

            Console.WriteLine("log from multimiter terminated at {0:mm.ss.ffffff}", DateTime.Now);

            // downloading data from board
            using (ScpClient client = new ScpClient(host, userName, password)) {
                Console.WriteLine("retriving data from board");

                client.Connect();

                using (Stream localFile = File.Create("usagedata.log")) {
                    client.Download("usagedata.log", localFile);
                }

                Console.WriteLine("retrived");
            }

            using (var client = new SshClient(host, userName, password)) {
                // deleting log on board
                client.Connect();
                var output1 = client.RunCommand("echo " + password + " | sudo chmod 777 usagedata.log");
                var output2 = client.RunCommand("rm usagedata.log");

                Console.BackgroundColor = ConsoleColor.DarkGreen;
                Console.Write(output1.Result);
                Console.Write(output2.Result);
                Console.BackgroundColor = ConsoleColor.Black;

                Console.WriteLine("log on board deleted");

                client.Disconnect();
            }

            //
            // processing logs
            //

            Console.WriteLine("processing data");

            usageLogSize = File.ReadLines(@"usagedata.log").Count();
            powerLogSize = File.ReadLines(@"powerdata.log").Count();

            logSize =
                usageLogSize > powerLogSize ? powerLogSize : usageLogSize;

            // ??
            if (Math.Abs(File.ReadLines(@"usagedata.log").Count() -
                         File.ReadLines(@"powerdata.log").Count()) > sampleFrequency * 8)
            {
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("data from board and multimeter are unconsistent");
                Console.BackgroundColor = ConsoleColor.Black;
            }

            counter  = 0;
            powerLog = new List <double>();
            boardLog = new StreamReader(@"powerdata.log");

            while ((line = boardLog.ReadLine()) != null)
            {
                counter++;

                // ??
                if (powerLogSize - usageLogSize - (counter) > 0)
                {
                    continue;
                }

                double power = double.Parse(line, CultureInfo.InvariantCulture);
                powerLog.Add(power);
            }

            counter  = 0;
            boardLog = new StreamReader(@"usagedata.log");

            // creating local log file (for power consumption in A)
            logFile = new System.IO.StreamWriter("data.log");

            while ((line = boardLog.ReadLine()) != null && counter < powerLogSize)
            {
                string[] stringLine = line.Split(' ');

                string[] cpuUsage = stringLine[5].Split('@')[0].TrimStart('[').TrimEnd(']').Split(',');

                double cpuUsageCore1 = double.Parse(cpuUsage[0] == "off" ? "0" : cpuUsage[0].TrimEnd('%'));
                double cpuUsageCore2 = double.Parse(cpuUsage[1] == "off" ? "0" : cpuUsage[1].TrimEnd('%'));
                double cpuUsageCore3 = double.Parse(cpuUsage[2] == "off" ? "0" : cpuUsage[2].TrimEnd('%'));
                double cpuUsageCore4 = double.Parse(cpuUsage[3] == "off" ? "0" : cpuUsage[3].TrimEnd('%'));

                double gpuUsage = double.Parse(stringLine[13].Split('%')[0]);

                // time[relative] power[W*10] core1[%] core2 core3 core4 gpu[%]
                logFile.WriteLine(String.Format(CultureInfo.InvariantCulture,
                                                "{0} {1:E} {2:E} {3:E} {4:E} {5:E} {6:E}",
                                                counter,
                                                powerLog.ElementAt(counter) * 12 * 10,
                                                cpuUsageCore1, cpuUsageCore2, cpuUsageCore3, cpuUsageCore4,
                                                gpuUsage
                                                ));

                counter++;
            }

            logFile.Close();

            Console.WriteLine("data processed see data.log");

            Console.WriteLine("press any key to continue...");
            Console.WriteLine();
            Console.ReadKey();
        }
예제 #17
0
        public String Commit()
        {
            if (_config.Settings.Enabled)
            {
                ConnectionInfo connInfo = new ConnectionInfo(
                    _config.Settings.Server,
                    _config.Settings.Username,
                    AuthMethod);

                try
                {
                    using (var client = new ScpClient(connInfo))
                    {
                        client.Connect();
                        if (client.IsConnected)
                        {
                            //TODO: check if the file is changed before uploading a new one if it is new back it up
                            foreach (var newFile in _output)
                            {
                                var location = $"{ _config.Settings.TargetFolderPath }/{ newFile.Key }".Replace("//", "/");

                                var originalFileMS = new MemoryStream();
                                try
                                {
                                    client.Download(location, originalFileMS);
                                } catch (Exception e)
                                {
                                    Console.WriteLine($"{location}: File Check Failed ({e.Message})");
                                }

                                MemoryStream  newDataStream = new MemoryStream();
                                ASCIIEncoding encoding      = new ASCIIEncoding();
                                newDataStream.Write(encoding.GetBytes(newFile.Value.ToString()), 0, newFile.Value.Length);

                                var NewVsExisting = Tools.CompareMemoryStreams(originalFileMS, newDataStream);
                                if (NewVsExisting)
                                {
                                    Console.WriteLine($"Skipping - Not Changed: {location}");
                                }
                                else
                                {
                                    newDataStream.Position = 0;
                                    client.Upload(newDataStream, location);
                                    Console.WriteLine($"Uploaded: {location}");
                                }
                            }
                            client.Disconnect();
                        }
                    }
                }
                catch (Exception e)
                {
                    return($"Prometheus: Error with SSH Upload ({e.Message}");
                }

                // We made it past the try block so upload was successful;
                return($"Prometheus: Commit Complete");
            }
            else
            {
                return("Plugin is disabled");
            }
        }
예제 #18
0
    protected void btnDownload_Click(object sender, EventArgs e)
    {
        try
        {
            if ((lblIDData.Text == "") || (lblIPAddressData.Text == "") || (lblServerNameData.Text == ""))
            {
                lblMessageData01.Text = "Please select a server.";
                lblMessageData02.Text = "";
            }
            else
            {
                String    strDesktop   = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                DataTable objDataTable = new DataTable();

                objDataTable = clsServersDataManager.SelectServer(dgridShowData.SelectedRow.Cells[1].Text);

                stringArr = objDataTable.Rows[0].ItemArray.Select(arrServer => arrServer.ToString()).ToArray();

                String strFilePath = stringArr[4].Remove(stringArr[4].LastIndexOf("/"), stringArr[4].Length - stringArr[4].LastIndexOf("/"));
                String strFileName = stringArr[4].Remove(0, stringArr[4].LastIndexOf("/") + 1);

                using (var ssh = new SshClient(stringArr[2], stringArr[6], stringArr[7]))
                {
                    ssh.Connect();

                    String strCommandChain = "cd " + strFilePath + "; tar -czvf " + strFileName + ".tgz " + strFileName;
                    ssh.CreateCommand(strCommandChain).Execute();

                    ssh.Disconnect();
                }

                using (var objScpClient = new ScpClient(stringArr[2], stringArr[6], stringArr[7]))
                    using (var ssh = new SshClient(stringArr[2], stringArr[6], stringArr[7]))
                    {
                        objScpClient.Connect();

                        string   objTempFile = Path.GetTempFileName();
                        FileInfo objFileInfo = new FileInfo(objTempFile);


                        objScpClient.Download(stringArr[4] + ".tgz", objFileInfo);

                        Guid objGUID = Guid.NewGuid();

                        String strDestinationPath = strTempPath + strFileName + objGUID.ToString() + ".tgz";

                        objFileInfo.MoveTo(strDestinationPath);

                        objScpClient.Disconnect();

                        Response.ContentType = "application/x-compressed";
                        Response.AppendHeader("Content-Disposition", "attachment; filename=" + strFileName + ".tgz");
                        Response.TransmitFile(Server.MapPath("~/temp/" + strFileName + objGUID.ToString() + ".tgz"));
                        Response.Flush();
                        Response.SuppressContent = true;
                        ApplicationInstance.CompleteRequest();

                        clsLog objLog = new clsLog();

                        objLog.strIP          = Request.UserHostAddress;
                        objLog.strLog_Date    = DateTime.Now.ToString();
                        objLog.strTarget_Name = stringArr[1];
                        objLog.strAction      = "D";

                        clsLogManager.InsertLog(objLog);
                    }

                lblMessageData01.Text = "";
                lblMessageData02.Text = "";
            }
        }
        catch (Exception ex)
        {
            Guid   objGUID      = Guid.NewGuid();
            String strException = ex.ToString();

            File.WriteAllText(strLogPath + DateTime.Today.ToString("yyyyMMdd") + "-" + objGUID.ToString() + ".log", strException);

            lblMessageData01.Text = "خطایی در سیستم رخ داده است. لطفا کد خطای زیر را به همراه آی دی سرور به واحد نصب و راه اندازی ارسال نمایید.\r\n";
            lblMessageData02.Text = DateTime.Today.ToString("yyyyMMdd") + "-" + objGUID.ToString();
        }
    }
        public bool DoWork(Util.UpdateStatusDelegate SetStatus, Util.ShouldCancelDelegate ShouldCancel)
        {
            SetStatus("Flushing local DNS cache...", 0);

            try
            {
                string IPConfigPath = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\System32\ipconfig.exe");
                var    IPConfig     = Process.Start(IPConfigPath, "/flushdns");
                if (IPConfig == null)
                {
                    throw new Exception("Unable to flush local DNS cache. Could not run ipconfig.");
                }
                IPConfig.WaitForExit();
            }
            catch (Exception)
            {
                throw new Exception("Unable to flush local DNS cache. Could not run ipconfig.");
            }

            SetStatus("Connecting to SSH and SCP...", 5);

            using (VyattaShell Shell = new VyattaShell(Data.Address, Data.Username, Data.Password))
            {
                using (ScpClient Client = new ScpClient(Data.Address, Data.Username, Data.Password))
                {
                    Client.Connect();

                    SetStatus("Grabbing dnsmasq config...", 10);

                    string DNSMasqConfigPath    = Path.ChangeExtension(Path.GetTempFileName(), Guid.NewGuid().ToString());
                    string NewDNSMasqConfigPath = Path.ChangeExtension(Path.GetTempFileName(), Guid.NewGuid().ToString());

                    using (Stream tempFile = new FileStream(DNSMasqConfigPath, FileMode.CreateNew))
                    {
                        Client.Download("/etc/dnsmasq.conf", tempFile);
                    }

                    SetStatus("Backing up dnsmasq config...", 15);

                    Shell.RunCommand("sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak");

                    SetStatus("Processing config file...", 20);

                    string[] ConfigFile = File.ReadAllLines(DNSMasqConfigPath);

                    for (int LineIndex = 0; LineIndex < ConfigFile.Length; LineIndex++)
                    {
                        //Comment out log related lines
                        if (ConfigFile[LineIndex].StartsWith("log-") || ConfigFile[LineIndex].StartsWith("cache-size="))
                        {
                            ConfigFile[LineIndex] = "#" + ConfigFile[LineIndex];
                        }
                    }

                    string[] ConfigFileFooter = new string[]
                    {
                        "log-queries",
                        //"log-async=25", // This just confuses the log and makes it more difficult to parse.
                        "log-facility=/tmp/dnslog.txt",
                        "cache-size=0"
                    };

                    string[] ConfigFileFinal = new string[ConfigFile.Length + ConfigFileFooter.Length];
                    ConfigFile.CopyTo(ConfigFileFinal, 0);
                    ConfigFileFooter.CopyTo(ConfigFileFinal, ConfigFile.Length);

                    using (TextWriter FileOut = new StreamWriter(NewDNSMasqConfigPath))
                    {
                        FileOut.NewLine = "\n";

                        foreach (var Line in ConfigFileFinal)
                        {
                            FileOut.WriteLine(Line);
                        }
                    }

                    SetStatus("Uploading new temporary config file...", 30);

                    using (Stream uploadFile = new FileStream(NewDNSMasqConfigPath, FileMode.Open))
                    {
                        Client.Upload(uploadFile, "/tmp/NewConfigFile");
                    }

                    SetStatus("Preparing config file...", 40);

                    Shell.RunCommand("sudo cp /tmp/NewConfigFile /etc/dnsmasq.conf");
                    Shell.RunCommand("rm /tmp/NewConfigFile");
                    Shell.RunCommand("sudo chmod 0644 /etc/dnsmasq.conf");

                    SetStatus("Restarting dnsmasq...", 60);

                    //Do stop/start separately so we can delete the file in case
                    //something went wrong previously.
                    Shell.RunCommand("sudo /etc/init.d/dnsmasq stop");
                    Shell.RunCommand("sudo rm /tmp/dnslog.txt");
                    Shell.RunCommand("sudo /etc/init.d/dnsmasq start");

                    SetStatus("Collecting data, press Cancel to stop...", 100);

                    while (!ShouldCancel())
                    {
                        System.Threading.Thread.Sleep(100);
                    }

                    SetStatus("Restarting dnsmasq as normal...", 0);

                    //Restore backup
                    Shell.RunCommand("sudo cp /etc/dnsmasq.conf.bak /etc/dnsmasq.conf");

                    //Restart dnsmasq with the old config again
                    Shell.RunCommand("sudo /etc/init.d/dnsmasq restart");

                    SetStatus("Downloading collected data...", 50);

                    LogPath = Path.ChangeExtension(Path.GetTempFileName(), Guid.NewGuid().ToString());

                    Shell.RunCommand("sudo chmod 777 /tmp/dnslog.txt");

                    using (Stream tempFile = new FileStream(LogPath, FileMode.CreateNew))
                    {
                        Client.Download("/tmp/dnslog.txt", tempFile);
                    }

                    //Delete the log file now we've got it.
                    Shell.RunCommand("sudo rm /tmp/dnslog.txt");

                    SetStatus("Disconnecting.", 95);

                    Client.Disconnect();
                }
            }

            SetStatus("Completed.", 100);

            return(true);
        }
예제 #20
0
        public void Test_Scp_10MB_Stream_Upload_Download()
        {
            RemoveAllFiles();

            using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
            {
                scp.Connect();

                string uploadedFileName = Path.GetTempFileName();
                string downloadedFileName = Path.GetTempFileName();

                this.CreateTestFile(uploadedFileName, 10);

                //  Calculate has value
                using (var stream = File.OpenRead(uploadedFileName))
                {
                    scp.Upload(stream, Path.GetFileName(uploadedFileName));
                }

                using (var stream = File.OpenWrite(downloadedFileName))
                {
                    scp.Download(Path.GetFileName(uploadedFileName), stream);
                }

                //  Calculate MD5 value
                var uploadedHash = CalculateMD5(uploadedFileName);
                var downloadedHash = CalculateMD5(downloadedFileName);

                File.Delete(uploadedFileName);
                File.Delete(downloadedFileName);

                scp.Disconnect();

                Assert.AreEqual(uploadedHash, downloadedHash);
            }
        }
예제 #21
0
        private static async Task DownloadOutputFilesAsync(long jobId, string sessionCode)
        {
            //GetFileTransferMethod
            var client  = new RestClient(baseUrl);
            var request = new RestRequest("FileTransfer/GetFileTransferMethod", Method.Post)
            {
                RequestFormat = DataFormat.Json
            }.AddJsonBody(
                new GetFileTransferMethodModel
            {
                SubmittedJobInfoId = jobId,
                SessionCode        = sessionCode
            });
            var response = await client.ExecuteAsync(request);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception(response.Content.ToString());
            }

            FileTransferMethodExt ft = JsonConvert.DeserializeObject <FileTransferMethodExt>(response.Content.ToString());

            //ListChangedFilesForJob
            request = new RestRequest("FileTransfer/ListChangedFilesForJob", Method.Post)
            {
                RequestFormat = DataFormat.Json
            }.AddJsonBody(
                new ListChangedFilesForJobModel
            {
                SubmittedJobInfoId = jobId,
                SessionCode        = sessionCode
            });
            response = await client.ExecuteAsync(request);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception(response.Content.ToString());
            }

            var outputFiles = JsonConvert.DeserializeObject <FileInformationExt[]>(response.Content.ToString());

            //DownloadOutputFiles
            using (MemoryStream pKeyStream = new MemoryStream(Encoding.UTF8.GetBytes(ft.Credentials.PrivateKey)))
            {
                using (ScpClient scpClient = new ScpClient(ft.ServerHostname, ft.Credentials.Username, new PrivateKeyFile(pKeyStream)))
                {
                    scpClient.Connect();

                    string        localOutputPath = $"C:\\Heappe\\projects\\develop\\tests\\output\\{jobId}\\";
                    DirectoryInfo di = new DirectoryInfo(localOutputPath);
                    if (!di.Exists)
                    {
                        di.Create();
                    }

                    foreach (var outputFile in outputFiles)
                    {
                        FileInfo fi = new FileInfo(localOutputPath + outputFile.FileName);
                        if (!fi.Directory.Exists)
                        {
                            fi.Directory.Create();
                        }

                        using (Stream fileStream = System.IO.File.OpenWrite(localOutputPath + outputFile.FileName))
                        {
                            sb.AppendLine(String.Format("Downloading file {0}", outputFile.FileName));
                            scpClient.Download(ft.SharedBasepath + "/" + outputFile.FileName, fileStream);
                            sb.AppendLine(String.Format("File downloaded {0}", outputFile.FileName));
                        }
                    }
                }
            }

            //EndFileTransfer
            request = new RestRequest("FileTransfer/EndFileTransfer", Method.Post)
            {
                RequestFormat = DataFormat.Json
            }.AddJsonBody(
                new EndFileTransferModel
            {
                SubmittedJobInfoId = jobId,
                UsedTransferMethod = ft,
                SessionCode        = sessionCode
            });
            response = await client.ExecuteAsync(request);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception(response.Content.ToString());
            }
        }
예제 #22
0
        private bool execute_script(string ipaddress, string private_key, string instance_type, string engine, string eval)
        {
            bool success = false;

            string[]  tags = new string[] { "nosse", "sse2", "sse41", "sse42", "avx2", "sse", "bmi2", "release", "all" };
            IPAddress addr = IPAddress.Parse(ipaddress);

            int  ssh_connection_timeout_seconds = setting_.getValueInt("common", "ssh_connection_timeout_seconds");
            long timeout = DateTime.Now.Ticks / TimeSpan.TicksPerSecond + ssh_connection_timeout_seconds;

            for (int ii = 0; ii < 60 * 10; ii++)
            {
                if (timeout < (DateTime.Now.Ticks / TimeSpan.TicksPerSecond))
                {
                    break;
                }
                if ((ii % 20) == 1)
                {
                    write_log(ipaddress + "への接続を試行しています。");
                }
                Socket       socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IAsyncResult result = socket.BeginConnect(addr, 22, null, null);
                success = result.AsyncWaitHandle.WaitOne(500, true);
                socket.Close();
                Helper.sleep(100);
                if (success)
                {
                    break;
                }
            }
            if (!success)
            {
                write_log(" ERROR: connect timeout.");
                return(false);
            }

            write_log(ipaddress + " でのスクリプト実行準備をしています。");
            Helper.sleep(8 * 1000);

            PrivateKeyFile privateKeyFile = new PrivateKeyFile(private_key);
            string         user           = setting_.getValueString("common", "ssh_user");
            ConnectionInfo connect_info   = new ConnectionInfo(ipaddress, 22, user,
                                                               new AuthenticationMethod[] { new PrivateKeyAuthenticationMethod(user, new PrivateKeyFile[] { privateKeyFile }) });

            int nn = -1;

            switch (shutdown_combo_box.SelectedIndex)
            {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                nn = checked ((60 * shutdown_combo_box.SelectedIndex) - 5);
                break;

            case 7:
                nn = (60 * 12) - 5;
                break;

            case 8:
                nn = (60 * 24) - 5;
                break;
            }
            end_time_str_ = "";
            if (nn != -1)
            {
                end_time_str_ = System.Xml.XmlConvert.ToString(DateTime.Now.AddMinutes(nn), System.Xml.XmlDateTimeSerializationMode.Local);
                Helper.sleep(3 * 1000);
                using (var client = new SshClient(connect_info))
                {
                    client.Connect();
                    using (ShellStream stream = client.CreateShellStream("usi_engine", 80, 24, 800, 600, 1024))
                    {
                        var reader = new StreamReader(stream);
                        var writer = new StreamWriter(stream);
                        writer.AutoFlush = true;
                        read_text_data(reader, stream, 3);
                        Helper.sleep(1000);
                        write_stream("nohup sh -c \"sleep " + nn + "m; sudo shutdown -h now\" > /dev/null 2>&1 &", writer, stream);
                        Helper.sleep(1000);
                    }
                }
            }

            Helper.sleep(3000);
            using (var scp = new ScpClient(connect_info))
            {
                string uploadfn = resouce_dir_ + "setup.tar.gz";
                scp.Connect();
                scp.Upload(new FileInfo(uploadfn), "/tmp");

                uploadfn = resouce_dir_ + engine + ".deb";
                if (File.Exists(uploadfn))
                {
                    scp.Upload(new FileInfo(uploadfn), "/tmp");
                }
                uploadfn = resouce_dir_ + engine + "-{0}.deb";
                foreach (string tag in tags)
                {
                    string tmp = string.Format(uploadfn, tag);
                    if (File.Exists(tmp))
                    {
                        scp.Upload(new FileInfo(tmp), "/tmp");
                    }
                }
                scp.Disconnect();
            }

            write_log(ipaddress + " にてスクリプトの実行を行います。");

            Helper.sleep(3000);
            using (var client = new SshClient(connect_info))
            {
                client.Connect();
                using (ShellStream stream = client.CreateShellStream("usi_engine", 80, 24, 800, 600, 1024))
                {
                    var reader = new StreamReader(stream);
                    var writer = new StreamWriter(stream);
                    writer.AutoFlush = true;

                    read_text_data(reader, stream, 3);
                    Helper.sleep(1000);

                    write_stream("mkdir -p /tmp/.setup; cd /tmp/.setup; sudo tar zxvf /tmp/setup.tar.gz; " +
                                 "sudo sh ./bootstrap.sh '" + instance_type + "' '" + engine + "' '" + eval + "';" +
                                 "sudo rm -Rf /tmp/.setup /tmp/setup.tar.gz;" +
                                 "echo '__EXIT__';", writer, stream);
                    if (read_text_data(reader, stream, 300) == false)
                    {
                        write_log(ipaddress + " からの応答異常を検知しました。");
                        return(false);
                    }
                }
            }

            using (var scp = new ScpClient(connect_info))
            {
                string downloadfn = "";
                scp.Connect();
                downloadfn = resouce_dir_ + engine + ".deb";
                if (!File.Exists(downloadfn))
                {
                    try
                    {
                        scp.Download("/tmp/" + engine + ".deb", new FileInfo(downloadfn));
                        write_log(ipaddress + " から " + engine + ".deb" + " をダウンロードしました。");
                    }
                    catch
                    {
                        // none.
                    }
                }

                downloadfn = engine + "-{0}.deb";
                foreach (string tag in tags)
                {
                    string tmp = string.Format(downloadfn, tag);
                    if (!File.Exists(resouce_dir_ + tmp))
                    {
                        try
                        {
                            scp.Download("/tmp/" + tmp, new FileInfo(resouce_dir_ + tmp));
                            write_log(ipaddress + " から " + tmp + " をダウンロードしました。");
                        }
                        catch
                        {
                            // none.
                        }
                    }
                }
                scp.Disconnect();
            }

            return(true);
        }
예제 #23
0
        public static void MigrateSaves(int sourceSize, int destinationSize, string lunarPath = null)
        {
            using (ScpClient scp = new ScpClient("169.254.215.100", "root", "5A7213"))
            {
                scp.Connect();

                MemoryStream settingsDataStream = new MemoryStream();
                scp.Download("/usr/game/save/data_008_0000.bin", settingsDataStream);

                MemoryStream settingsMetaStream = new MemoryStream();
                scp.Download("/usr/game/save/meta_008_0000.bin", settingsMetaStream);

                byte[] settingsData = new byte[settingsDataStream.Length];
                settingsDataStream.Position = 0;
                settingsDataStream.Read(settingsData, 0, settingsData.Length);
                SystemData originalSave = SystemData.FromByteArray(settingsData, sourceSize);
                SystemData migratedSave = new SystemData(destinationSize);

                migratedSave.Base_Settings = originalSave.Base_Settings;
                migratedSave.Work_Trial    = originalSave.Work_Trial;
                int savesToCopy = Math.Min(sourceSize, destinationSize);
                for (int i = 0; i < savesToCopy; i++)
                {
                    migratedSave.Setting_Games[i] = originalSave.Setting_Games[i];
                    migratedSave.Sram_Data[i]     = originalSave.Sram_Data[i];
                }

                byte[] migratedData = migratedSave.ToByteArray();

                using (MemoryStream saveFile = new MemoryStream(migratedData))
                {
                    scp.Upload(saveFile, "/usr/game/save/data_008_0000.bin");
                    if (!string.IsNullOrEmpty(lunarPath))
                    {
                        File.WriteAllBytes($@"{lunarPath}\data_008_0000.bin", migratedData);
                    }
                }

                settingsMetaStream.Position = 0;
                using (PsbReader psbReader = new PsbReader(settingsMetaStream))
                {
                    JToken meta = psbReader.Root;
                    meta["FileSize"]     = migratedData.Length;
                    meta["OriginalSize"] = migratedData.Length;
                    (meta["Digest"] as JStream).BinaryData = MD5.Create().ComputeHash(migratedData);

                    PsbWriter psbWriter = new PsbWriter(meta, null);
                    psbWriter.Version = psbReader.Version;

                    using (MemoryStream metaFile = new MemoryStream())
                    {
                        psbWriter.Write(metaFile, null);
                        metaFile.Position = 0;
                        scp.Upload(metaFile, "/usr/game/save/meta_008_0000.bin");

                        if (!string.IsNullOrEmpty(lunarPath))
                        {
                            byte[] metaData = new byte[metaFile.Length];
                            metaFile.Position = 0;
                            metaFile.Read(metaData, 0, metaData.Length);
                            File.WriteAllBytes($@"{lunarPath}\meta_008_0000.bin", metaData);
                        }
                    }
                }
            }
        }
예제 #24
0
        protected override void ProcessRecord()
        {
            foreach (var computer in _computername)
            {
                ConnectionInfo connectInfo;
                if (_keyfile.Equals(""))
                {
                    WriteVerbose("Using SSH Username and Password authentication for connection.");
                    var kIconnectInfo = new KeyboardInteractiveAuthenticationMethod(_credential.GetNetworkCredential().UserName);
                    connectInfo = ConnectionInfoGenerator.GetCredConnectionInfo(computer,
                                                                                _port,
                                                                                _credential,
                                                                                _proxyserver,
                                                                                _proxytype,
                                                                                _proxyport,
                                                                                _proxycredential,
                                                                                kIconnectInfo);

                    // Event Handler for interactive Authentication
                    kIconnectInfo.AuthenticationPrompt += delegate(object sender, AuthenticationPromptEventArgs e)
                    {
                        foreach (var prompt in e.Prompts)
                        {
                            if (prompt.Request.Contains("Password"))
                            {
                                prompt.Response = _credential.GetNetworkCredential().Password;
                            }
                        }
                    };
                }
                else
                {
                    WriteVerbose("Using SSH Key authentication for connection.");
                    connectInfo = ConnectionInfoGenerator.GetKeyConnectionInfo(computer,
                                                                               _port,
                                                                               _keyfile,
                                                                               _credential,
                                                                               _proxyserver,
                                                                               _proxytype,
                                                                               _proxyport,
                                                                               _proxycredential);
                }

                //Ceate instance of SSH Client with connection info
                var client = new ScpClient(connectInfo);


                // Handle host key
                var computer1 = computer;
                client.HostKeyReceived += delegate(object sender, HostKeyEventArgs e)
                {
                    var sb = new StringBuilder();
                    foreach (var b in e.FingerPrint)
                    {
                        sb.AppendFormat("{0:x}:", b);
                    }
                    var fingerPrint = sb.ToString().Remove(sb.ToString().Length - 1);

                    if (_sshHostKeys.ContainsKey(computer1))
                    {
                        if (_sshHostKeys[computer1] == fingerPrint)
                        {
                            if (MyInvocation.BoundParameters.ContainsKey("Verbose"))
                            {
                                Host.UI.WriteVerboseLine("Fingerprint matched trusted fingerprint for host " + computer1);
                            }
                            e.CanTrust = true;
                        }
                        else
                        {
                            var ex = new System.Security.SecurityException("SSH fingerprint mismatch for host " + computer1);
                            ThrowTerminatingError(new ErrorRecord(
                                                      ex,
                                                      "SSH fingerprint mismatch for host " + computer1,
                                                      ErrorCategory.SecurityError,
                                                      computer1));
                        }
                    }
                    else
                    {
                        int choice;
                        if (_acceptkey)
                        {
                            choice = 0;
                        }
                        else
                        {
                            var choices = new Collection <ChoiceDescription>
                            {
                                new ChoiceDescription("Y"),
                                new ChoiceDescription("N")
                            };

                            choice = Host.UI.PromptForChoice("Server SSH Fingerprint", "Do you want to trust the fingerprint " + fingerPrint, choices, 1);
                        }
                        if (choice == 0)
                        {
                            var keymng = new TrustedKeyMng();
                            keymng.SetKey(computer1, fingerPrint);
                            e.CanTrust = true;
                        }
                        else
                        {
                            e.CanTrust = false;
                        }
                    }
                };
                // Set the connection timeout
                client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(_connectiontimeout);

                // Connect to host using Connection info
                client.Connect();

                var counter = 0;
                // Print progess of download.
                client.Downloading += delegate(object sender, ScpDownloadEventArgs e)
                {
                    if (e.Size != 0)
                    {
                        counter++;
                        if (counter > 900)
                        {
                            var percent = Convert.ToInt32((e.Downloaded * 100) / e.Size);

                            if (percent == 100)
                            {
                                return;
                            }

                            var progressRecord = new ProgressRecord(1,
                                                                    "Downloading " + e.Filename,
                                                                    String.Format("{0} Bytes Downloaded of {1}",
                                                                                  e.Downloaded,
                                                                                  e.Size))
                            {
                                PercentComplete = percent
                            };

                            Host.UI.WriteProgress(1, progressRecord);
                            counter = 0;
                        }
                    }
                };

                var localfullPath = Path.GetFullPath(_localfolder);
                WriteVerbose("Downloading " + _remotefolder);
                var dirinfo = new DirectoryInfo(@localfullPath);
                client.Download(_remotefolder, dirinfo);
                client.Disconnect();
                WriteVerbose("Finished downloading.");
            }
        } // End process record
        public void GetFile(string localpath, string remotepath)
        {
            FileStream fi = File.Create(localpath);

            ScpConn.Download(remotepath, fi);
        }
예제 #26
0
        bool PatchDirector(LogWindow log)
        {
            var SshConnectionInfo = new ConnectionInfo(Address.Text.ToString(), Username.Text, new PasswordAuthenticationMethod(Username.Text, Password.Text));

            SshConnectionInfo.RetryAttempts = 1;
            SshConnectionInfo.Timeout       = TimeSpan.FromSeconds(2);

            using (var ssh = new ScpClient(SshConnectionInfo))
            {
                log.WriteNormal($"Connecting to director via SCP.. ");

                try
                {
                    ssh.Connect();
                }
                catch (System.Exception e)
                {
                    log.WriteError(e);
                    return(false);
                }

                log.WriteSuccess($" .. connected!\n");

                // Get the existing certificate
                using (var stream = new MemoryStream())
                {
                    log.WriteNormal($"Downloading /etc/openvpn/clientca-prod.pem\n");
                    ssh.Download("/etc/openvpn/clientca-prod.pem", stream);
                    log.WriteSuccess($"Done - got {stream.Length} bytes\n\n");

                    stream.Position = 0;

                    var backupName = $"/etc/openvpn/clientca-prod.{DateTime.Now.ToString( "yyyy-dd-M--HH-mm-ss" )}.backup";
                    log.WriteNormal($"Uploading {backupName}\n");
                    ssh.Upload(stream, backupName);
                    log.WriteSuccess($"Done!\n\n");

                    log.WriteNormal($"Constructing new clientca-prod.pem\n");
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        stream.Position = 0;

                        var certificate = reader.ReadToEnd();

                        certificate += "\n";

                        log.WriteNormal($"  Reading Certs/public.pem\n");
                        var localCert = System.IO.File.ReadAllText("Certs/public.pem");

                        var localBackupName = $"Certs/clientca-prod.{DateTime.Now.ToString( "yyyy-dd-M--HH-mm-ss" )}.backup";
                        log.WriteNormal($"  Downloading to {localBackupName}\n");
                        System.IO.File.WriteAllText(localBackupName, certificate);

                        if (certificate.Contains(localCert))
                        {
                            log.WriteError($"The certificate on the director already contains our public key!\n");
                            return(false);
                        }
                        else
                        {
                            //
                            // We just add our public key to the end
                            //
                            certificate += localCert;
                        }

                        //
                        // This serves no purpose but it doesn't hurt to have it hanging around
                        //
                        localBackupName += ".new";
                        log.WriteNormal($"  Downloading to {localBackupName}\n");
                        System.IO.File.WriteAllText(localBackupName, certificate);


                        //
                        // Upload the modded certificates to the director
                        //
                        log.WriteNormal($"Uploading New Certificate..\n");
                        using (var wstream = new MemoryStream())
                        {
                            using (StreamWriter writer = new StreamWriter(wstream))
                            {
                                writer.Write(certificate);
                                writer.Flush();

                                wstream.Position = 0;
                                ssh.Upload(wstream, "/etc/openvpn/clientca-prod.pem");
                            }
                        }

                        log.WriteSuccess($"Done!\n");
                    }
                }
            }

            return(true);
        }
        public bool DoWork(Util.UpdateStatusDelegate SetStatus, Util.ShouldCancelDelegate ShouldCancel)
        {
            if (ShouldCancel())
            {
                return(false);
            }

            SetStatus("Connecting to SSH...", 0);

            using (VyattaShell Shell = new VyattaShell(Data.Address, Data.Username, Data.Password))
            {
                if (ShouldCancel())
                {
                    return(false);
                }

                //Verify that we can identify the device

                SetStatus("Identifying device...", 5);

                var Version = Shell.RunCommand("cat /proc/version");
                if (!Version.Contains("edgeos"))
                {
                    throw new Exception("The device is not running EdgeOS and is not supported. Device ");
                }

                if (ShouldCancel())
                {
                    return(false);
                }

                //Enter configure mode

                SetStatus("Processing routing interfaces...", 8);

                Dictionary <string, string> Gateways = IPRoute.GetDefaultGateways(Shell);

                SetStatus("Processing interface list...", 16);
                string ShowInterfaces = Shell.RunCommand("show interfaces");

                Regex ParseInterfaces = new Regex(@"([\w\.]+)\s+([0-9.\-]+(:?\/[0-9]+)?)\s+(\w\/\w)\s+(\w+)?");

                Data.Interfaces = new ObservableCollection <InterfaceMapping>();

                string[] InterfaceLines = ShowInterfaces.Split(new char[] { '\n' });
                foreach (string Line in InterfaceLines)
                {
                    Match Match = ParseInterfaces.Match(Line);
                    if (Match.Success)
                    {
                        InterfaceMapping Mapping = new InterfaceMapping();

                        Mapping.Interface   = Match.Groups[1].Value;
                        Mapping.IPAddress   = Match.Groups[2].Value == "-" ? "" : Match.Groups[2].Value;
                        Mapping.Codes       = Match.Groups[4].Value;
                        Mapping.Description = Match.Groups[5].Value;

                        string Gateway;
                        if (Gateways.TryGetValue(Mapping.Interface, out Gateway))
                        {
                            Mapping.Gateway = Gateway;
                        }

                        Data.Interfaces.Add(Mapping);
                    }
                }
            }

            SetStatus("Connecting over SCP...", 60);

            if (ShouldCancel())
            {
                return(false);
            }

            using (ScpClient Client = new ScpClient(Data.Address, Data.Username, Data.Password))
            {
                Client.Connect();

                if (ShouldCancel())
                {
                    return(false);
                }

                SetStatus("Downloading config...", 80);

                using (Stream tempFile = new FileStream(TempPath, FileMode.CreateNew))
                {
                    Client.Download("/config/config.boot", tempFile);
                }

                SetStatus("Parsing existing config...", 85);

                string Errors = "";
                Data.OldConfigLines = File.ReadAllLines(TempPath);
                Data.ConfigRoot     = VyattaConfigUtil.ReadFromFile(TempPath, ref Errors);
                if (Errors.Length > 0)
                {
                    throw new Exception(Errors);
                }

                SetStatus("Downloading current template...", 90);

                try
                {
                    using (Stream tempTemplateFile = new FileStream(TempTemplatePath, FileMode.CreateNew))
                    {
                        Client.Download("/config/vcu/current.vcu", tempTemplateFile);
                    }

                    SetStatus("Parsing current template...", 95);

                    Errors            = "";
                    Data.TemplateRoot = VyattaConfigUtil.ReadFromFile(TempTemplatePath, ref Errors);
                    if (Errors.Length > 0)
                    {
                        throw new Exception(Errors);
                    }
                }
                catch (SshException e)
                {
                    if (!e.Message.Contains("No such file or directory"))
                    {
                        throw e;
                    }

                    //It's quite okay to fail here, it means the user hasn't uploaded
                    //a config with the tool yet.

                    Data.TemplateRoot = new VyattaConfigObject(null);
                }

                if (ShouldCancel())
                {
                    return(false);
                }

                SetStatus("Disconnecting...", 98);

                Client.Disconnect();
            }

            SetStatus("Completed.", 100);

            return(true);
        }
        public bool DoWork(Util.UpdateStatusDelegate SetStatus, Util.ShouldCancelDelegate ShouldCancel)
        {
            SetStatus("Connecting to SSH...", 0);

            using (VyattaShell Shell = new VyattaShell(Data.Address, Data.Username, Data.Password))
            {
                SetStatus("Checking for wget...", 5);

                string DoesWgetExist = Shell.RunCommand("wget");
                if (DoesWgetExist.Contains("command not found"))
                {
                    SetStatus("Adding main debian sources...", 10);

                    Shell.RunCommand("configure");
                    Shell.RunCommand("set system package repository wheezy components 'main contrib non-free'");
                    Shell.RunCommand("set system package repository wheezy distribution wheezy");
                    Shell.RunCommand("set system package repository wheezy url http://http.us.debian.org/debian");

                    SetStatus("Committing changes...", 10);
                    Shell.RunCommand("commit");
                    Shell.RunCommand("save");
                    Shell.RunCommand("exit");

                    var WgetResult = Shell.RunCommand("sudo apt-get install wget");
                    if (WgetResult.Contains("Package 'wget' has no installation candidate") ||
                        WgetResult.Contains("Unable to locate package wget"))
                    {
Prev:
                        SetStatus("Updating package lists...", 15);
                        var UpdateResult = Shell.RunCommand("sudo apt-get update");
                        if (FreeUpPreviousImage(Shell, UpdateResult))
                        {
                            goto Prev;
                        }
                        if (UpdateResult.Contains("Not Found") || UpdateResult.Contains("not Found"))
                        {
                            string[] Lines           = UpdateResult.Split(new [] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                            int      Max5Lines       = Lines.Length - 5;
                            string   LastUpdateBlock = string.Join("\n", Lines, Max5Lines, Lines.Length - Math.Max(Max5Lines, 0));

                            throw new Exception("One or more packages was not found:\n" + LastUpdateBlock);
                        }

Prev2:
                        SetStatus("Installing wget...", 20);
                        WgetResult = Shell.RunCommand("sudo apt-get install wget");
                        if (WgetResult.Contains("Package 'wget' has no installation candidate") ||
                            WgetResult.Contains("Unable to locate package wget"))
                        {
                            string[] Lines           = UpdateResult.Split(new [] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                            int      Max5Lines       = Lines.Length - 5;
                            string   LastUpdateBlock = string.Join("\n", Lines, Max5Lines, Lines.Length - Math.Max(Max5Lines, 0));

                            throw new Exception("Unable to install wget:\n" + LastUpdateBlock + WgetResult);
                        }
                        if (FreeUpPreviousImage(Shell, UpdateResult))
                        {
                            goto Prev2;
                        }

                        SetStatus("Checking wget...", 25);
                        string RecheckWget = Shell.RunCommand("wget");

                        if (!RecheckWget.Contains("wget: missing URL"))
                        {
                            throw new Exception("wget not found after installation.");
                        }
                    }
                }
                else if (!DoesWgetExist.Contains("wget: missing URL"))
                {
                    throw new Exception("Unknown state - could not determine if wget exists.");
                }

                if (ShouldCancel())
                {
                    return(false);
                }

                SetStatus("Checking for Entware...", 30);

                string DoesOpkgExist = Shell.RunCommand("/opt/bin/opkg");

                if (DoesOpkgExist.Contains("No such file or directory"))
                {
                    SetStatus("Installing Entware...", 32);

                    string GetInstaller = Shell.RunCommand("wget -O - https://pkg.entware.net/binaries/mipsel/installer/installer.sh | sudo sh");
                    if (GetInstaller.Contains("Connection refused"))
                    {
                        if (MessageBox.Show("The connection to the Entware package server was refused over https.\nDo you want to try again over http? This might indicate a problem on their server or an attempt to interfere with your installation.", "Try again over an insecure connection?", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                        {
                            Shell.RunCommand("wget -O - http://pkg.entware.net/binaries/mipsel/installer/installer.sh | sudo sh");
                        }
                        else
                        {
                            return(false);
                        }
                    }

                    SetStatus("Checking for Entware...", 60);

                    DoesOpkgExist = Shell.RunCommand("/opt/bin/opkg");
                    if (!DoesOpkgExist.Contains("usage: opkg"))
                    {
                        throw new Exception("Unknown state - could not determine if opkg exists.");
                    }
                }
                else if (!DoesOpkgExist.Contains("usage: opkg"))
                {
                    throw new Exception("Unknown state - could not determine if opkg exists.");
                }

                if (ShouldCancel())
                {
                    return(false);
                }

                SetStatus("Checking for newer packages...", 61);
                Shell.RunCommand("sudo /opt/bin/opkg update");

                SetStatus("Checking for DNSCrypt...", 63);

                bool WasInstalled = false;

                string DoesDNSCryptExist = Shell.RunCommand("/opt/sbin/dnscrypt-proxy --help");
                if (DoesDNSCryptExist.Contains("No such file or directory"))
                {
                    SetStatus("Installing DNSCrypt...", 66);

                    Shell.RunCommand("sudo /opt/bin/opkg install dnscrypt-proxy", new Regex("Choose server from list or hit Enter to continue"), ChooseResolver);

                    DoesDNSCryptExist = Shell.RunCommand("/opt/sbin/dnscrypt-proxy --help");
                    if (!DoesDNSCryptExist.Contains("dnscrypt-proxy "))
                    {
                        throw new Exception("Unknown state - could not determine if DNSCrypt exists.");
                    }

                    WasInstalled = true;
                }
                else if (!DoesDNSCryptExist.Contains("dnscrypt-proxy "))
                {
                    throw new Exception("Unknown state - could not determine if DNSCrypt exists.");
                }

                if (ShouldCancel())
                {
                    return(false);
                }

                if (!WasInstalled)
                {
                    //It's entirely possible that we've changed something and we previously had DNSCrypt running
                    //So we should find it and kill it.

                    SetStatus("Killing DNSCrypt if running...", 68);

                    KillDNSCrypt(Shell);

                    Shell.RunCommand("sudo /opt/bin/opkg upgrade dnscrypt-proxy", new Regex("Choose server from list or hit Enter to continue"), ChooseResolver);
                }

                if (ShouldCancel())
                {
                    return(false);
                }

                SetStatus("Connecting via SCP...", 80);

                string DNSCryptProxyPort = "65053";

                using (ScpClient Client = new ScpClient(Data.Address, Data.Username, Data.Password))
                {
                    Client.Connect();

                    if (ShouldCancel())
                    {
                        return(false);
                    }

                    SetStatus("Downloading daemon profile...", 85);

                    string OriginalDaemonPath = Path.ChangeExtension(Path.GetTempFileName(), Guid.NewGuid().ToString());
                    string NewDaemonPath      = Path.ChangeExtension(Path.GetTempFileName(), Guid.NewGuid().ToString());
                    string DNSCryptResolvers  = Path.ChangeExtension(Path.GetTempFileName(), Guid.NewGuid().ToString());
                    string StartupScriptPath  = Path.ChangeExtension(Path.GetTempFileName(), Guid.NewGuid().ToString());

                    using (Stream tempFile = new FileStream(OriginalDaemonPath, FileMode.CreateNew))
                    {
                        Client.Download("/opt/etc/init.d/S09dnscrypt-proxy", tempFile);
                    }

                    SetStatus("Downloading resolver list...", 87);

                    using (Stream tempFile = new FileStream(DNSCryptResolvers, FileMode.CreateNew))
                    {
                        Client.Download("/opt/share/dnscrypt-proxy/dnscrypt-resolvers.csv", tempFile);
                    }

                    SetStatus("Processing daemon file...", 88);

                    string[] DaemonFile = File.ReadAllLines(OriginalDaemonPath);

                    string LocalAddress = "--local-address=127.0.0.1:";
                    string Resolver     = "-R ";
                    //First match group is the port it starts on, the second is the current
                    var DaemonArgsRegex = new Regex(@".*" + LocalAddress + @"(\d+).*" + Resolver + @"([\w.-]+).*");
                    for (int LineIndex = 0; LineIndex < DaemonFile.Length; LineIndex++)
                    {
                        if (DaemonFile[LineIndex].StartsWith("ARGS="))
                        {
                            var Match = DaemonArgsRegex.Match(DaemonFile[LineIndex]);
                            if (!Match.Success)
                            {
                                throw new Exception("Unable to parse dnscrypt-proxy arg line: " + DaemonFile[LineIndex]);
                            }

                            DNSCryptProxyPort = Match.Groups[1].Value;

                            var OldResolverName = Match.Groups[2].Value;
                            var ResolverName    = Match.Groups[2].Value;

                            bool Aborted = false;
                            Parent.Invoke(new Action(() =>
                            {
                                var PickDNS = new DNSCryptResolverPicker(DNSCryptResolvers);
                                if (PickDNS.ShowDialog() != DialogResult.OK)
                                {
                                    Aborted = true;
                                }

                                ResolverName = PickDNS.GetPickedResolver();
                            }));

                            if (Aborted)
                            {
                                return(false);
                            }

                            DaemonFile[LineIndex] = DaemonFile[LineIndex].Replace(Resolver + OldResolverName, Resolver + ResolverName);
                        }
                    }

                    SetStatus("Uploading new daemon file...", 88);

                    using (TextWriter FileOut = new StreamWriter(NewDaemonPath))
                    {
                        FileOut.NewLine = "\n";

                        foreach (var Line in DaemonFile)
                        {
                            FileOut.WriteLine(Line);
                        }
                    }

                    using (Stream uploadFile = new FileStream(NewDaemonPath, FileMode.Open))
                    {
                        Client.Upload(uploadFile, "/tmp/NewDaemonProfile");
                    }

                    Shell.RunCommand("sudo cp /tmp/NewDaemonProfile /opt/etc/init.d/S09dnscrypt-proxy");
                    Shell.RunCommand("rm /tmp/NewDaemonProfile");
                    Shell.RunCommand("sudo chmod 0755 /opt/etc/init.d/S09dnscrypt-proxy");

                    SetStatus("Uploading new startup script...", 90);

                    string[] StartupScript = new string[]
                    {
                        "#!/bin/bash",
                        "sudo /opt/etc/init.d/S09dnscrypt-proxy start",
                        "exit"
                    };

                    using (TextWriter FileOut = new StreamWriter(StartupScriptPath))
                    {
                        FileOut.NewLine = "\n";

                        foreach (var Line in StartupScript)
                        {
                            FileOut.WriteLine(Line);
                        }
                    }

                    using (Stream uploadFile = new FileStream(StartupScriptPath, FileMode.Open))
                    {
                        Client.Upload(uploadFile, "/tmp/NewStartupScript");
                    }

                    Client.Disconnect();
                }

                Shell.RunCommand("sudo cp /tmp/NewStartupScript /config/scripts/post-config.d/start_dnscrypt.sh");
                Shell.RunCommand("rm /tmp/NewStartupScript");
                Shell.RunCommand("sudo chmod 0755 /config/scripts/post-config.d/start_dnscrypt.sh");

                SetStatus("Starting DNSCrypt...", 92);

                Shell.RunCommand("sudo /config/scripts/post-config.d/start_dnscrypt.sh");

                SetStatus("Configuring dnsmasq to use DNSCrypt...", 94);

                Shell.RunCommand("configure");
                Shell.RunCommand(string.Format("set service dns forwarding options \"server=127.0.0.1#{0}\"", DNSCryptProxyPort));
                Shell.RunCommand("set service dns forwarding options proxy-dnssec");

                //WARNING: This line disables all other forms of DNS. If DNSCrypt is not working you'll lose DNS completely.
                Shell.RunCommand("set service dns forwarding options no-resolv");

                SetStatus("Committing changes...", 96);
                Shell.RunCommand("commit");
                Shell.RunCommand("save");
                Shell.RunCommand("exit");

                SetStatus("Restarting dnsmasq...", 98);

                Shell.RunCommand("sudo /etc/init.d/dnsmasq restart");
            }

            //^(\d+\))\W+([a-zA-Z0-9\-.]+)\W+\((.*)\)$

            SetStatus("Completed.", 100);

            return(true);
        }
예제 #29
0
        public void Test_Scp_Directory_Upload_Download()
        {
            RemoveAllFiles();

            using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
            {
                scp.Connect();

                var uploadDirectory = Directory.CreateDirectory(string.Format("{0}\\{1}", Path.GetTempPath(), Path.GetRandomFileName()));
                for (int i = 0; i < 3; i++)
                {
                    var subfolder = Directory.CreateDirectory(string.Format(@"{0}\folder_{1}", uploadDirectory.FullName, i));
                    for (int j = 0; j < 5; j++)
                    {
                        this.CreateTestFile(string.Format(@"{0}\file_{1}", subfolder.FullName, j), 1);
                    }
                    this.CreateTestFile(string.Format(@"{0}\file_{1}", uploadDirectory.FullName, i), 1);
                }

                scp.Upload(uploadDirectory, "uploaded_dir");

                var downloadDirectory = Directory.CreateDirectory(string.Format("{0}\\{1}", Path.GetTempPath(), Path.GetRandomFileName()));

                scp.Download("uploaded_dir", downloadDirectory);

                var uploadedFiles = uploadDirectory.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
                var downloadFiles = downloadDirectory.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

                var result = from f1 in uploadedFiles
                             from f2 in downloadFiles
                             where
                                f1.FullName.Substring(uploadDirectory.FullName.Length) == f2.FullName.Substring(downloadDirectory.FullName.Length)
                                && CalculateMD5(f1.FullName) == CalculateMD5(f2.FullName)
                             select f1;

                var counter = result.Count();

                scp.Disconnect();

                Assert.IsTrue(counter == uploadedFiles.Length && uploadedFiles.Length == downloadFiles.Length);
            }
        }
예제 #30
0
 internal void ExportFile(string source, string destination)
 {
     SCPClient.Connect();
     SCPClient.Download(source, new FileInfo(destination));
     SCPClient.Disconnect();
 }
예제 #31
0
        protected override void ProcessRecord()
        {
            foreach (var computer in _computername)
            {
                ConnectionInfo connectInfo = null;
                switch (ParameterSetName)
                {
                case "NoKey":
                    WriteVerbose("Using SSH Username and Password authentication for connection.");
                    var kIconnectInfo = new KeyboardInteractiveAuthenticationMethod(_credential.UserName);
                    connectInfo = ConnectionInfoGenerator.GetCredConnectionInfo(computer,
                                                                                _port,
                                                                                _credential,
                                                                                _proxyserver,
                                                                                _proxytype,
                                                                                _proxyport,
                                                                                _proxycredential,
                                                                                kIconnectInfo);

                    // Event Handler for interactive Authentication
                    kIconnectInfo.AuthenticationPrompt += delegate(object sender, AuthenticationPromptEventArgs e)
                    {
                        foreach (var prompt in e.Prompts)
                        {
                            if (prompt.Request.Contains("Password"))
                            {
                                prompt.Response = _credential.GetNetworkCredential().Password;
                            }
                        }
                    };
                    break;

                case "Key":
                    ProviderInfo provider;
                    var          pathinfo      = GetResolvedProviderPathFromPSPath(_keyfile, out provider);
                    var          localfullPath = pathinfo[0];
                    connectInfo = ConnectionInfoGenerator.GetKeyConnectionInfo(computer,
                                                                               _port,
                                                                               localfullPath,
                                                                               _credential,
                                                                               _proxyserver,
                                                                               _proxytype,
                                                                               _proxyport,
                                                                               _proxycredential);
                    break;

                case "KeyString":
                    WriteVerbose("Using SSH Key authentication for connection.");
                    connectInfo = ConnectionInfoGenerator.GetKeyConnectionInfo(computer,
                                                                               _port,
                                                                               _keystring,
                                                                               _credential,
                                                                               _proxyserver,
                                                                               _proxytype,
                                                                               _proxyport,
                                                                               _proxycredential);
                    break;

                default:
                    break;
                }

                //Ceate instance of SSH Client with connection info
                var client = new ScpClient(connectInfo);
                // Set the connection timeout
                client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(_connectiontimeout);

                // Handle host key
                if (_force)
                {
                    WriteWarning("Host key for " + computer + " is not being verified since Force switch is used.");
                }
                else
                {
                    var computer1 = computer;
                    client.HostKeyReceived += delegate(object sender, HostKeyEventArgs e)
                    {
                        var sb = new StringBuilder();
                        foreach (var b in e.FingerPrint)
                        {
                            sb.AppendFormat("{0:x}:", b);
                        }
                        var fingerPrint = sb.ToString().Remove(sb.ToString().Length - 1);

                        if (MyInvocation.BoundParameters.ContainsKey("Verbose"))
                        {
                            Host.UI.WriteVerboseLine("Fingerprint for " + computer1 + ": " + fingerPrint);
                        }

                        if (_sshHostKeys.ContainsKey(computer1))
                        {
                            if (_sshHostKeys[computer1] == fingerPrint)
                            {
                                if (MyInvocation.BoundParameters.ContainsKey("Verbose"))
                                {
                                    Host.UI.WriteVerboseLine("Fingerprint matched trusted fingerprint for host " + computer1);
                                }
                                e.CanTrust = true;
                            }
                            else
                            {
                                e.CanTrust = false;
                            }
                        }
                        else
                        {
                            if (_errorOnUntrusted)
                            {
                                e.CanTrust = false;
                            }
                            else
                            {
                                int choice;
                                if (_acceptkey)
                                {
                                    choice = 0;
                                }
                                else
                                {
                                    var choices = new Collection <ChoiceDescription>
                                    {
                                        new ChoiceDescription("Y"),
                                        new ChoiceDescription("N")
                                    };

                                    choice = Host.UI.PromptForChoice("Server SSH Fingerprint", "Do you want to trust the fingerprint " + fingerPrint, choices, 1);
                                }
                                if (choice == 0)
                                {
                                    var keymng = new TrustedKeyMng();
                                    keymng.SetKey(computer1, fingerPrint);
                                    e.CanTrust = true;
                                }
                                else
                                {
                                    e.CanTrust = false;
                                }
                            }
                        }
                    };
                }
                try
                {
                    // Connect to host using Connection info
                    client.Connect();

                    var _progresspreference = (ActionPreference)this.SessionState.PSVariable.GetValue("ProgressPreference");

                    if (_noProgress == false)
                    {
                        var counter = 0;
                        // Print progess of download.

                        client.Downloading += delegate(object sender, ScpDownloadEventArgs e)
                        {
                            if (e.Size != 0)
                            {
                                counter++;
                                if (counter > 900)
                                {
                                    var percent = Convert.ToInt32((e.Downloaded * 100) / e.Size);
                                    if (percent == 100)
                                    {
                                        return;
                                    }

                                    var progressRecord = new ProgressRecord(1,
                                                                            "Downloading " + e.Filename,
                                                                            String.Format("{0} Bytes Downloaded of {1}",
                                                                                          e.Downloaded, e.Size))
                                    {
                                        PercentComplete = percent
                                    };

                                    Host.UI.WriteProgress(1, progressRecord);
                                    counter = 0;
                                }
                            }
                        };
                    }
                    WriteVerbose("Connection successful");
                }
                catch (Renci.SshNet.Common.SshConnectionException e)
                {
                    ErrorRecord erec = new ErrorRecord(e, null, ErrorCategory.SecurityError, client);
                    WriteError(erec);
                }
                catch (Renci.SshNet.Common.SshOperationTimeoutException e)
                {
                    ErrorRecord erec = new ErrorRecord(e, null, ErrorCategory.OperationTimeout, client);
                    WriteError(erec);
                }
                catch (Renci.SshNet.Common.SshAuthenticationException e)
                {
                    ErrorRecord erec = new ErrorRecord(e, null, ErrorCategory.SecurityError, client);
                    WriteError(erec);
                }
                catch (Exception e)
                {
                    ErrorRecord erec = new ErrorRecord(e, null, ErrorCategory.InvalidOperation, client);
                    WriteError(erec);
                }

                try
                {
                    if (client.IsConnected)
                    {
                        var localfullPath = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(_localfile);

                        WriteVerbose("Downloading " + _remotefile);
                        WriteVerbose("Saving as " + localfullPath);
                        var fil = new FileInfo(@localfullPath);

                        // Download the file
                        client.Download(_remotefile, fil);

                        client.Disconnect();
                    }
                }
                catch (Exception e)
                {
                    ErrorRecord erec = new ErrorRecord(e, null, ErrorCategory.OperationStopped, client);
                    WriteError(erec);
                }
            }
        } // End process record
예제 #32
0
        public void Test_Scp_File_Upload_Download_Events()
        {
            using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
            {
                scp.Connect();

                var uploadFilenames = new string[10];

                for (int i = 0; i < uploadFilenames.Length; i++)
                {
                    uploadFilenames[i] = Path.GetTempFileName();
                    this.CreateTestFile(uploadFilenames[i], 1);
                }

                var uploadedFiles = uploadFilenames.ToDictionary((filename) => Path.GetFileName(filename), (filename) => 0L);
                var downloadedFiles = uploadFilenames.ToDictionary((filename) => string.Format("{0}.down", Path.GetFileName(filename)), (filename) => 0L);

                scp.Uploading += delegate(object sender, ScpUploadEventArgs e)
                {
                    uploadedFiles[e.Filename] = e.Uploaded;
                };

                scp.Downloading += delegate(object sender, ScpDownloadEventArgs e)
                {
                    downloadedFiles[string.Format("{0}.down", e.Filename)] = e.Downloaded;
                };

                Parallel.ForEach(uploadFilenames,
                    (filename) =>
                    {
                        scp.Upload(new FileInfo(filename), Path.GetFileName(filename));
                    });

                Parallel.ForEach(uploadFilenames,
                    (filename) =>
                    {
                        scp.Download(Path.GetFileName(filename), new FileInfo(string.Format("{0}.down", filename)));
                    });

                var result = from uf in uploadedFiles
                             from df in downloadedFiles
                             where
                                 string.Format("{0}.down", uf.Key) == df.Key
                                 && uf.Value == df.Value
                             select uf;

                scp.Disconnect();

                Assert.IsTrue(result.Count() == uploadFilenames.Length && uploadFilenames.Length == uploadedFiles.Count && uploadedFiles.Count == downloadedFiles.Count);
            }
        }