Ejemplo n.º 1
0
 public void download(String serverPath, String localPath)
 {
     myScp = new Scp(ftpAdr, user, password);
     myScp.Connect(22);
     myScp.Get(serverPath, localPath);
     myScp.Close();
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Close the connection.
 /// </summary>
 public void Close()
 {
     if (_scp.Connected)
     {
         _scp.Close();
     }
 }
Ejemplo n.º 3
0
        public static bool SshMvAllFileSync(string host, string user, string password, string LocalFilePath, string remoteFilePath, out string errMes, Action <string> reportProcess, Action <string> reportError)
        {
            SshTransferProtocolBase sshCp = new Scp(host, user, password);

            sshCp.Connect();
            bool outResult = SshMvAllFileSync(sshCp, LocalFilePath, remoteFilePath, out errMes, reportProcess, reportError);

            sshCp.Close();
            return(outResult);
        }
Ejemplo n.º 4
0
        private void btnTransfer_Click(object sender, EventArgs e)
        {
            if (host_input.Text == "")
            {
                MessageBox.Show("Please input a Host.", "Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (user_input.Text == "")
            {
                MessageBox.Show("Please input a User.", "Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            Scp sshCp = new Scp(host_input.Text, user_input.Text);

            if (password_input.Text != "")
            {
                sshCp.Password = password_input.Text;
            }
            else if (privatekey_input.FileName != "")
            {
                sshCp.AddIdentityFile(privatekey_input.FileName);
            }
            else
            {
                MessageBox.Show("Please input a password or private key file.", "Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (serverlocation_input.Text == "")
            {
                MessageBox.Show("Please input a Server Side location to store the file.", "Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            sshCp.OnTransferStart    += new FileTransferEvent(sshCp_OnTransferStart);
            sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
            sshCp.OnTransferEnd      += new FileTransferEvent(sshCp_OnTransferEnd);

            sshCp.Connect();

            string destination = serverlocation_input.Text;

            if (!destination.EndsWith("/"))
            {
                destination += '/';
            }
            destination += "dnadata/" + this.dest_folder;

            sshCp.To(this.interface_folder, destination, true);

            sshCp.Close();

            MessageBox.Show("Transfer complete!", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Ejemplo n.º 5
0
 private void SshCp_OnTransferEnd(string src, string dst, int transferredBytes, int totalBytes, string message)
 {
     mTransferredFileCount++;
     mTransferredFileSize += totalBytes;
     if (mTransferredFileCount == mFileCount)
     {
         SaveSettings();
         mScp.Close();
         Reset();
     }
 }
Ejemplo n.º 6
0
        public void auxTransfer(RaspberryPi pi, string local, string remote)
        {
            Scp scp = new Scp(pi.getHostname(), pi.getUser(), pi.getPassword());

            try
            {
                scp.Connect();
                scp.From(remote, local);
                scp.Close();
            }

            catch (Exception)
            { }
        }
Ejemplo n.º 7
0
        public static void TransferFileFromMachine(string remoteFile, string localPath)
        {
            try
            {
                //Create a new SCP instance
                Scp scp = new Scp(MACHINE_IP, USER_NAME, PASSWORD);

                scp.Connect();

                //Copy a file from remote SSH server to local machine
                scp.From(remoteFile, localPath);

                scp.Close();
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 8
0
        }     // end BajarSCP

        public static void SubirSCP(string pServidor, string pUsuario, string pPassword, string pFileRemoto, string pFileLocal)
        {
            // instancia del objeto SCP
            Scp SCP = new Scp(pServidor, pUsuario, pPassword);

            try
            {
                // crear conexion SSH
                SCP.Connect();
                // Subir el archivo
                SCP.To(pFileLocal, pFileRemoto, false);
            }
            catch (Exception ex)
            { throw ex; }
            finally
            {
                //cerrar conexion SCP
                SCP.Close();
            } // end try
        }     // end SubirSCP
Ejemplo n.º 9
0
        protected override void RunWithSession(ref Session session)
        {
            string toDirectory = "/tmp";

            Status = Messages.IN_PROGRESS;
            log.InfoFormat("{0} Upgrade...", Host.address);
            int sshdstate = 1;
            int sshport   = 22;
            Dictionary <string, string> _servicearguments = new Dictionary <string, string>();

            _servicearguments.Add("servicename", "sshd");
            try
            {
                string rsvalue = XenAPI.Host.call_plugin(Host.Connection.Session, Host.opaque_ref, "serviceinfo.py", "getserviceinfo", _servicearguments);
                rsvalue = rsvalue.Replace("[", "").Replace("]", "").Replace("'", "").Replace("\\n", "");
                string[] rsvalues = rsvalue.Split(',');
                if (rsvalues.Length > 1 && "0".Equals(rsvalues[1].Trim()))
                {
                    log.InfoFormat("{0} sshd is running ...", Host.address);
                }
                if (rsvalues.Length > 1 && "3".Equals(rsvalues[1].Trim()))
                {
                    log.InfoFormat("{0} sshd is stop, start sshd ...", Host.address);
                    rsvalue  = XenAPI.Host.call_plugin(Host.Connection.Session, Host.opaque_ref, "serviceinfo.py", "startserviceinfo", _servicearguments);
                    rsvalue  = rsvalue.Replace("[", "").Replace("]", "").Replace("'", "").Replace("\\n", "");
                    rsvalues = rsvalue.Split(',');
                    if (rsvalues.Length > 1 && "0".Equals(rsvalues[1].Trim()))
                    {
                        sshdstate = 0;
                    }
                    else
                    {
                        throw new Exception(Messages.START_SSH_FAILED);
                    }
                }
                //sshport = 7443;
            }catch (Exception)
            {}
            FileStream file        = new FileStream(_fileName, FileMode.Open);
            string     topfilename = Path.GetFileNameWithoutExtension(_fileName);

            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            log.InfoFormat("md5.......{0}", sb);

            SshTransferProtocolBase sshCp;

            //sshCp = new Sftp(Host.address, Host.Connection.Username);
            sshCp = new Scp(Host.address, Host.Connection.Username);

            sshCp.Password = Host.Connection.Password;

            log.InfoFormat("sshCp Connecting...");
            sshCp.Connect(sshport);

            log.InfoFormat("sshCp Connecting OK .......");

            String Programurl = Program.AssemblyDir;

            sshCp.Put(_fileName, toDirectory + "/" + Path.GetFileName(_fileName));

            log.InfoFormat("cp Installation package ok......");

            sshCp.Put(Programurl + "\\halsign_host_upgrade.py", "/etc/xapi.d/plugins/halsign_host_upgrade.py");

            log.InfoFormat("cp halsign_host_upgrade.py ok......");

            SshShell ssh = new SshShell(Host.address, Host.Connection.Username);

            ssh.Password = Host.Connection.Password;
            string productVersion = Host.ProductVersion;

            log.InfoFormat("sshshell Connecting...");
            ssh.Connect(sshport);

            log.InfoFormat("sshshell Connecting ok...");

            ssh.ExpectPattern = "#";
            ssh.RemoveTerminalEmulationCharacters = true;

            //System.Console.WriteLine();
            //Thread.Sleep(1000);
            //ssh.WriteLine("chmod 777 /etc/xapi.d/plugins/halsign_host_upgrade.py;echo $?");
            //string output = ssh.Expect("#");

            //log.InfoFormat(output);

            while (true)
            {
                ssh.WriteLine("chmod 777 /etc/xapi.d/plugins/halsign_host_upgrade.py;echo $?");
                string output = ssh.Expect("#");
                log.InfoFormat(output);

                if (output.IndexOf("rwxrwxrwx") > -1)
                {
                    break;
                }
                ssh.WriteLine("ls -ld /etc/xapi.d/plugins/halsign_host_upgrade.py |awk '{print $1}'|sed 's/^[a-zA-Z-]//'");
                output = ssh.Expect("#");
                log.InfoFormat(output);

                if (output.IndexOf("rwxrwxrwx") > -1)
                {
                    break;
                }
            }

            ssh.WriteLine("exit");
            ssh.Close();
            log.InfoFormat("sshshell Disconnecting OK.....");

            sshCp.Close();

            log.InfoFormat("sshCp Disconnecting OK.....");

            if (sshdstate == 0)
            {
                XenAPI.Host.call_plugin(Host.Connection.Session, Host.opaque_ref, "serviceinfo.py", "stopserviceinfo", _servicearguments);
            }

            _arguments = new Dictionary <string, string>();
            _arguments.Add("md5", sb.ToString());
            _arguments.Add("filename", topfilename);
            _arguments.Add("toDirectory", toDirectory);
            Status = Messages.PLAN_ACTION_STATUS_HOST_UPGRADED;
            string value = XenAPI.Host.call_plugin(session, Host.opaque_ref, "halsign_host_upgrade.py", "main", _arguments);

            if (value.ToLower() == "true")
            {
                //base.RunWithSession(ref session);
                Status = Messages.PLAN_ACTION_STATUS_HOST_UPGRADED;
            }
            else if (",1,2,5,6,7,10,11,12,13,1001,1002,1003,1004,1005,1006,".IndexOf("," + value + ",") > -1)
            {
                value = value == "2" ? "1" : value;

                string Ms = "PATCH_UPGRADE_" + value;

                if (value.Equals("1"))
                {
                    //Status = (string)XenAdmin.Messages.ResourceManager.GetString(Ms, resourceCulture);
                    throw new Exception("PATCHOK_" + (string)XenAdmin.Messages.ResourceManager.GetString(Ms));
                }
                else
                {
                    throw new Exception((string)XenAdmin.Messages.ResourceManager.GetString(Ms));
                }
            }
            else
            {
                throw new Exception(Messages.ERROR + ":" + value);
            }
        }