protected void doOpen() throws Exception { super.doOpen(); Buffer buffer; if (agentForwarding) { log.info("Send agent forwarding request"); buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0); buffer.putInt(recipient); buffer.putString("*****@*****.**"); buffer.putBoolean(false); session.writePacket(buffer); } if (usePty) { log.info("Send SSH_MSG_CHANNEL_REQUEST pty-req"); buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0); buffer.putInt(recipient); buffer.putString("pty-req"); buffer.putBoolean(false); buffer.putString(ptyType); buffer.putInt(ptyColumns); buffer.putInt(ptyLines); buffer.putInt(ptyHeight); buffer.putInt(ptyWidth); Buffer modes = new Buffer(); for (PtyMode mode : ptyModes.keySet()) { modes.putByte((byte) mode.toInt()); modes.putInt(ptyModes.get(mode)); } modes.putByte((byte) 0); buffer.putBytes(modes.getCompactData()); session.writePacket(buffer); } if (!env.isEmpty()) { log.info("Send SSH_MSG_CHANNEL_REQUEST env"); for (Map.Entry<String, String> entry : env.entrySet()) { buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0); buffer.putInt(recipient); buffer.putString("env"); buffer.putBoolean(false); buffer.putString(entry.getKey()); buffer.putString(entry.getValue()); session.writePacket(buffer); } } log.info("Send SSH_MSG_CHANNEL_REQUEST shell"); buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0); buffer.putInt(recipient); buffer.putString("shell"); buffer.putBoolean(false); session.writePacket(buffer); }
public static void main(String[] args) throws GeneralSecurityException, JSchException, IOException { SimpleGeneratorHostKeyProvider p; if (args.length != 1) { System.err.println("Error: requires path to the SSH host key"); return; } else { File file = new File(args[0]); if (!file.exists() || !file.isFile() || !file.canRead()) { System.err.println("Error: ssh key should exist and be readable"); return; } } p = new SimpleGeneratorHostKeyProvider(); // Gerrit's SSH "simple" keys are always RSA. p.setPath(args[0]); p.setAlgorithm("RSA"); Iterable<KeyPair> keys = p.loadKeys(); // forces the key to generate. for (KeyPair k : keys) { System.out.println("Public Key (" + k.getPublic().getAlgorithm() + "):"); // From Gerrit's SshDaemon class; use JSch to get the public // key/type final Buffer buf = new Buffer(); buf.putRawPublicKey(k.getPublic()); final byte[] keyBin = buf.getCompactData(); HostKey pub = new HostKey("localhost", keyBin); System.out.println(pub.getType() + " " + pub.getKey()); System.out.println("Private Key:"); // Use Bouncy Castle to write the private key back in PEM format // (PKCS#1) // http://stackoverflow.com/questions/25129822/export-rsa-public-key-to-pem-string-using-java StringWriter privout = new StringWriter(); JcaPEMWriter privWriter = new JcaPEMWriter(privout); privWriter.writeObject(k.getPrivate()); privWriter.close(); System.out.println(privout); } }