.pemファイルを使用してamazonインスタンスに接続するコードは次のとおりです。JSchライブラリを使用してec2接続の.pemファイルのコンテンツを文字列として使用する方法
import com.jcraft.jsch.*;
public class JConnectEC2shell{
public static void main(String[] arg){
try{
JSch jsch=new JSch();
String user = "ec2-user";
String host = "Enter Ip address of your instance";
int port = 22;
String privateKey = "D:\\privateKeyFile.pem";
jsch.addIdentity(privateKey);
System.out.println("identity added ");
Session session = jsch.getSession(user, host, port);
System.out.println("session created.");
// disabling StrictHostKeyChecking may help to make connection but makes it insecure
// see http://stackoverflow.com/questions/30178936/jsch-sftp-security-with-session-setconfigstricthostkeychecking-no
//
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel=session.openChannel("shell");
// Enable agent-forwarding.
//((ChannelShell)channel).setAgentForwarding(true);
channel.setInputStream(System.in);
/*
// a hack for MS-DOS prompt on Windows.
channel.setInputStream(new FilterInputStream(System.in){
public int read(byte[] b, int off, int len)throws IOException{
return in.read(b, off, (len>1024?1024:len));
}
});
*/
channel.setOutputStream(System.out);
/*
// Choose the pty-type "vt102".
((ChannelShell)channel).setPtyType("vt102");
*/
/*
// Set environment variable "LANG" as "ja_JP.eucJP".
((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP");
*/
//channel.connect();
channel.connect(3*1000);
}
catch(Exception e){
System.out.println(e);
}
}
}
私は、データベースから文字列としての.pemファイル(jsch.addIdentity(privateKey);
)で秘密鍵を設定します。今はファイル名です。これが可能なのかどうか、どんな助けにも感謝できるでしょう。そして、あなたが文字列にあなたのデータベースフィールドを読み取ることができ
addIdentity(String name, byte[]prvkey, byte[]pubkey, byte[] passphrase)
と:私はリンクから、このコードclick here
上記の方法で私に例を挙げてください。それはもっと感謝しています。 –
私が持っているのは.pemファイルです.pemファイルから秘密鍵、公開鍵、パスフレーズを分離できますか?どんな助けでも感謝します。 –
プライベートキーファイルから公開鍵を作成するには、これをチェックしてください: http://stackoverflow.com/questions/5244129/use-rsa-private-key-to-generate-public-key#5246045。作成後、パスフレーズと同じように、DBにコンテンツを配置することもできます。パスフレーズを定義していない場合は、空のままにしておくことができます( 'null')。 –