我是SSH和JSch的新手.当我从客户端连接到服务器时,我想做两个任务:
ChannelSFTP)目前我使用两个单独的shell登录来执行每个任务(实际上我还没有开始编程MySQL查询).
对于上传,相关代码是
session.connect();
Channel channel=session.openChannel("sftp");
channel.connect();
ChannelSftp c=(ChannelSftp)channel;
c.put(source, destination);
Run Code Online (Sandbox Code Playgroud)
而对于我的命令
String command = "ls -l";//just an example
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
Run Code Online (Sandbox Code Playgroud)
我应该在第一个频道之后断开会话,然后打开第二个频道吗?或者完全关闭会话并开启新会话?正如我所说,我是新手.
我正在考虑使用JSch库(或实际上是它的sharpSSH C#端口)为SFTP设置公钥客户端和服务器身份验证.不幸的是,我找不到关键加载函数使用的文件格式的任何文档:
jsch.addIdentity(filename, passphrase);
jsch.setKnownHosts(filename);
Run Code Online (Sandbox Code Playgroud)
私钥和已知主机文件使用什么文件格式?
我无法在我的ec2客户端上运行"sudo su",我通过java程序进入客户端并通过程序运行命令.我可以运行像"ls"和"ifconfig"这样的命令.
我收到一条错误说"sudo:抱歉,你必须有一个tty才能运行sudo".
我如何运行命令,我正在使用Jsch for ssh到我的ec2实例.
我正在使用java.security.KeyPairGenerator来生成RSA密钥对,然后尝试通过Jsch(0.1.49)中提供的KeyPair类加载私钥.代码:
public static void main(String[] args) {
String header = "-----BEGIN RSA PRIVATE KEY-----";
String footer = "-----END RSA PRIVATE KEY-----";
KeyPairGenerator keyPairGenerator;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048, new SecureRandom());
PrivateKey privateKey = keyPairGenerator.genKeyPair().getPrivate();
String key = new String(Base64.encodeBase64(privateKey.getEncoded()));
StringBuffer pem = new StringBuffer(header+"\n");
int len=key.length();
for( int idx=0 ; idx < len ; idx+= 64 )
pem.append(key.substring(idx, (idx+64)<len?(idx+64):len)+"\n");
pem.append(footer);
String privateKeyStr = pem.toString();
System.out.println(privateKeyStr);
com.jcraft.jsch.KeyPair.load(null, privateKeyStr.getBytes(), null);
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我是Android和Java的新手,这是我的情况:
我正在尝试使用ssh连接创建连接到Beaglebone Black的应用程序,然后通过发出来自Android设备的命令来控制连接到BBB的一些外围设备.我在AsyncTask中打开(成功)ssh会话,同时用户看到启动画面,如果连接成功,用户将获得确认,然后可以通过单击一些可用按钮发送预定义命令.
我接下来要做的是让会话打开,然后每次我想发出命令并等待BBB的响应时创建一个新的通道(exec或shell),但我不知道如何重用这样的ssh AsynkTask之外的会话.
甚至可能吗?
我使用的是Android Studio 0.8.2和Jsch 0.1.51,我的代码如下:
公共类SplashScreen扩展ActionBarActivity {
public static final int segundos =10;
public static final int milisegundos =segundos*1000;
public static final int delay=2;
private ProgressBar pbprogreso;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
pbprogreso= (ProgressBar)findViewById(R.id.pbprogreso);
pbprogreso.setMax(maximo_progreso());
empezaranimacion();
}
public void empezaranimacion()
{
sshConnect task = new sshConnect();
task.execute(new String[] {"http:"});
new CountDownTimer(milisegundos,1000)
{
@Override
public void onTick(long milisUntilFinished){
pbprogreso.setProgress(establecer_progreso(milisUntilFinished));
}
@Override
public void onFinish(){
finish();
}
}.start();
}
public int establecer_progreso (long miliseconds) …Run Code Online (Sandbox Code Playgroud) 我使用带有私钥的JSch到FTP文件
第3行有问题.如果没有这一行,JSch就无法运行.
我的问题是:第3行是否会使SFTP转移不安全?
我尝试通过Java连接SFTP服务器.
我收到了一个错误.
com.jcraft.jsch.JSchException:算法协商失败
这是POM:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.53</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是日志:
INFO: Connecting to **"FTP ADRESS"** port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-Maverick_SSHD
INFO: Local version string: SSH-2.0-JSCH-0.1.53
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: aes256-ctr is not available.
INFO: aes192-ctr is not available.
INFO: aes256-cbc is not available.
INFO: aes192-cbc is not available.
INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
INFO: diffie-hellman-group14-sha1 is not available.
INFO: ecdh-sha2-nistp256 is not available.
INFO: ecdh-sha2-nistp384 is not available.
INFO: ecdh-sha2-nistp521 is not available.
INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: …Run Code Online (Sandbox Code Playgroud) 我一直在与JSch合作开发一个项目,该项目连接到Unix服务器。使用ecdsa-sha2-nistp256进行密钥交换连接到服务器时遇到问题。
奇怪的是,我已经从这篇文章中启用了JCE(使用JDK 1.8 65),但是仍然没有骰子。
如果我正确阅读日志,则表明客户端确实启用了ecdsa-sha2-nistp256。但是它回落到ssh-rsa。也许我错过了一步?
错误:
UnknownHostKey: *********. RSA key fingerprint is 1d:ac:f7:50:10:07:51:4b:17:9c:cd:b5:be:25:75:28
Run Code Online (Sandbox Code Playgroud)
这是日志:
INFO: Connecting to ******* port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_6.6.1
INFO: Local version string: SSH-2.0-JSCH-0.1.53
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
INFO: kex: server: ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
INFO: kex: server: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: none,zlib@openssh.com
INFO: kex: …Run Code Online (Sandbox Code Playgroud) 我想在远程系统上执行命令,并希望获得相同的结果.
我试过以下代码:
package rough;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class RemoteSSH {
public static void main(String[] args) {
String host = "\\\\10.209.110.114";
String user = "Administrator";
String password = "Admin123";
String command1 = "ipconfig";
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp …Run Code Online (Sandbox Code Playgroud) 我试图使用JSch通过ssh连接到我的计算机,然后运行命令。
但是,当我运行代码时,我从未连接到计算机。和以下错误:
I/System.out: com.jcraft.jsch.JSchException: timeout: socket is not established
这是我的相关代码:
protected void sshTesting(){
String name = "";
String userName = "kalenpw";
String password = "hunter2";
String ip = "192.168.0.4";
int port = 22;
String command = "cmus-remote -u";
try{
JSch jsch = new JSch();
Session session = jsch.getSession(userName, ip, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing connection");
session.connect(10);
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
channel.setInputStream(null);
channel.connect(10000);
// name = session.getUserName();
// System.out.println(session.getHost());
}
catch(Exception e){
System.err.print(e);
System.out.print(e);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我第一次使用JSch,所以我几乎只关注他们的一个示例Exec.java …