private static void SampleShell(Reader reader) { byte[] b = new byte[1]; while (true) { int input = System.Console.Read(); b[0] = (byte)input; reader._pf.Transmit(b); } }
//Tutorial: Connecting to a host and opening a shell private static void ConnectAndOpenShell() { SSHConnectionParameter f = new SSHConnectionParameter(); f.EventTracer = new Tracer(); //to receive detailed events, set ISSHEventTracer Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); f.Protocol = SSHProtocol.SSH2; //this sample works on both SSH1 and SSH2 string host_ip = "172.22.1.15"; //<--!!! [TO USERS OF Granados] f.UserName = "******"; //<--!!! if you try this sample, edit these values for your environment! string password = "******"; s.Connect(new IPEndPoint(IPAddress.Parse(host_ip), 22)); //22 is the default SSH port //former algorithm is given priority in the algorithm negotiation f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA }; f.PreferableCipherAlgorithms = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES }; f.WindowSize = 0x1000; //this option is ignored with SSH1 Reader reader = new Reader(); //simple event receiver AuthenticationType at = AuthenticationType.PublicKey; f.AuthenticationType = at; if (at == AuthenticationType.KeyboardInteractive) { //Creating a new SSH connection over the underlying socket _conn = SSHConnection.Connect(f, reader, s); reader._conn = _conn; Debug.Assert(_conn.AuthenticationResult == AuthenticationResult.Prompt); AuthenticationResult r = ((SSH2Connection)_conn).DoKeyboardInteractiveAuth(new string[] { password }); Debug.Assert(r == AuthenticationResult.Success); } else { //NOTE: if you use public-key authentication, follow this sample instead of the line above: //f.AuthenticationType = AuthenticationType.PublicKey; f.IdentityFile = "C:\\P4\\tools\\keys\\aaa"; f.Password = password; f.KeyCheck = delegate(SSHConnectionInfo info) { byte[] h = info.HostKeyMD5FingerPrint(); foreach (byte b in h) Debug.Write(String.Format("{0:x2} ", b)); return true; }; //Creating a new SSH connection over the underlying socket _conn = SSHConnection.Connect(f, reader, s); reader._conn = _conn; } //Opening a shell SSHChannel ch = _conn.OpenShell(reader); reader._pf = ch; //you can get the detailed connection information in this way: SSHConnectionInfo ci = _conn.ConnectionInfo; //Go to sample shell SampleShell(reader); }
//Tutorial: port forwarding private static void ConnectSSH2AndPortforwarding() { SSHConnectionParameter f = new SSHConnectionParameter(); f.EventTracer = new Tracer(); //to receive detailed events, set ISSHEventTracer Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); f.Protocol = SSHProtocol.SSH1; //this sample works on both SSH1 and SSH2 string host_ip = "10.10.9.8"; //<--!!! [TO USERS OF Granados] f.UserName = "******"; //<--!!! if you try this sample, edit these values for your environment! f.Password = ""; //<--!!! s.Connect(new IPEndPoint(IPAddress.Parse(host_ip), 22)); //22 is the default SSH port f.Protocol = SSHProtocol.SSH2; f.AuthenticationType = AuthenticationType.Password; //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 Reader reader = new Reader(); //simple event receiver //Creating a new SSH connection over the underlying socket _conn = SSHConnection.Connect(f, reader, s); reader._conn = _conn; //Local->Remote port forwarding SSHChannel ch = _conn.ForwardPort(reader, "www.google.co.jp", 80, "localhost", 0); reader._pf = ch; while (!reader._ready) System.Threading.Thread.Sleep(100); //wait response reader._pf.Transmit(Encoding.ASCII.GetBytes("GET / HTTP/1.0\r\n\r\n")); //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(); }
private static void AgentForward() { SSHConnectionParameter f = new SSHConnectionParameter(); f.EventTracer = new Tracer(); //to receive detailed events, set ISSHEventTracer Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); f.Protocol = SSHProtocol.SSH2; //this sample works on both SSH1 and SSH2 string host_ip = "172.22.1.15"; //<--!!! [TO USERS OF Granados] f.UserName = "******"; //<--!!! if you try this sample, edit these values for your environment! string password = ""; s.Connect(new IPEndPoint(IPAddress.Parse(host_ip), 22)); //22 is the default SSH port //former algorithm is given priority in the algorithm negotiation f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA }; f.PreferableCipherAlgorithms = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES }; f.WindowSize = 0x1000; //this option is ignored with SSH1 f.AgentForward = new AgentForwardClient(); Reader reader = new Reader(); //simple event receiver AuthenticationType at = AuthenticationType.Password; f.AuthenticationType = at; f.Password = password; //Creating a new SSH connection over the underlying socket _conn = SSHConnection.Connect(f, reader, s); reader._conn = _conn; //Opening a shell SSHChannel ch = _conn.OpenShell(reader); reader._pf = ch; while (!reader._ready) Thread.Sleep(100); Thread.Sleep(1000); ch.Transmit(Encoding.Default.GetBytes("ssh -A -l okajima localhost\r")); //Go to sample shell SampleShell(reader); }
//Tutorial: Connecting to a host and opening a shell private static void ConnectAndOpenShell() { SSHConnectionParameter f = new SSHConnectionParameter("172.22.1.15", 22, SSHProtocol.SSH2, AuthenticationType.PublicKey, "okajima", "aaa"); f.EventTracer = new Tracer(); //to receive detailed events, set ISSHEventTracer //former algorithm is given priority in the algorithm negotiation f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA }; f.PreferableCipherAlgorithms = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES }; f.WindowSize = 0x1000; //this option is ignored with SSH1 Reader reader = new Reader(); //simple event receiver 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 if (f.AuthenticationType == AuthenticationType.KeyboardInteractive) { //Creating a new SSH connection over the underlying socket _conn = SSHConnection.Connect(f, reader, s); reader._conn = _conn; Debug.Assert(_conn.AuthenticationResult == AuthenticationResult.Prompt); AuthenticationResult r = ((SSH2Connection)_conn).DoKeyboardInteractiveAuth(new string[] { f.Password }); Debug.Assert(r == AuthenticationResult.Success); } else { //NOTE: if you use public-key authentication, follow this sample instead of the line above: //f.AuthenticationType = AuthenticationType.PublicKey; f.IdentityFile = "C:\\P4\\tools\\keys\\aaa"; f.VerifySSHHostKey = (info) => { byte[] h = info.HostKeyFingerPrint; foreach (byte b in h) Debug.Write(String.Format("{0:x2} ", b)); return true; }; //Creating a new SSH connection over the underlying socket _conn = SSHConnection.Connect(f, reader, s); reader._conn = _conn; } //Opening a shell SSHChannel ch = _conn.OpenShell(reader); reader._pf = ch; //you can get the detailed connection information in this way: //SSHConnectionInfo ci = _conn.ConnectionInfo; //Go to sample shell SampleShell(reader); }
private static void AgentForward() { SSHConnectionParameter f = new SSHConnectionParameter("172.22.1.15", 22, SSHProtocol.SSH2, AuthenticationType.Password, "root", ""); f.EventTracer = new Tracer(); //to receive detailed events, set ISSHEventTracer 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 //former algorithm is given priority in the algorithm negotiation f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA }; f.PreferableCipherAlgorithms = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES }; f.WindowSize = 0x1000; //this option is ignored with SSH1 f.AgentForward = new AgentForwardClient(); Reader reader = new Reader(); //simple event receiver //Creating a new SSH connection over the underlying socket _conn = SSHConnection.Connect(f, reader, s); reader._conn = _conn; //Opening a shell SSHChannel ch = _conn.OpenShell(reader); reader._pf = ch; while (!reader._ready) Thread.Sleep(100); Thread.Sleep(1000); ch.Transmit(Encoding.Default.GetBytes("ssh -A -l okajima localhost\r")); //Go to sample shell SampleShell(reader); }
//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(); }
private static void AgentForward() { SSHConnectionParameter f = new SSHConnectionParameter("172.22.1.15", 22, SSHProtocol.SSH2, AuthenticationType.Password, "root", ""); Tracer tracer = new Tracer(); 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 //former algorithm is given priority in the algorithm negotiation f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA }; f.PreferableCipherAlgorithms = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES }; f.WindowSize = 0x1000; //this option is ignored with SSH1 f.AgentForwardingAuthKeyProvider = new AgentForwardingAuthKeyProvider(); //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); //Opening a shell var ch = conn.OpenShell(channelOperator => new ChannelHandler(channelOperator)); while (!reader._ready) Thread.Sleep(100); Thread.Sleep(1000); byte[] data = Encoding.Default.GetBytes("ssh -A -l okajima localhost\r"); ch.Operator.Send(new DataFragment(data, 0, data.Length)); //Go to sample shell SampleShell(ch); }