2016-04-14 45 views
0

.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

答えて

0

Jschクラスはプライベートとバイト配列として公開鍵の両方を取るthis methodを提供を持っていますそれを渡す、例えば

// read db columns 
String privateKey = ... 
String publicKey = ... 
String passphrase = ... 

final JSch jsch = new JSch(); 
jsch.addIdentity("my key", privateKey.getBytes(), publicKey.getBytes(), passphrase.getBytes()); 
+0

上記の方法で私に例を挙げてください。それはもっと感謝しています。 –

+0

私が持っているのは.pemファイルです.pemファイルから秘密鍵、公開鍵、パスフレーズを分離できますか?どんな助けでも感謝します。 –

+0

プライベートキーファイルから公開鍵を作成するには、これをチェックしてください: http://stackoverflow.com/questions/5244129/use-rsa-private-key-to-generate-public-key#5246045。作成後、パスフレーズと同じように、DBにコンテンツを配置することもできます。パスフレーズを定義していない場合は、空のままにしておくことができます( 'null')。 –

0

"my key"としてpemファイル名を入れ、次のようにpemファイルの内容をbyte []として渡します。 jsch.addIdentity( "privateKeyFile.pem"、pemString.getBytes()、null、null);

メモ:Pemコンテンツの最初の行に "+ System.getProperty(" line.separator ")"を追加する必要がありました。他の行は行区切りを必要としませんが、最初の行が区切り文字で終わらない限り、それはエラーになります。例: "----- BEGIN RSAプライベートキー-----" + System.getProperty( "line.separator")

関連する問題