private static void SampleShell(ChannelHandler channelHandler) { byte[] b = new byte[1]; while (true) { int input = System.Console.Read(); b[0] = (byte)input; channelHandler.Operator.Send(new DataFragment(b, 0, b.Length)); } }
//Tutorial: port forwarding private static void ConnectSSH2AndPortforwarding() { SSHConnectionParameter f = new SSHConnectionParameter("10.10.9.8", 22, SSHProtocol.SSH2, AuthenticationType.Password, "root", ""); Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Connect(new IPEndPoint(IPAddress.Parse(f.HostName), f.PortNumber)); //22 is the default SSH port //NOTE: if you use public-key authentication, follow this sample instead of the line above: // f.AuthenticationType = AuthenticationType.PublicKey; // f.IdentityFile = "privatekey.bin"; // f.Password = "******"; //former algorithm is given priority in the algorithm negotiation f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.DSA }; f.PreferableCipherAlgorithms = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES }; f.WindowSize = 0x1000; //this option is ignored with SSH1 //Creating a new SSH connection over the underlying socket Reader reader = null; var conn = SSHConnection.Connect(s, f, c => { return(reader = new Reader(c)); }, c => new Tracer()); Debug.Assert(reader != null); //Local->Remote port forwarding ChannelHandler ch = conn.ForwardPort( channelOperator => new ChannelHandler(channelOperator), "www.google.co.jp", 80u, "localhost", 0u); while (!reader._ready) { System.Threading.Thread.Sleep(100); //wait response } byte[] data = Encoding.ASCII.GetBytes("GET / HTTP/1.0\r\n\r\n"); ch.Operator.Send(new DataFragment(data, 0, data.Length)); //get the toppage //Remote->Local // if you want to listen to a port on the SSH server, follow this line: //_conn.ListenForwardedPort("0.0.0.0", 10000); //NOTE: if you use SSH2, dynamic key exchange feature is supported. //((SSH2Connection)_conn).ReexchangeKeys(); }