我正在使用JSch进行sftp通信,现在我想使用方便基于密钥的身份验证,密钥由我的网络团队一次加载到客户端和服务器机器上,所有后来的通信都只是基于用户的,我们已经加载了密钥.
sftp -oPort=10022 jmark@192.18.0.246
Run Code Online (Sandbox Code Playgroud)
如 tjill@192.18.0.135
像这个命令工作正常并连接到sftp,我如何以编程方式实现此功能.
如果不可能使用JSch,请建议其他一些库.我遇到了Apache SSHD.
我正在运行一个java程序,我使用Java SFTP将文件从一个文件夹传输到另一个文件夹.我遇到的问题是我在Java SFTP中遇到以下错误(使用JSch):
C:\ Oracle\Middleware\Oracle_Home\oracle_common\jdk\bin\javaw.exe -server -classpath C:\ JDeveloper\mywork\Java_Hello_World.adf; C:\ JDeveloper\mywork\Java_Hello_World\Client\classes; C:\ Users\ADMIN\Downloads\jsch-0.1.53.jar -Djavax.net.ssl.trustStore = C:\ Users\IBM_AD~1\AppData\Local\Temp\trustStore5840796204189742395.jks FileTransfer com.jcraft.jsch.JSchException:UnknownHostKey: 127.0.0.1.RSA密钥指纹是a2:39:3f:44:88:e9:1f:d7:d1:71:f4:85:98:fb:90:dc at com.jcraft.jsch.Session.checkHost(Session.java: 797)at com.jcraft.jsch.Session.connect(Session.java:342)at com.jcraft.jsch.Session.connect(Session.java:183)at FileTransfer.main(FileTransfer.java:33)进程退出时退出代码0.
以下是我目前的代码:
FileTransfer fileTransfer = new FileTransfer();
JSch jsch = new JSch();
try {
String host = "127.0.0.1";
int port = 22;
String user = "user";
Session session = jsch.getSession(user, host, port);
session = jsch.getSession("username", "127.0.0.1", 22);
session.connect(); // bug here , java.net.ConnectException
ChannelSftp sftp = null;
sftp = (ChannelSftp)session.openChannel("sftp") ; //channel;
//extra config code …Run Code Online (Sandbox Code Playgroud) 我试图使用JSch库通过SSH协议执行多个命令.但我似乎陷入困境,无法找到任何解决方案.该setCommand()方法每个会话只能执行单个命令.但我想按顺序执行命令,就像Android平台上的connectbot应用程序一样.到目前为止我的代码是:
package com.example.ssh;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class ExampleSSH extends Activity {
/** Called when the activity is first created. */
EditText command;
TextView result;
Session session;
ByteArrayOutputStream baos;
ByteArrayInputStream bais;
Channel channel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bais = new ByteArrayInputStream(new byte[1000]);
command = (EditText) findViewById(R.id.editText1);
result = (TextView) …Run Code Online (Sandbox Code Playgroud) 我正在使用JSCH -SSH库在"shell"通道中执行命令,但无法找到办法做两件事: -
1)如何查找命令是否在远程unix盒上完全执行?
2)如何捕获String中的命令输出,而不是在System.out控制台上打印它?
下面是我的代码片段,可以在system.out上显示shell命令输出
注意:我不想使用"exec"通道,因为它为每个命令启动一个新进程,并且不记得导出的"会话"变量.我必须使用"shell"频道.
以下是我的代码段.感谢任何帮助.谢谢你的时间.
try{
String commandToRun = "ls /tmp/*.log \n";
if(channel.isClosed())
channel=session.openChannel("shell");
byte[] bytes = commandToRun.getBytes();
ByteArrayInputStream bais=new ByteArrayInputStream(bytes);
channel.setInputStream(bais);
InputStream ins=channel.getInputStream();
channel.connect();
channel.setOutputStream(System.out);//This prints on console. Need 2 capture in String somehow?
//in-efficient way to allow command to execute completely on remote Unix machine
//DO NOT know a better way, to know when command is executed completely
Thread.sleep(5000L);
}
catch(Exception e){
System.out.println("Exception in executeCommand() --->"+ e.getMessage());
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)