Click here to Skip to main content
15,885,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I am making a project whose purpose is to connect to remote machine, execute a command and show the output. I am using WPF project in C#. and below is my code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tamir.SharpSsh;
using Tamir.SharpSsh.jsch;
using Tamir.Streams;
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void image1_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
        }
        public void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            string strval = textBox1.Text;
        }
        public void button2_Click(object sender, RoutedEventArgs e)
        {
            string host = "192.168.1.9";
            string uname = "root";
            string passwd = "test";
            string commnd = "ls";
           SshStream ssh = new SshStream(host, uname, passwd);
            //Set the end of response matcher character
           //Tamir.SharpSsh.SshStream (host, uname, passwd);
            ssh.Prompt = "ls";
            //Remove terminal emulation characters
            ssh.RemoveTerminalEmulationCharacters = true;
            ssh.Write(commnd+textBox1.Text);
            string response = ssh.ReadResponse();
            Console.Write(response);
        }

        public void viewbox1_show(object sender, TextChangedEventArgs e)
        {
        }

    }
}


There are no build errors but when I click the button it hangs the program for a while and leads to error so such known hosts.
P.S: target machine is a Virtual machine
I am not a programmer...just doing it as hobby but this is driving me nuts so please someone help.

Regards,
Posted
Updated 22-Aug-10 11:37am
v2
Comments
Steve Maier 14-Sep-10 11:39am    
Are you still having this problem? Can you actually use a normal ssh client and connect to the machine? Can you ping the machine?

1 solution

In case u didn't get this fixed.. the PROMPT property is the value that is to be searched in the response from the ssh client. Normally you will find a [username@machine] $ as being the command prompt on a machine, so you must set the prompt value to $ in this case.. That way it knows when it finds that $ (prompt) the session is in a wait state for a command again. It seems in your example you are setting it to a ls command, and it will never find that in the response. Hope that helps...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900