C# 使用c在远程ssh服务器上启动脚本#
我在Visual Studio上使用C#设计了一个GUI。我是C语言初学者,但擅长C++编程,但由于任务的需要,我在C语言中设计。在这个GUI中,我有一个连接到远程ssh服务器的按钮,作为试用,当用户按下按钮1时,我会运行以下命令C# 使用c在远程ssh服务器上启动脚本#,c#,ssh,C#,Ssh,我在Visual Studio上使用C#设计了一个GUI。我是C语言初学者,但擅长C++编程,但由于任务的需要,我在C语言中设计。在这个GUI中,我有一个连接到远程ssh服务器的按钮,作为试用,当用户按下按钮1时,我会运行以下命令 client.Connect(); var output = client.RunCommand("echo happy test"); var dltOutput = client.RunComm
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);
删除我的主文件夹中的“helloWorld.txt”的命令运行正常,但无法运行运行shell脚本“first.sh”的命令。我想附上下面的完整代码供您参考
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");
}
}
}