Launch a script on remote ssh server using c#

Viewed 938

I have a GUI designed on Visual Studio using C#. I am a beginner in C# but good at C++ programming but due to requirements of task, I am designing it in C#. In this GUI, i have a button that connects to remote ssh server and as a trial, i have following commands to run when user presses button1.

client.Connect();
                var output    = client.RunCommand("echo happy test");
                var dltOutput = client.RunCommand("rm /home/helloWorld.txt");
                var launchFirst = client.RunCommand("bash /root/first.sh");
                client.Disconnect();
                Console.WriteLine(output.Result);

The command to delete "helloWorld.txt" in my home folder runs fine but could not running the command to run the shell script "first.sh". I would like to attach complete code below for your reference.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Renci.SshNet;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        //Connection information
        string user = "root";
        string pass = "hello123ado";
        string host = "192.168.38.50";
        int port = 22;

        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Say Button1_Clicked");

            using (var client = new SshClient(host,user,pass))
            {
                client.Connect();
                var output    = client.RunCommand("echo happy test");
                var dltOutput = client.RunCommand("rm /home/helloWorld.txt");

                var launchFirst = client.RunCommand("bash /root/first.sh");
                client.Disconnect();
                Console.WriteLine(output.Result);
            }


        }

        private void Button2_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Say Button2_Clicked");

        }

        private void Button3_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Say Button3_Clicked");
        }
    }
}
0 Answers
Related