Renci ssh.net - 连接时从表单创建控制台屏幕
Renci ssh.net - Create console screen from form when connected
我想在 renci ssh.net 连接后(或连接期间)创建(我认为它是一个控制台)屏幕来输入和查看输出。
我目前有一个收集连接信息的 form1,然后你按下一个按钮通过 backgroundworker1 进行连接。
我想我需要一个 form2 和 backgroundworker2 来保持屏幕实时,但我不确定。
我可以创建 form2,但不确定如何让输入和输出实时连接,到目前为止一切正常。
一般来说,你是如何创建一个输入/输出屏幕的。
如果我至少有一个起点,我想我可以从那里开始。
public string inputTo;
public string outputFrom;
public string Server { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public static SshClient client { get; set; }
public ShellStream shellStream { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Server = textBox1.Text;
Username = textBox2.Text;
Password = textBox3.Text;
client = new SshClient(Server, Username, Password);
client.Connect();
string reply = string.Empty;
shellStream = client.CreateShellStream("dumb", 80, 24, 800, 600, 1024);
reply = shellStream.Expect(new Regex(@":.*>#"), new TimeSpan(0, 0, 3));
richTextBox1.Text = "Connected please enter command\r\n#";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
ShellRunner();
}
}
private void button2_Click(object sender, EventArgs e)
{
client.Disconnect();
client.Dispose();
}
public void ShellRunner()
{
try
{
var reader = new StreamReader(shellStream);
int totalLines = richTextBox1.Lines.Length;
inputTo = richTextBox1.Lines[totalLines - 2];
int i = inputTo.LastIndexOf("#");
if (i != -1){ shellStream.WriteLine(inputTo.Substring(i + 1)); }
else { shellStream.WriteLine(inputTo); }
string result = shellStream.ReadLine(new TimeSpan(0, 0, 3));
outputFrom = reader.ReadToEnd();
richTextBox1.AppendText(outputFrom);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
我想在 renci ssh.net 连接后(或连接期间)创建(我认为它是一个控制台)屏幕来输入和查看输出。 我目前有一个收集连接信息的 form1,然后你按下一个按钮通过 backgroundworker1 进行连接。
我想我需要一个 form2 和 backgroundworker2 来保持屏幕实时,但我不确定。
我可以创建 form2,但不确定如何让输入和输出实时连接,到目前为止一切正常。
一般来说,你是如何创建一个输入/输出屏幕的。
如果我至少有一个起点,我想我可以从那里开始。
public string inputTo;
public string outputFrom;
public string Server { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public static SshClient client { get; set; }
public ShellStream shellStream { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Server = textBox1.Text;
Username = textBox2.Text;
Password = textBox3.Text;
client = new SshClient(Server, Username, Password);
client.Connect();
string reply = string.Empty;
shellStream = client.CreateShellStream("dumb", 80, 24, 800, 600, 1024);
reply = shellStream.Expect(new Regex(@":.*>#"), new TimeSpan(0, 0, 3));
richTextBox1.Text = "Connected please enter command\r\n#";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
ShellRunner();
}
}
private void button2_Click(object sender, EventArgs e)
{
client.Disconnect();
client.Dispose();
}
public void ShellRunner()
{
try
{
var reader = new StreamReader(shellStream);
int totalLines = richTextBox1.Lines.Length;
inputTo = richTextBox1.Lines[totalLines - 2];
int i = inputTo.LastIndexOf("#");
if (i != -1){ shellStream.WriteLine(inputTo.Substring(i + 1)); }
else { shellStream.WriteLine(inputTo); }
string result = shellStream.ReadLine(new TimeSpan(0, 0, 3));
outputFrom = reader.ReadToEnd();
richTextBox1.AppendText(outputFrom);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}