c# ssh连接操作linux 开启ros

c# 连接操作linux
作者:@nele
本文为作者原创,转载请注明出处:https://www.cnblogs.com/nele/p/7350670.html

目录
0.背景

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

1.在项目中添加SSH.NET

在这里插入图片描述

2.如何使用SSH.NET 直接上来代码
//调用ros命令务必 source /opt/ros/noetic/setup.bash && source /home/sz/catkin_ws2.0/install/setup.bash 等等工作空间

using System;
using Renci.SshNet;

namespace NetSSHOperation.Utils
{
class Program
{
private static string host = “10.0.0.9”;
private static string username = “sz”;
private static string password = “123”;

    // 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)
    {

        ExecuteCommand_robotRun();  //机器人启动
        //ExecuteCommand_slam();   //slam扫图开始
        //ExecuteCommand_killNodes(); //关闭所有节点

        //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 ExecuteCommand_robotRun()
    {
        using (var sshclient = new SshClient(ConnNfo))
        {
            sshclient.Connect();
            //&& chmod u+x test.sh &&  ./test.sh
            using (var cmd = sshclient.CreateCommand("source /opt/ros/noetic/setup.bash && source /home/sz/catkin_ws2.0/install/setup.bash && " +
                "source /home/sz/catkin_ws2.0/devel/setup.bash && source /home/sz/catkin_ws_cartographer/install_isolated/setup.bash && " +
                "export ROS_HOSTNAME=10.0.0.9 && export ROS_MASTER_URI=http://10.0.0.9:11311 &&"+
                "roslaunch main_robot robot_run.launch"))
            {
                string result = cmd.Execute();
                Console.WriteLine("Command>" + cmd.CommandText);
                Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
                Console.WriteLine("result>" + result);
            }
            sshclient.Disconnect();
        }
    }

    public static void ExecuteCommand_slam()
    {
        using (var sshclient = new SshClient(ConnNfo))
        {
            sshclient.Connect();
            //&& chmod u+x test.sh &&  ./test.sh
            using (var cmd = sshclient.CreateCommand("source /opt/ros/noetic/setup.bash && source /home/sz/catkin_ws2.0/install/setup.bash && " +
                "source /home/sz/catkin_ws2.0/devel/setup.bash && source /home/sz/catkin_ws_cartographer/install_isolated/setup.bash && " +
                "export ROS_HOSTNAME=10.0.0.9 && export ROS_MASTER_URI=http://10.0.0.9:11311 &&" +
                "roslaunch main_robot slam.launch"))
            {
                string result = cmd.Execute();
                Console.WriteLine("Command>" + cmd.CommandText);
                Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
                Console.WriteLine("result>" + result);
            }
            sshclient.Disconnect();
        }
    }

    public static void ExecuteCommand_killNodes()
    {
        using (var sshclient = new SshClient(ConnNfo))
        {
            sshclient.Connect();
            //&& chmod u+x test.sh &&  ./test.sh
            using (var cmd = sshclient.CreateCommand("source /opt/ros/noetic/setup.bash && source /home/sz/catkin_ws2.0/install/setup.bash && " +
                "source /home/sz/catkin_ws2.0/devel/setup.bash && source /home/sz/catkin_ws_cartographer/install_isolated/setup.bash && " +
                "export ROS_HOSTNAME=10.0.0.9 && export ROS_MASTER_URI=http://10.0.0.9:11311 &&" +
                "rosnode kill -a"))
            {
                string result = cmd.Execute();
                Console.WriteLine("Command>" + cmd.CommandText);
                Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
                Console.WriteLine("result>" + result);
            }
            sshclient.Disconnect();
        }
    }

    public static void ExecuteCommand_saveMap()
    {
        using (var sshclient = new SshClient(ConnNfo))
        {
            sshclient.Connect();
            //&& chmod u+x test.sh &&  ./test.sh
            //windows保存路径改为本地地址
            using (var cmd = sshclient.CreateCommand("source /opt/ros/noetic/setup.bash && source /home/sz/catkin_ws2.0/install/setup.bash && " +
                "source /home/sz/catkin_ws2.0/devel/setup.bash && source /home/sz/catkin_ws_cartographer/install_isolated/setup.bash && " +
                "rosservice call /finish_trajectory 0 && rosservice call /write_state \"filename: '${HOME}/Downloads/cartoMap.pbstream'\" &&" +
                "rosrun cartographer_ros cartographer_pbstream_to_ros_map -map_filestem=${HOME}/Downloads/cartoMap.pgm  -pbstream_filename=${HOME}/Downloads/cartoMap.pbstream -resolution=0.05 "))
            {
                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 /home && 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();
        }
    }
}

}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值