c# 串連操作linux

來源:互聯網
上載者:User

標籤:tar   cat   操作   ase   pen   color   pos   start   setup   

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();            }        }

 

c# 串連操作linux

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.