mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 23:30:55 +03:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20c5ab8dfc | ||
|
|
d9c438ed16 | ||
|
|
653e8ad4f2 | ||
|
|
c46dc913e8 | ||
|
|
069ebbd47d | ||
|
|
da2cec8fa2 | ||
|
|
75caa8bcf3 | ||
|
|
f664b7b24f | ||
|
|
70f3aeee68 | ||
|
|
882d40a1b6 | ||
|
|
9649b2f72e | ||
|
|
79a8d0b3ad |
@@ -1,3 +1,8 @@
|
||||
.. image:: http://api.flattr.com/button/button-compact-static-100x17.png
|
||||
:align: right
|
||||
:alt: Flattr this
|
||||
:target: http://flattr.com/thing/49085/sshj-ssh-scp-and-sftp-library-for-java
|
||||
|
||||
sshj - SSHv2 library for Java
|
||||
==============================
|
||||
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
||||
<groupId>net.schmizz</groupId>
|
||||
<artifactId>sshj</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>0.3.0</version>
|
||||
<version>0.3.1</version>
|
||||
|
||||
<name>sshj</name>
|
||||
<description>SSHv2 library for Java</description>
|
||||
|
||||
@@ -20,6 +20,7 @@ import net.schmizz.sshj.connection.channel.direct.Session;
|
||||
import net.schmizz.sshj.connection.channel.direct.Session.Command;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/** This examples demonstrates how a remote command can be executed. */
|
||||
public class Exec {
|
||||
@@ -36,6 +37,7 @@ public class Exec {
|
||||
try {
|
||||
final Command cmd = session.exec("ping -c 1 google.com");
|
||||
System.out.print(cmd.getOutputAsString());
|
||||
cmd.join(5, TimeUnit.SECONDS);
|
||||
System.out.println("\n** exit status: " + cmd.getExitStatus());
|
||||
} finally {
|
||||
session.close();
|
||||
|
||||
28
src/main/java/net/schmizz/sshj/AndroidConfig.java
Normal file
28
src/main/java/net/schmizz/sshj/AndroidConfig.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2010 Shikhar Bhushan
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.schmizz.sshj;
|
||||
|
||||
import net.schmizz.sshj.transport.random.JCERandom;
|
||||
import net.schmizz.sshj.transport.random.SingletonRandomFactory;
|
||||
|
||||
public class AndroidConfig extends DefaultConfig {
|
||||
|
||||
@Override
|
||||
protected void initRandomFactory(boolean ignored) {
|
||||
setRandomFactory(new SingletonRandomFactory(new JCERandom.Factory()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -272,6 +272,11 @@ public abstract class AbstractChannel
|
||||
close.await();
|
||||
}
|
||||
|
||||
public void join(int timeout, TimeUnit unit)
|
||||
throws ConnectionException {
|
||||
close.await(timeout, unit);
|
||||
}
|
||||
|
||||
protected synchronized void sendClose()
|
||||
throws TransportException {
|
||||
try {
|
||||
|
||||
@@ -23,6 +23,7 @@ import net.schmizz.sshj.transport.TransportException;
|
||||
import java.io.Closeable;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/** A channel is the basic medium for application-layer data on top of an SSH transport. */
|
||||
public interface Channel
|
||||
@@ -138,4 +139,7 @@ public interface Channel
|
||||
void join()
|
||||
throws ConnectionException;
|
||||
|
||||
void join(int timeout, TimeUnit unit)
|
||||
throws ConnectionException;
|
||||
|
||||
}
|
||||
|
||||
@@ -35,10 +35,7 @@
|
||||
*/
|
||||
package net.schmizz.sshj.connection.channel.direct;
|
||||
|
||||
import net.schmizz.sshj.common.Buffer;
|
||||
import net.schmizz.sshj.common.IOUtils;
|
||||
import net.schmizz.sshj.common.SSHPacket;
|
||||
import net.schmizz.sshj.common.StreamCopier;
|
||||
import net.schmizz.sshj.common.*;
|
||||
import net.schmizz.sshj.connection.Connection;
|
||||
import net.schmizz.sshj.connection.ConnectionException;
|
||||
import net.schmizz.sshj.connection.channel.ChannelInputStream;
|
||||
@@ -50,9 +47,10 @@ import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/** {@link Session} implementation. */
|
||||
public class
|
||||
SessionChannel
|
||||
/**
|
||||
* {@link Session} implementation.
|
||||
*/
|
||||
public class SessionChannel
|
||||
extends AbstractDirectChannel
|
||||
implements Session, Session.Command, Session.Shell, Session.Subsystem {
|
||||
|
||||
@@ -66,6 +64,8 @@ public class
|
||||
|
||||
private Boolean canDoFlowControl;
|
||||
|
||||
private boolean usedUp;
|
||||
|
||||
public SessionChannel(Connection conn) {
|
||||
super(conn, "session");
|
||||
}
|
||||
@@ -114,9 +114,11 @@ public class
|
||||
@Override
|
||||
public Command exec(String command)
|
||||
throws ConnectionException, TransportException {
|
||||
checkReuse();
|
||||
log.info("Will request to exec `{}`", command);
|
||||
sendChannelRequest("exec", true, new Buffer.PlainBuffer().putString(command))
|
||||
.await(conn.getTimeout(), TimeUnit.SECONDS);
|
||||
usedUp = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -170,8 +172,7 @@ public class
|
||||
|
||||
@Override
|
||||
public void reqX11Forwarding(String authProto, String authCookie, int screen)
|
||||
throws ConnectionException,
|
||||
TransportException {
|
||||
throws ConnectionException, TransportException {
|
||||
sendChannelRequest(
|
||||
"x11-req",
|
||||
true,
|
||||
@@ -199,16 +200,20 @@ public class
|
||||
@Override
|
||||
public Shell startShell()
|
||||
throws ConnectionException, TransportException {
|
||||
checkReuse();
|
||||
sendChannelRequest("shell", true, null).await(conn.getTimeout(), TimeUnit.SECONDS);
|
||||
usedUp = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Subsystem startSubsystem(String name)
|
||||
throws ConnectionException, TransportException {
|
||||
checkReuse();
|
||||
log.info("Will request `{}` subsystem", name);
|
||||
sendChannelRequest("subsystem", true, new Buffer.PlainBuffer().putString(name))
|
||||
.await(conn.getTimeout(), TimeUnit.SECONDS);
|
||||
usedUp = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -238,4 +243,15 @@ public class
|
||||
super.gotExtendedData(dataTypeCode, buf);
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void notifyError(SSHException error) {
|
||||
err.notifyError(error);
|
||||
super.notifyError(error);
|
||||
}
|
||||
|
||||
private void checkReuse() {
|
||||
if (usedUp)
|
||||
throw new SSHRuntimeException("This session channel is all used up");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ public class StatefulSFTPClient
|
||||
@Override
|
||||
public void put(String source, String dest)
|
||||
throws IOException {
|
||||
super.get(source, cwdify(dest));
|
||||
super.put(source, cwdify(dest));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -123,9 +123,6 @@ public final class SCPDownloadClient
|
||||
return true;
|
||||
|
||||
case (char) 1:
|
||||
addWarning(msg.substring(1));
|
||||
break;
|
||||
|
||||
case (char) 2:
|
||||
throw new SCPException("Remote SCP command returned error: " + msg.substring(1));
|
||||
|
||||
|
||||
@@ -61,7 +61,6 @@ abstract class SCPEngine {
|
||||
|
||||
final SessionFactory host;
|
||||
final TransferListener listener;
|
||||
final Queue<String> warnings = new LinkedList<String>();
|
||||
|
||||
Command scp;
|
||||
int exitStatus;
|
||||
@@ -86,19 +85,6 @@ abstract class SCPEngine {
|
||||
return exitStatus;
|
||||
}
|
||||
|
||||
public Queue<String> getWarnings() {
|
||||
return warnings;
|
||||
}
|
||||
|
||||
public boolean hadWarnings() {
|
||||
return !warnings.isEmpty();
|
||||
}
|
||||
|
||||
void addWarning(String warning) {
|
||||
log.warn(warning);
|
||||
warnings.add(warning);
|
||||
}
|
||||
|
||||
void check(String what)
|
||||
throws IOException {
|
||||
int code = scp.getInputStream().read();
|
||||
@@ -111,9 +97,7 @@ abstract class SCPEngine {
|
||||
case 0: // OK
|
||||
log.debug(what);
|
||||
return;
|
||||
case 1:
|
||||
addWarning(readMessage());
|
||||
break;
|
||||
case 1: // Warning? not
|
||||
case 2:
|
||||
throw new SCPException("Remote SCP command had error: " + readMessage());
|
||||
default:
|
||||
@@ -123,7 +107,6 @@ abstract class SCPEngine {
|
||||
|
||||
void cleanSlate() {
|
||||
exitStatus = -1;
|
||||
warnings.clear();
|
||||
}
|
||||
|
||||
void execSCPWith(List<Arg> args, String path)
|
||||
|
||||
Reference in New Issue
Block a user