Class for handling SCP file transfers over SSH connection.
Inheritance: Tamir.SharpSsh.SshTransferProtocolBase
示例#1
0
        public static Scp Clone(SshBase baseConnection)
        {
            var clone = new Scp(baseConnection.Host, baseConnection.Username, baseConnection.Password);

            clone.Session = baseConnection.Session;

            return(clone);
        }
示例#2
0
        protected void PutFile(string host, NodeAuth auth, string local, string remote)
        {
            Scp scp = new Scp (host, auth.UserName);

            SetupSSH (scp, auth);

            scp.Put (local, remote);
            scp.Close ();
        }
示例#3
0
        private void CopyScript(string host, NodeAuth auth)
        {
            Scp scp = new Scp (host, auth.UserName);

            SetupSSH (scp, auth);

            scp.Put (LocalScriptPath, RemoteScriptPath);
            scp.Close ();
        }
示例#4
0
        private void ScpCopy(ResourceNode node, string remotePath, string localPath)
        {
            var scp = new Ssh.Scp(node.NodeAddress, node.Credentials.Username, node.Credentials.Password);

            scp.Connect();
            scp.Recursive = true;
            scp.Put(localPath, remotePath);
            scp.Close();
        }
示例#5
0
        protected void PutDirectory(string host, NodeAuth auth, string local, string remote)
        {
            Scp scp = new Scp (host, auth.UserName);

            SetupSSH (scp, auth);

            scp.To (local, remote, true);
            scp.Close ();
        }
示例#6
0
        public void SFtpDownload()
        {
            string remoteFilename = string.Format("{0}/{1}", _remoteDirectory, _remoteFilename);

            //Create a new SCP instance
            Scp scp = new Scp();

            //Copy a file from remote SSH server to local machine
            scp.From(_remoteHost, remoteFilename, _user, _password, _localFilename);
        }
		public static void RunExample()
		{		
			try
			{
				SshConnectionInfo input = Util.GetInput();
				string proto = GetProtocol();
				SshTransferProtocolBase sshCp;

				if(proto.Equals("scp"))
					sshCp = new Scp(input.Host, input.User);
				else
					sshCp = new Sftp(input.Host, input.User);

				if(input.Pass != null) sshCp.Password = input.Pass;
				if(input.IdentityFile != null) sshCp.AddIdentityFile( input.IdentityFile );
				sshCp.OnTransferStart += new FileTransferEvent(sshCp_OnTransferStart);
				sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
				sshCp.OnTransferEnd += new FileTransferEvent(sshCp_OnTransferEnd);

				Console.Write("Connecting...");
				sshCp.Connect();
				Console.WriteLine("OK");

				while(true)
				{
					string direction = GetTransferDirection();
					if(direction.Equals("to"))
					{
						string lfile = GetArg("Enter local file ['Enter to cancel']");
						if(lfile=="") break;
						string rfile = GetArg("Enter remote file ['Enter to cancel']");
						if(rfile=="") break;
						sshCp.Put(lfile, rfile);
					}
					else
					{
						string rfile = GetArg("Enter remote file ['Enter to cancel']");
						if(rfile=="") break;
						string lpath = GetArg("Enter local path ['Enter to cancel']");
						if(lpath=="") break;
						sshCp.Get(rfile, lpath);
					}
				}

				Console.Write("Disconnecting...");
				sshCp.Close();
				Console.WriteLine("OK");
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message);
			}
		}
示例#8
0
 public static bool UploadScp(string server, string localFile, string remoteFile, string username, string password)
 {
     try {
     SshTransferProtocolBase sshCp = new Scp(server, username, password);
     sshCp.Connect();
     sshCp.Put(localFile, remoteFile);
     sshCp.Close();
     return true;
       } catch (Exception e) {
     nlogger.ErrorException("while uploading this happened", e);
     return false;
       }
 }
示例#9
0
文件: Scp.cs 项目: swzry/tamir.ssh
        /// <summary>
        /// Opens a <see cref="ScpStream"/> to be used for writing a file to a remote target
        /// </summary>
        /// <param name="host">The name of the remote target</param>
        /// <param name="user">The user to use on the remote target</param>
        /// <param name="password">The user's password</param>
        /// <param name="filePath">The full path, including the filename, on the remote target to write into</param>
        /// <param name="filesize">The size, in bytes, of the file being written</param>
        /// <returns>A <see cref="ScpStream"/> that can be used to write a file</returns>
        public static ScpStream OpenWriteStream(Scp scp, string filePath, long filesize)
        {
            Channel channel = null;
            Stream  server  = null;

            scp.Connect();
            scp.SCP_ConnectTo(out channel, out server, filePath, false);

            // send "C0644 filesize filename", where filename should not include '/'
            string command = "C0644 " + filesize + " " + Path.GetFileName(filePath) + "\n";

            byte[] buff = Util.getBytes(command);
            server.Write(buff, 0, buff.Length); server.Flush();

            return(new ScpStream(channel, server, true));
        }
示例#10
0
        private static void ScpPut(string host, string from, string to)
        {
            var scp = new SSH.Scp(host.Split(':')[0], User, Password);

            if (host.Contains(":"))
            {
                scp.Connect(Int32.Parse(host.Split(':')[1]));
            }
            else
            {
                scp.Connect();
            }

            scp.Recursive = true;
            scp.Put(from, to);
            scp.Close();
        }
        public static void TransferFileToMachine(string localFilePath, string remoteFilePath)
        {
            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.To(localFilePath, remoteFilePath);

                scp.Close();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static void Upload(string filename, string path)
        {
            if (Convert.ToBoolean(ConfigurationManager.AppSettings.Get("EnableSCP")))
            {
                string host = ConfigurationManager.AppSettings.Get("SCPDirectory");
                string user = ConfigurationManager.AppSettings.Get("SCPLogin");
                string password = ConfigurationManager.AppSettings.Get("SCPPassword");
                string newDirectory = "/home/honeybadgers/public_html/Music Files/" + Globals.UserID + "/";

                //Create a new SCP instance
                Scp scp = new Scp(host, user, password);

                scp.Connect();
                scp.Mkdir(newDirectory);

                //Copy a file from local machine to remote SSH server
                scp.To(path, newDirectory + filename);
            }
        }
示例#13
0
        public void TransferFile(SshConnectionInfo input)
        {
            try
            {
                string proto = "scp";
                SshTransferProtocolBase sshCp;

                if (proto.Equals("scp"))
                    sshCp = new Scp(input.Host, input.User);
                else
                    sshCp = new Sftp(input.Host, input.User);

                if (input.Pass != null) sshCp.Password = input.Pass;
                //if (input.IdentityFile != null) sshCp.AddIdentityFile(input.IdentityFile);
                sshCp.OnTransferStart += new FileTransferEvent(sshCp_OnTransferStart);
                sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
                sshCp.OnTransferEnd += new FileTransferEvent(sshCp_OnTransferEnd);

                Console.Write("Connecting...");
                sshCp.Connect();
                Console.WriteLine("OK");

                if (input.Direction == TransferDirection.To)
                    sshCp.Put(input.LocalFilePath, input.RemoteFilePath);
                else
                    sshCp.Get(input.RemoteFilePath, input.LocalFilePath);


                Console.Write("Disconnecting...");
                sshCp.Close();
                Console.WriteLine("OK");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
示例#14
0
文件: Scp.cs 项目: swzry/tamir.ssh
        /// <summary>
        /// Opens a <see cref="ScpStream"/> to be used for reading a file from a remote target
        /// </summary>
        /// <param name="host">The name of the remote target</param>
        /// <param name="user">The user to use on the remote target</param>
        /// <param name="password">The user's password</param>
        /// <param name="filePath">The full path, including the filename, on the remote target to read from</param>
        /// <returns>A <see cref="ScpStream"/> that can be used to read a file</returns>
        public static ScpStream OpenReadStream(Scp scp, string filePath)
        {
            Channel channel  = null;
            Stream  server   = null;
            String  filename = null;

            scp.m_cancelled = false;
            int    filesize = 0;
            string cmd      = null;

            scp.Connect();
            scp.SCP_ConnectFrom(out channel, out server, filePath, false);
            MemoryStream stream = new MemoryStream();

            try
            {
                //scp.SCP_ConnectFrom(out channel, out server, filePath, false);

                byte[] buf = new byte[1024];

                // send '\0'
                scp.SCP_SendAck(server);
                int c = scp.SCP_CheckAck(server);

                //parse scp commands
                while ((c == 'D') || (c == 'C') || (c == 'E'))
                {
                    if (scp.m_cancelled)
                    {
                        break;
                    }

                    cmd = "" + (char)c;
                    if (c == 'E')
                    {
                        c = scp.SCP_CheckAck(server);
                        //dir = Path.GetDirectoryName(dir);
                        if (scp.Verbos)
                        {
                            Console.WriteLine("E");
                        }
                        //send '\0'
                        scp.SCP_SendAck(server);
                        c = (char)scp.SCP_CheckAck(server);
                        continue;
                    }

                    // read '0644 ' or '0755 '
                    server.Read(buf, 0, 5);
                    for (int i = 0; i < 5; i++)
                    {
                        cmd += (char)buf[i];
                    }

                    //reading file size
                    filesize = 0;
                    while (true)
                    {
                        server.Read(buf, 0, 1);
                        if (buf[0] == ' ')
                        {
                            break;
                        }
                        filesize = filesize * 10 + (buf[0] - '0');
                    }

                    //reading file name
                    for (int i = 0; ; i++)
                    {
                        server.Read(buf, i, 1);
                        if (buf[i] == (byte)0x0a)
                        {
                            filename = Util.getString(buf, 0, i);
                            break;
                        }
                    }
                    cmd += " " + filesize + " " + filename;
                    // send '\0'
                    scp.SCP_SendAck(server);

                    //Receive file
                    if (c == 'C')
                    {
                        if (scp.Verbos)
                        {
                            Console.WriteLine("Sending file modes: " + cmd);
                        }

                        scp.SCP_ReceiveFile(server, stream, filename, null, filesize);
                        stream.Seek(0, 0);

                        break;
                    }

                    c = scp.SCP_CheckAck(server);
                }
            }
            catch (Exception e)
            {
                if (scp.Verbos)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
                try
                {
                    channel.disconnect();
                }
                catch { }
                throw e;
            }


            return(new ScpStream(channel, stream, false));
        }
示例#15
0
        public object Run(TaskRunContext task)
        {
            lock (_gridLock)
            {
                RefreshCertificate();
                //var incarnation = task.Incarnation;

                string tmpFileName = null;
                if (task.UserCert != null)
                {
                    Log.Info("Using user's certificate");
                    tmpFileName = Path.GetTempFileName();
                    IOProxy.Storage.Download(task.UserCert, tmpFileName);

                    var scpForCert = new SSH.Scp(HELPER_SSH_HOST, HELPER_SSH_USER, HELPER_SSH_PASS);
                    scpForCert.Connect();
                    scpForCert.Recursive = true;
                    scpForCert.Put(tmpFileName, "/tmp/x509up_u500");
                    scpForCert.Close();

                    File.Delete(tmpFileName);
                    SshExec(PilotCommands.SetPermissionsOnProxyCertFile);
                }
                else
                {
                    Log.Info("Using system's certificate");
                }

                try
                {
                    long coresToUse = task.NodesConfig.Sum(cfg => cfg.Cores);
                    var  node       = GetNode(task);
                    var  pack       = node.PackageByName(task.PackageName);

                    // todo : remove
                    string commandLine = task.CommandLine;
                    commandLine = commandLine.Replace("java -jar ", "");
                    if (task.PackageName.ToLowerInvariant() == "cnm")
                    {
                        commandLine = commandLine.Replace("{0}", "ru.ifmo.hpc.main.ExtendedModel");
                    }
                    else
                    if (task.PackageName.ToLowerInvariant() == "ism")
                    {
                        commandLine = commandLine.Replace("{0}", "ru.ifmo.hpc.main.SpreadModel");
                    }
                    else
                    {
                        //if (task.PackageName.ToLowerInvariant() == "orca")
                        commandLine = commandLine.Replace("{0}", "");
                    }


                    string ftpFolderFromSystem   = IncarnationParams.IncarnatePath(node.DataFolders.ExchangeUrlFromSystem, task.TaskId, CopyPhase.In);
                    string ftpFolderFromResource = IncarnationParams.IncarnatePath(node.DataFolders.ExchangeUrlFromResource, task.TaskId, CopyPhase.In);

                    string gridFtpFolder = IncarnationParams.IncarnatePath(node.DataFolders.LocalFolder, task.TaskId, CopyPhase.None);
                    SshExec(PilotCommands.MakeFolderOnGridFtp, gridFtpFolder);

                    string endl = "\n";

                    // Сначала дописываем недостающий входной файл (скрипт запуска пакета на кластере)

                    string scriptName = pack.AppPath;

                    //if (pack.EnvVars.Any())
                    {
                        // Файл с установкой переменных окружения, если пакет их использует

                        scriptName = "run.sh";
                        var scriptContent = new StringBuilder();
                        scriptContent.Append("#!/bin/bash" + endl);
                        foreach (var pair in pack.EnvVars)
                        {
                            scriptContent.AppendFormat("export {0}={1}" + endl, pair.Key, pair.Value);
                        }

                        scriptContent.Append(pack.AppPath);

                        /*
                         * if (task.PackageName.ToLowerInvariant() == "orca")
                         * {
                         *  string[] args = commandLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                         *  for (int i = 0; i < args.Length; i++)
                         *  {
                         *      if (args[i] == "orca.out")
                         *          scriptContent.Append(" >");
                         *
                         *      scriptContent.Append(" $" + (i + 1).ToString());
                         *  }
                         * }
                         * else*/
                        {
                            scriptContent.Append(" " + commandLine);
                        }

                        string scriptLocalPath = Path.GetTempFileName();
                        File.WriteAllText(scriptLocalPath, scriptContent.ToString());
                        IOProxy.Ftp.UploadLocalFile(scriptLocalPath, ftpFolderFromSystem, scriptName);

                        File.Delete(scriptLocalPath);
                    }

                    //IOProxy.Ftp.UploadLocalFile(DEFAULT_JOB_LAUNCHER_PATH, GetFtpInputFolder(taskId), Path.GetFileName(DEFAULT_JOB_LAUNCHER_PATH));

                    // Копируем входные файлы с ФТП на ГридФТП

                    SshExec(PilotCommands.CopyFilesToGridFtp, ftpFolderFromResource + " " + gridFtpFolder);
                    SshExec(PilotCommands.MakeFilesExecutableOnGridFtp, gridFtpFolder + "*");

                    // Формируем описание задания для грида

                    var jobFileContent = new StringBuilder();

                    jobFileContent.AppendFormat(@"{{ ""version"": 2, ""description"": ""{0}""," + endl, task.TaskId);
                    jobFileContent.AppendFormat(@"  ""default_storage_base"": ""{0}""," + endl, gridFtpFolder);
                    jobFileContent.AppendFormat(@"  ""tasks"": [ {{ ""id"": ""a"", ""description"": ""task"", ""definition"": {{ ""version"": 2," + endl);
                    jobFileContent.AppendFormat(@"      ""executable"": ""{0}""," + endl, scriptName);
                    //jobFileContent.AppendFormat(@"      ""arguments"": [ ""{0}"" ]," + endl, String.Join(@""", """, args));

                    jobFileContent.AppendFormat(@"      ""input_files"": {{" + endl);
                    if (scriptName == "run.sh") // todo : if no input files?
                    {
                        jobFileContent.AppendFormat(@"          ""run.sh"": ""run.sh""," + endl);
                    }
                    jobFileContent.AppendFormat(@"          " + String.Join(
                                                    "," + endl + "          ",
                                                    task.InputFiles.Select(
                                                        file => String.Format(@"""{0}"": ""{0}""", file.FileName)
                                                        )
                                                    ));
                    jobFileContent.AppendFormat(endl + @"      }}," + endl);

                    jobFileContent.AppendFormat(@"      ""output_files"": {{" + endl);

                    //if (task.PackageName.ToLowerInvariant() == "cnm")
                    //    jobFileContent.AppendFormat(@"          ""output.dat"": ""output.dat""" + endl);
                    //else
                    if (task.PackageName.ToLowerInvariant() == "ism")
                    {
                        jobFileContent.AppendFormat(@"          ""output.dat"": ""output.dat""" + endl);
                    }
                    else
                    if (task.PackageName.ToLowerInvariant() == "orca")
                    {
                        jobFileContent.AppendFormat(@"          ""orca.out"":    ""orca.out""," + endl);
                        jobFileContent.AppendFormat(@"          ""eldens.cube"": ""eldens.cube""" + endl);
                    }
                    else
                    {
                        jobFileContent.AppendFormat(@"          " + String.Join(
                                                        "," + endl + "          ",
                                                        task.ExpectedOutputFileNames
                                                        .Where(name => name != "std.out" && name != "std.err")
                                                        .Select(
                                                            name => String.Format(@"""{0}"": ""{0}""", name)
                                                            )
                                                        ) + endl);
                    }

                    jobFileContent.AppendFormat(@"      }}," + endl);

                    jobFileContent.AppendFormat(@"      ""stdout"": ""std.out"", ""stderr"": ""std.err"", " + endl);
                    jobFileContent.AppendFormat(@"      ""count"": {0}" + endl, coresToUse);

                    if (pack.Params.ContainsKey("requirements"))
                    {
                        jobFileContent.AppendFormat(@"      ,""requirements"": {0}" + endl, pack.Params["requirements"]);
                    }

                    jobFileContent.AppendFormat(@"  }} }} ]," + endl);

                    jobFileContent.AppendFormat(@"  ""requirements"": {{ ""hostname"": [""{0}""]", node.NodeAddress);

                    //if (pack.Params.ContainsKey("requirements"))
                    //    jobFileContent.AppendFormat(@", {0}" + endl, pack.Params["requirements"]);

                    jobFileContent.AppendFormat(@"}}" + endl + "}}", node.NodeAddress);

                    Log.Debug(String.Format("Task's '{0}' grid job JSON: ", task.TaskId, jobFileContent));

                    string jobFileName         = "job_" + task.TaskId.ToString() + ".js";
                    string jobFilePathOnHelper = JOBS_FOLDER_ON_HELPER + jobFileName;

                    //string jobFileContent = File.ReadAllText(DEFAULT_JOB_DESCR_PATH).Replace(GRIDFTP_PATH_TOKEN, taskFolderOnGridFtp);
                    string jobFilePathLocal = Path.GetTempFileName();
                    File.WriteAllText(jobFilePathLocal, jobFileContent.ToString());

                    // Записываем его на сервер с Пилотом

                    var scp = new SSH.Scp(HELPER_SSH_HOST, HELPER_SSH_USER, HELPER_SSH_PASS);

                    /*
                     * var notifier = new JobDescriptionUploadNotifier(TaskId, Cluster, RunParams);
                     * scp.OnTransferEnd += new SSH.FileTransferEvent(notifier.OnFinish); // todo : необязательно
                     */

                    scp.Connect();
                    scp.Recursive = true;
                    scp.Put(jobFilePathLocal, jobFilePathOnHelper);
                    scp.Close();

                    File.Delete(jobFilePathLocal); // todo : remove files on helper and gridftp

                    // Запускаем

                    Log.Info(String.Format(
                                 "Trying to exec task {0} on grid cluster {1}",
                                 task.TaskId, node.NodeName
                                 ));

                    string launchResult = SshExec(PilotCommands.SubmitJob, jobFilePathOnHelper, pilotUrl: node.Services.ExecutionUrl);
                    int    urlPos       = launchResult.IndexOf("https://");
                    string jobUrl       = launchResult.Substring(urlPos).Trim() + "a";
                    Log.Debug(jobUrl);

                    Log.Info(String.Format(
                                 "Task {0} launched on grid with jobUrl = {1}",
                                 task.TaskId, jobUrl
                                 ));

                    return(jobUrl);
                }
                catch (Exception e)
                {
                    Log.Error(String.Format(
                                  "Error while starting task {0} in grid: {1}\n{2}",
                                  task.TaskId, e.Message, e.StackTrace
                                  ));

                    throw;
                }
                finally
                {
                    if (task.UserCert != null)
                    {
                        Log.Info("Wiping user's certificate");
                        tmpFileName = Path.GetTempFileName();
                        File.WriteAllText(tmpFileName, "Wiped by Easis system");

                        var scpForCert = new SSH.Scp(HELPER_SSH_HOST, HELPER_SSH_USER, HELPER_SSH_PASS);
                        scpForCert.Connect();
                        scpForCert.Recursive = true;
                        scpForCert.Put(tmpFileName, "/tmp/x509up_u500");
                        scpForCert.Close();

                        File.Delete(tmpFileName);
                        SshExec(PilotCommands.SetPermissionsOnProxyCertFile);
                    }
                }
            }
        }
示例#16
0
        public object Run(TaskRunContext task)
        {
            lock (_gridLock)
            {
                RefreshCertificate();
                //var incarnation = task.Incarnation;

                string tmpFileName = null;
                if (task.UserCert != null)
                {
                    Log.Info("Using user's certificate");
                    tmpFileName = Path.GetTempFileName();
                    IOProxy.Storage.Download(task.UserCert, tmpFileName);

                    var scpForCert = new SSH.Scp(HELPER_SSH_HOST, HELPER_SSH_USER, HELPER_SSH_PASS);
                    scpForCert.Connect();
                    scpForCert.Recursive = true;
                    scpForCert.Put(tmpFileName, "/tmp/x509up_u500");
                    scpForCert.Close();

                    File.Delete(tmpFileName);
                    SshExec(PilotCommands.SetPermissionsOnProxyCertFile);
                }
                else
                {
                    Log.Info("Using system's certificate");
                }

                try
                {
                    long coresToUse = task.NodesConfig.Sum(cfg => cfg.Cores);
                    var node = GetNode(task);
                    var pack = node.PackageByName(task.PackageName);

                    // todo : remove
                    string commandLine = task.CommandLine;
                    commandLine = commandLine.Replace("java -jar ", "");
                    if (task.PackageName.ToLowerInvariant() == "cnm")
                        commandLine = commandLine.Replace("{0}", "ru.ifmo.hpc.main.ExtendedModel");
                    else
                    if (task.PackageName.ToLowerInvariant() == "ism")
                        commandLine = commandLine.Replace("{0}", "ru.ifmo.hpc.main.SpreadModel");
                    else
                        //if (task.PackageName.ToLowerInvariant() == "orca")
                        commandLine = commandLine.Replace("{0}", "");

                    string ftpFolderFromSystem = IncarnationParams.IncarnatePath(node.DataFolders.ExchangeUrlFromSystem, task.TaskId, CopyPhase.In);
                    string ftpFolderFromResource = IncarnationParams.IncarnatePath(node.DataFolders.ExchangeUrlFromResource, task.TaskId, CopyPhase.In);

                    string gridFtpFolder = IncarnationParams.IncarnatePath(node.DataFolders.LocalFolder, task.TaskId, CopyPhase.None);
                    SshExec(PilotCommands.MakeFolderOnGridFtp, gridFtpFolder);

                    string endl = "\n";

                    // Сначала дописываем недостающий входной файл (скрипт запуска пакета на кластере)

                    string scriptName = pack.AppPath;

                    //if (pack.EnvVars.Any())
                    {
                        // Файл с установкой переменных окружения, если пакет их использует

                        scriptName = "run.sh";
                        var scriptContent = new StringBuilder();
                        scriptContent.Append("#!/bin/bash" + endl);
                        foreach (var pair in pack.EnvVars)
                            scriptContent.AppendFormat("export {0}={1}" + endl, pair.Key, pair.Value);

                        scriptContent.Append(pack.AppPath);

                        /*
                        if (task.PackageName.ToLowerInvariant() == "orca")
                        {
                            string[] args = commandLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            for (int i = 0; i < args.Length; i++)
                            {
                                if (args[i] == "orca.out")
                                    scriptContent.Append(" >");

                                scriptContent.Append(" $" + (i + 1).ToString());
                            }
                        }
                        else*/
                        {
                            scriptContent.Append(" " + commandLine);
                        }

                        string scriptLocalPath = Path.GetTempFileName();
                        File.WriteAllText(scriptLocalPath, scriptContent.ToString());
                        IOProxy.Ftp.UploadLocalFile(scriptLocalPath, ftpFolderFromSystem, scriptName);

                        File.Delete(scriptLocalPath);
                    }

                    //IOProxy.Ftp.UploadLocalFile(DEFAULT_JOB_LAUNCHER_PATH, GetFtpInputFolder(taskId), Path.GetFileName(DEFAULT_JOB_LAUNCHER_PATH));

                    // Копируем входные файлы с ФТП на ГридФТП

                    SshExec(PilotCommands.CopyFilesToGridFtp, ftpFolderFromResource + " " + gridFtpFolder);
                    SshExec(PilotCommands.MakeFilesExecutableOnGridFtp, gridFtpFolder + "*");

                    // Формируем описание задания для грида

                    var jobFileContent = new StringBuilder();

                    jobFileContent.AppendFormat(@"{{ ""version"": 2, ""description"": ""{0}""," + endl, task.TaskId);
                    jobFileContent.AppendFormat(@"  ""default_storage_base"": ""{0}""," + endl, gridFtpFolder);
                    jobFileContent.AppendFormat(@"  ""tasks"": [ {{ ""id"": ""a"", ""description"": ""task"", ""definition"": {{ ""version"": 2," + endl);
                    jobFileContent.AppendFormat(@"      ""executable"": ""{0}""," + endl, scriptName);
                    //jobFileContent.AppendFormat(@"      ""arguments"": [ ""{0}"" ]," + endl, String.Join(@""", """, args));

                    jobFileContent.AppendFormat(@"      ""input_files"": {{" + endl);
                    if (scriptName == "run.sh") // todo : if no input files?
                        jobFileContent.AppendFormat(@"          ""run.sh"": ""run.sh""," + endl);
                    jobFileContent.AppendFormat(@"          " + String.Join(
                        "," + endl + "          ",
                        task.InputFiles.Select(
                            file => String.Format(@"""{0}"": ""{0}""", file.FileName)
                        )
                    ));
                    jobFileContent.AppendFormat(endl + @"      }}," + endl);

                    jobFileContent.AppendFormat(@"      ""output_files"": {{" + endl);

                    //if (task.PackageName.ToLowerInvariant() == "cnm")
                    //    jobFileContent.AppendFormat(@"          ""output.dat"": ""output.dat""" + endl);
                    //else
                    if (task.PackageName.ToLowerInvariant() == "ism")
                        jobFileContent.AppendFormat(@"          ""output.dat"": ""output.dat""" + endl);
                    else
                    if (task.PackageName.ToLowerInvariant() == "orca")
                    {
                        jobFileContent.AppendFormat(@"          ""orca.out"":    ""orca.out""," + endl);
                        jobFileContent.AppendFormat(@"          ""eldens.cube"": ""eldens.cube""" + endl);
                    }
                    else
                    {
                        jobFileContent.AppendFormat(@"          " + String.Join(
                            "," + endl + "          ",
                            task.ExpectedOutputFileNames
                                .Where(name => name != "std.out" && name != "std.err")
                                .Select(
                                    name => String.Format(@"""{0}"": ""{0}""", name)
                                )
                        ) + endl);
                    }

                    jobFileContent.AppendFormat(@"      }}," + endl);

                    jobFileContent.AppendFormat(@"      ""stdout"": ""std.out"", ""stderr"": ""std.err"", " + endl);
                    jobFileContent.AppendFormat(@"      ""count"": {0}" + endl, coresToUse);

                    if (pack.Params.ContainsKey("requirements"))
                        jobFileContent.AppendFormat(@"      ,""requirements"": {0}" + endl, pack.Params["requirements"]);

                    jobFileContent.AppendFormat(@"  }} }} ]," + endl);

                    jobFileContent.AppendFormat(@"  ""requirements"": {{ ""hostname"": [""{0}""]", node.NodeAddress);

                    //if (pack.Params.ContainsKey("requirements"))
                    //    jobFileContent.AppendFormat(@", {0}" + endl, pack.Params["requirements"]);

                    jobFileContent.AppendFormat(@"}}" + endl + "}}", node.NodeAddress);

                    Log.Debug(String.Format("Task's '{0}' grid job JSON: ", task.TaskId, jobFileContent));

                    string jobFileName = "job_" + task.TaskId.ToString() + ".js";
                    string jobFilePathOnHelper = JOBS_FOLDER_ON_HELPER + jobFileName;

                    //string jobFileContent = File.ReadAllText(DEFAULT_JOB_DESCR_PATH).Replace(GRIDFTP_PATH_TOKEN, taskFolderOnGridFtp);
                    string jobFilePathLocal = Path.GetTempFileName();
                    File.WriteAllText(jobFilePathLocal, jobFileContent.ToString());

                    // Записываем его на сервер с Пилотом

                    var scp = new SSH.Scp(HELPER_SSH_HOST, HELPER_SSH_USER, HELPER_SSH_PASS);

                    /*
                    var notifier = new JobDescriptionUploadNotifier(TaskId, Cluster, RunParams);
                    scp.OnTransferEnd += new SSH.FileTransferEvent(notifier.OnFinish); // todo : необязательно
                    */

                    scp.Connect();
                    scp.Recursive = true;
                    scp.Put(jobFilePathLocal, jobFilePathOnHelper);
                    scp.Close();

                    File.Delete(jobFilePathLocal); // todo : remove files on helper and gridftp

                    // Запускаем

                    Log.Info(String.Format(
                        "Trying to exec task {0} on grid cluster {1}",
                        task.TaskId, node.NodeName
                    ));

                    string launchResult = SshExec(PilotCommands.SubmitJob, jobFilePathOnHelper, pilotUrl: node.Services.ExecutionUrl);
                    int urlPos = launchResult.IndexOf("https://");
                    string jobUrl = launchResult.Substring(urlPos).Trim() + "a";
                    Log.Debug(jobUrl);

                    Log.Info(String.Format(
                        "Task {0} launched on grid with jobUrl = {1}",
                        task.TaskId, jobUrl
                    ));

                    return jobUrl;
                }
                catch (Exception e)
                {
                    Log.Error(String.Format(
                        "Error while starting task {0} in grid: {1}\n{2}",
                        task.TaskId, e.Message, e.StackTrace
                    ));

                    throw;
                }
                finally
                {
                    if (task.UserCert != null)
                    {
                        Log.Info("Wiping user's certificate");
                        tmpFileName = Path.GetTempFileName();
                        File.WriteAllText(tmpFileName, "Wiped by Easis system");

                        var scpForCert = new SSH.Scp(HELPER_SSH_HOST, HELPER_SSH_USER, HELPER_SSH_PASS);
                        scpForCert.Connect();
                        scpForCert.Recursive = true;
                        scpForCert.Put(tmpFileName, "/tmp/x509up_u500");
                        scpForCert.Close();

                        File.Delete(tmpFileName);
                        SshExec(PilotCommands.SetPermissionsOnProxyCertFile);
                    }
                }
            }
        }
        private void cmdOK_Click(object sender, EventArgs e)
        {
            String app = Path.Combine(solutionDirectory, (String) lstLaunchItems.SelectedItem);
            CloverleafEnvironment.RemoteServerHost = txtHostName.Text;
            this.Hide();

            String host = txtHostName.Text;
            String user = txtUsername.Text;
            String password = txtPassword.Text;

            String zipDirectory = CloverleafEnvironment.CloverleafAppDataPath;
            String zipFileName = host + "." + user + "." + DateTime.Now.Ticks.ToString() + ".zip";
            String zipPath = Path.Combine(zipDirectory, zipFileName);
            String scriptPath = Path.Combine(zipDirectory, DateTime.Now.Ticks.ToString() + ".sh");
            String remoteExecutable = Path.GetFileName(app);
            String remoteDirectory = Path.GetFileNameWithoutExtension(zipFileName);

            FastZip fz = new FastZip();

            fz.CreateEmptyDirectories = true;
            fz.RestoreAttributesOnExtract = true;
            fz.RestoreDateTimeOnExtract = true;
            fz.CreateZip(zipPath, Path.GetDirectoryName(app),
                    true, null);

            ProcessStartInfo xInfo = new ProcessStartInfo();
            xInfo.FileName = CloverleafEnvironment.XPath;
            xInfo.Arguments = "-ac -internalwm";
            Process xProcess = new Process();
            xProcess.StartInfo = xInfo;
            xProcess.Start();

            Scp scp = new Scp(host, user, password);
            scp.Connect();
            if (scp.Connected == false)
            {
                throw new SshTransferException("Couldn't connect to host with SCP.");
            }
            scp.Mkdir("/home/" + user + "/.cloverleaf");
            scp.Put(zipPath, "/home/" + user + "/.cloverleaf/" + zipFileName);
            File.Delete(zipPath);
            
            String ssh1ArgumentData = "#! /bin/bash" + "\n" +
                "export DISPLAY=" + cboLocalIPs.SelectedItem.ToString() + ":0.0" + "\n" +
                "cd /home/" + user + "/.cloverleaf" + "\n" +
                "mkdir " + remoteDirectory + "\n" +
                "cp " + zipFileName + " " + remoteDirectory + "\n" +
                "cd " + remoteDirectory + "\n" +
                "unzip " + zipFileName + " > /dev/null \n" +
                "mono " + remoteExecutable + "\n" +
                "cd /home/" + user + "/.cloverleaf" + "\n" +
                "rm " + zipFileName + "\n" +
                "rm -rf " + remoteDirectory + "\n" +
                "rm /home/" + user + "/.cloverleaf/" + Path.GetFileName(scriptPath);
            File.WriteAllText(scriptPath, ssh1ArgumentData);

            if (scp.Connected == false)
            {
                throw new SshTransferException("Couldn't connect to host with SCP.");
            }
            scp.Put(scriptPath, "/home/" + user + "/.cloverleaf/" + Path.GetFileName(scriptPath));

            String stdOut = "";
            String stdErr = "";

            SshExec ssh = new SshExec(host, user, password);
            ssh.Connect();
            ssh.RunCommand("/bin/bash /home/" + user + "/.cloverleaf/" + Path.GetFileName(scriptPath),
                    ref stdOut, ref stdErr);

            (new RemoteStdOutDisplay(stdOut, stdErr)).Show();
        }
        private void cmdOK_Click(object sender, EventArgs e)
        {
            String app = projectDirectory;
            CloverleafEnvironment.RemoteServerHost = txtHostName.Text;
            this.Hide();

            String host = txtHostName.Text;
            String user = txtUsername.Text;
            String password = txtPassword.Text;
			Int32 port = FirstOpenPort(host, 60000, 65000);

            String zipDirectory = CloverleafEnvironment.CloverleafAppDataPath;
            String zipFileName = "web." + host + "." + user + "." + DateTime.Now.Ticks.ToString() + ".zip";
            String zipPath = Path.Combine(zipDirectory, zipFileName);
            String scriptPath = Path.Combine(zipDirectory, "web." + host + "." + user + "." + DateTime.Now.Ticks.ToString() + ".sh");
            String closeScriptPath = Path.Combine(zipDirectory, "web." + host + "." + user + "." + DateTime.Now.Ticks.ToString() + ".close.sh");
            String pidPath = Path.GetFileNameWithoutExtension(zipFileName) + ".pid";
            String remoteDirectory = Path.GetFileNameWithoutExtension(zipFileName);

            FastZip fz = new FastZip();

            fz.CreateEmptyDirectories = true;
            fz.RestoreAttributesOnExtract = true;
            fz.RestoreDateTimeOnExtract = true;
            fz.CreateZip(zipPath, app,
                    true, null);

            Scp scp = new Scp(host, user, password);
            scp.Connect();
            if (scp.Connected == false)
            {
                throw new SshTransferException("Couldn't connect to host with SCP.");
            }
            scp.Mkdir("/home/" + user + "/.cloverleaf");
            scp.Put(zipPath, "/home/" + user + "/.cloverleaf/" + zipFileName);
            File.Delete(zipPath);

            String ssh1ArgumentData = "";
            String ssh2ArgumentData = "";

            if (optXSP.Checked == true)
            {
                ssh1ArgumentData = "#! /bin/bash" + "\n" +
                     "cd /home/" + user + "/.cloverleaf" + "\n" +
                     "mkdir " + remoteDirectory + "\n" +
                     "cp " + zipFileName + " " + remoteDirectory + "\n" +
                     "cd " + remoteDirectory + "\n" +
                     "unzip " + zipFileName + " > /dev/null \n" +
                     "xsp2 --nonstop --port " + port.ToString() + "& \n" +
                     "pgrep -l " + user + " -n mono > /home/" + user + "/.cloverleaf/" + pidPath;
                ssh2ArgumentData = "#! /bin/bash" + "\n" +
                     "cd /home/" + user + "/.cloverleaf" + "\n" +
                     "kill `cat " + pidPath + "`" + "\n" +
                     "rm -rf " + Path.GetFileNameWithoutExtension(pidPath) + "*";
            }
            File.WriteAllText(scriptPath, ssh1ArgumentData);
            File.WriteAllText(closeScriptPath, ssh2ArgumentData);

            if (scp.Connected == false)
            {
                throw new SshTransferException("Couldn't connect to host with SCP.");
            }
            scp.Put(scriptPath, "/home/" + user + "/.cloverleaf/" + Path.GetFileName(scriptPath));
            scp.Put(closeScriptPath, "/home/" + user + "/.cloverleaf/" + Path.GetFileName(closeScriptPath));

            String stdOut = "";
            String stdErr = "";

            SshExec ssh = new SshExec(host, user, password);
            ssh.Connect();
            ssh.RunCommand("/bin/bash /home/" + user + "/.cloverleaf/" + Path.GetFileName(scriptPath),
                    ref stdOut, ref stdErr);

            (new RemoteWebServerCloser(Path.GetFileName(closeScriptPath),
                host, user, password)).Show();

            ProcessStartInfo wwwProcInfo = new ProcessStartInfo();
            wwwProcInfo.FileName = "http://" + host + ":" + port.ToString();
            wwwProcInfo.UseShellExecute = true;
            Process wwwProc = new Process();
            wwwProc.StartInfo = wwwProcInfo;
            wwwProc.Start();
        }
示例#19
0
        private static void ScpPut(string host, string from, string to)
        {
            var scp = new SSH.Scp(host.Split(':')[0], User, Password);
            if (host.Contains(":"))
            {
                scp.Connect(Int32.Parse(host.Split(':')[1]));
            }
            else
            {
                scp.Connect();
            }

            scp.Recursive = true;
            scp.Put(from, to);
            scp.Close();
        }
示例#20
0
        public SecureCopier(ResourceNode node)
        {
            Log.Info("SCP: Establishing connection to node " + node.ResourceName + "." + node.NodeName);

            var nodeAddress = node.NodeAddress; // todo : OR ExecutionUrl????!!!!!
            var addrParts   = node.NodeAddress.Split(':');

            int port = DEFAULT_SCP_PORT;

            if (addrParts.Length == 2)
            {
                int.TryParse(addrParts[1], out port);
                nodeAddress = addrParts[0];
            }

            // if resource asks us for password interactively:
            var interactiveAuthMethod = new KeyboardInteractiveAuthenticationMethod(node.Credentials.Username);

            interactiveAuthMethod.AuthenticationPrompt += delegate(object sender, Renci.SshNet.Common.AuthenticationPromptEventArgs e)
            {
                foreach (var prompt in e.Prompts)
                {
                    Log.Debug("Interactive request by resource node " + node.NodeName + ": '" + prompt.Request + "'");

                    if (prompt.Request.ToLowerInvariant().Contains("password"))
                    {
                        Log.Debug("Responding by password");
                        prompt.Response = node.Credentials.Password;
                    }
                }
            };

            ConnectionInfo connectionInfo;

            if (!String.IsNullOrWhiteSpace(node.Credentials.CertFile))
            {
                connectionInfo = new ConnectionInfo(nodeAddress, port, node.Credentials.Username,
                                                    new PrivateKeyAuthenticationMethod(
                                                        node.Credentials.Username,
                                                        new PrivateKeyFile(node.Credentials.CertFile, node.Credentials.Password)
                                                        )
                                                    );
            }
            else
            {
                connectionInfo = new ConnectionInfo(nodeAddress, port, node.Credentials.Username,
                                                    new PasswordAuthenticationMethod(node.Credentials.Username, node.Credentials.Password),
                                                    interactiveAuthMethod
                                                    );
            }

            try
            {
                _sftpClient = new SftpClient(connectionInfo);
                _sftpClient.Connect();

                _scp = null;
            }
            catch (Exception e)
            {
                Log.Warn("Unable to use sftp. Rolling bask to SCP for resource node " + node.ResourceName + "." + node.NodeName + ": " + e.ToString());
                _sftpClient = null;

                _scp = new Scp(nodeAddress, node.Credentials.Username, node.Credentials.Password);
                _scp.Connect(port);
            }
        }
示例#21
0
文件: Scp.cs 项目: akrisiun/SharpSSH
        public static Scp Clone(SshBase baseConnection)
        {
            var clone = new Scp(baseConnection.Host, baseConnection.Username, baseConnection.Password);
            clone.Session = baseConnection.Session;

            return clone;
        }
示例#22
0
        private void ApplyButton_Click(object sender, EventArgs e)
        {
            if (DefaultIP.Text == "" ||
                DefaultGW.Text == "" ||
                DefaultSN.Text == "" ||
                NewIP.Text == "" ||
                NewGW.Text == "" ||
                NewSN.Text == ""
                )
            {
                MessageBox.Show("All variables must have a value","Error");
                return;
            }

            host = DefaultIP.Text;

            string fileName = string.Empty;
            fileName = Path.GetTempFileName();
            FileInfo fileInfo = new FileInfo(fileName);
            fileInfo.Attributes = FileAttributes.Temporary;

            string fullPath = Path.GetTempFileName();

            StreamWriter streamWriter = File.AppendText(fullPath);
            streamWriter.WriteLine(Properties.Resources.rc);
            streamWriter.Flush();
            streamWriter.Close();

            //MessageBox.Show(fullPath);

            string local = fullPath, remote = "disk/etc/rc";

            ReplaceInFile(local, "10.0.1.230", DefaultIP.Text);
            ReplaceInFile(local, "255.255.255.0", DefaultSN.Text);
            ReplaceInFile(local, "10.0.1.1", DefaultGW.Text);

            ReplaceInFile(local, DefaultIP.Text, NewIP.Text);
            ReplaceInFile(local, DefaultSN.Text, NewSN.Text);
            ReplaceInFile(local, DefaultGW.Text, NewGW.Text);
            try
            {
                Scp scp = new Scp();
                scp.To(local, host, remote, user, pass);
            }
            catch
            {
                string error = "Cannot connect to controller at " + DefaultIP.Text;
                MessageBox.Show(error, "Error");
                return;
            }
            if (File.Exists(fullPath))
            {
                File.Delete(fullPath);
            }

            MessageBox.Show("You must reboot the controller for IP changes to take effect.", "Important");
        }