Java SSHJ 库和启用日志记录
Java SSHJ library and enabling logging
下面是我的 java 1.8 程序的示例部分。尝试进行身份验证时似乎失败了。它通过多种身份验证方法,然后声明其 运行 出来。
我想从 sshj 库中查看调试信息,以帮助我确定失败的原因:- 用户名、密钥交换或其他内容。我熟悉 log4j,我可以在我的代码中放置日志记录语句,但是我找不到一个示例(简单易懂)来说明如何将 log4j 连接到 sfl4j,然后告诉 sshj 使用记录器。
'''
SSHClient sshClient = new SSHClient();
try
{
String username = "testuser";
File privateKey = new File("/mykeys/keyname");
KeyProvider keys;
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
keys = sshClient.loadKeys(privateKey.getPath());
sshClient.connect("1.2.3.4", 22);
sshClient.authPublickey(username, keys);
SFTPClient sftpClient = sshClient.newSFTPClient();
sftpClient.put("./send/file1.xml", "file1.xml");
sshClient.close();
}
catch (UserAuthException e)
{
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
catch (TransportException e)
{
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
catch (IOException e)
{
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
'''
添加
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
到pom.xml
并创建 log4j.proerties 文件对我有用:
# Define the root logger with appender file
log = ssh-test.log
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
下面是我的 java 1.8 程序的示例部分。尝试进行身份验证时似乎失败了。它通过多种身份验证方法,然后声明其 运行 出来。 我想从 sshj 库中查看调试信息,以帮助我确定失败的原因:- 用户名、密钥交换或其他内容。我熟悉 log4j,我可以在我的代码中放置日志记录语句,但是我找不到一个示例(简单易懂)来说明如何将 log4j 连接到 sfl4j,然后告诉 sshj 使用记录器。
'''
SSHClient sshClient = new SSHClient();
try
{
String username = "testuser";
File privateKey = new File("/mykeys/keyname");
KeyProvider keys;
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
keys = sshClient.loadKeys(privateKey.getPath());
sshClient.connect("1.2.3.4", 22);
sshClient.authPublickey(username, keys);
SFTPClient sftpClient = sshClient.newSFTPClient();
sftpClient.put("./send/file1.xml", "file1.xml");
sshClient.close();
}
catch (UserAuthException e)
{
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
catch (TransportException e)
{
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
catch (IOException e)
{
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
'''
添加
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
到pom.xml
并创建 log4j.proerties 文件对我有用:
# Define the root logger with appender file
log = ssh-test.log
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n