zoukankan      html  css  js  c++  java
  • c# 连接操作linux

    0.背景

    现在linux重要性是显然易见的,学习linux是必须,通过程序来来控制linux 也能发挥很大的作用。比如我们可以做一个自动化部署的程序,来发布程序到linux上面。

    1.在项目中添加SSH.NET 

    2.如何使用SSH.NET 直接上来代码

     private static string host = "xxx.xxx.xxx.xx";
            private static string username = "root";
            private static string password = "pwd";
    
            // Setup Credentials and Server Information
            public static ConnectionInfo ConnNfo = new ConnectionInfo(host, 22, username,
                 new AuthenticationMethod[]{
                    // Pasword based Authentication
                    new PasswordAuthenticationMethod(username,password),
                     // Key Based Authentication (using keys in OpenSSH Format)
                     //new PrivateKeyAuthenticationMethod("username",new PrivateKeyFile[]{
                     //    new PrivateKeyFile(@"..openssh.key","passphrase")
                     //}),
                 }
             );
    
            static void Main(string[] args)
            {
    
                ExecuteCommand1();
                ExecuteCommand2();
                Upload();
                Console.ReadLine();
            }
    
            /// <summary>
            /// 开始
            /// </summary>
            public void Start()
            {
                using (var client = new SshClient(host, username, password))
                {
                    try
                    {
                        string command = "ls";
                        client.Connect();
                        string result = client.RunCommand(command).Execute();
                        Console.WriteLine(result);
                        client.Disconnect();
                    }
                    catch (Exception e1)
                    {
                        Console.WriteLine(e1.Message);
                    }
                }
            }
    
            /// <summary>
            /// 执行 command
            /// </summary>
            public static void ExecuteCommand1()
            {
                using (var sshclient = new SshClient(ConnNfo))
                {
                    sshclient.Connect();
                    using (var cmd = sshclient.CreateCommand("cd /local && ls"))
                    {
                        string result = cmd.Execute();
                        Console.WriteLine("Command>" + cmd.CommandText);
                        Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
                        Console.WriteLine("result>" + result);
                    }
                    sshclient.Disconnect();
                }
            }
    
            /// <summary>
            /// 执行 command
            /// </summary>
            public static void ExecuteCommand2()
            {
                // Execute (SHELL) Commands
                using (var sshclient = new SshClient(ConnNfo))
                {
                    sshclient.Connect();
                    // quick way to use ist, but not best practice - SshCommand is not Disposed, ExitStatus not checked...
                    Console.WriteLine(sshclient.CreateCommand("cd /local && ls -lah").Execute());
                    Console.WriteLine(sshclient.CreateCommand("pwd").Execute());
                    Console.WriteLine(sshclient.CreateCommand("cd /local/soft && ls -lah").Execute());
                    sshclient.Disconnect();
                }
            }
    
            /// <summary>
            /// 上传
            /// </summary>
            public static void Upload()
            {
                using (var sftp = new SftpClient(ConnNfo))
                {
                    string uploadfn = "Renci.SshNet.dll";
    
                    sftp.Connect();
                    sftp.ChangeDirectory("/local/soft");
                    using (var uplfileStream = System.IO.File.OpenRead(uploadfn))
                    {
                        sftp.UploadFile(uplfileStream, uploadfn, true);
                    }
                    sftp.Disconnect();
                }
            }
  • 相关阅读:
    Python多线程Selenium跨浏览器测试
    Python Selenium设计模式-POM
    python webdriver安装
    webdriver介绍&与Selenium RC的比较
    sql server
    HTML5的一些新元素
    HTML5的一些新表单元素
    深入浅出:JavaScript作用域链
    简单的利用JS来判断页面是在手机端还是在PC端打开的方法
    浅谈移动端的自适应问题——响应式、rem/em、利用Js动态实现移动端自适应
  • 原文地址:https://www.cnblogs.com/nele/p/7350670.html
Copyright © 2011-2022 走看看