本文整理汇总了Java中org.apache.sshd.ClientSession类的典型用法代码示例。如果您正苦于以下问题:Java ClientSession类的具体用法?Java ClientSession怎么用?Java ClientSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ClientSession类属于org.apache.sshd包,在下文中一共展示了ClientSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: copyBootStrap
import org.apache.sshd.ClientSession; //导入依赖的package包/类
protected String copyBootStrap(ClientSession session) throws IOException {
String dest = BOOTSTRAP_FILE.get() + random.nextLong();
SftpClient sftpClient = session.createSftpClient();
try {
Handle handle = sftpClient.open(dest, EnumSet.of(OpenMode.Read, OpenMode.Write, OpenMode.Create, OpenMode.Exclusive));
Attributes attr = new Attributes().perms(0700);
sftpClient.setStat(handle, attr);
byte[] content = BootstrapScript.getBootStrapSource();
sftpClient.write(handle, 0, content, 0, content.length);
sftpClient.close(handle);
return dest;
} finally {
if ( sftpClient != null ) {
sftpClient.close();
}
}
}
示例2: SSHWindow
import org.apache.sshd.ClientSession; //导入依赖的package包/类
/**
* The constructor for the SSH window
*
* @param app The main application to which this window should be attached
* @param session The current SSH session
* @param width The width of the window
* @param height The height of the window
*/
public SSHWindow(ClientSession session, int width, int height) {
setCaption("SSH");
setImmediate(true);
setResizable(false);
setWidth("" + width + "px");
setHeight(""+ height + "px");
VerticalLayout vPanel = new VerticalLayout();
vPanel.setWidth("100%");
vPanel.setHeight("100%");
if (session != null) {
terminal = new SSHTerminal(this, session, TERM_WIDTH, TERM_HEIGHT);
vPanel.addComponent(terminal);
vPanel.setComponentAlignment(terminal, Alignment.TOP_CENTER);
} else {
vPanel.addComponent(errorLabel);
vPanel.setComponentAlignment(errorLabel, Alignment.MIDDLE_CENTER);
}
addComponent(vPanel);
}
示例3: SSHTerminal
import org.apache.sshd.ClientSession; //导入依赖的package包/类
/**
* Constructor for the SSH Terminal
* @param app The main application
* @param sshWindow The window holding the terminal
* @param session The client instance used in the authorization of user names and passwords
* @param width The width of the terminal
* @param height The height of the terminal
*/
public SSHTerminal(SSHWindow sshWindow, ClientSession session, int width, int height) {
super();
this.sshWindow = sshWindow;
this.session = session;
TERM_WIDTH = width;
TERM_HEIGHT = height;
dumpContents = null;
closeClient = false;
isClosed = false;
try {
st = new SessionTerminal();
forceUpdate = true;
focus = false;
} catch (IOException e) { e.printStackTrace(); }
}
示例4: setUp
import org.apache.sshd.ClientSession; //导入依赖的package包/类
@SuppressWarnings("serial")
@Before
public void setUp() throws Exception {
app = new Application() {
@Override
public void init() {}
};
mainWindow = new Window();
app.setMainWindow(mainWindow);
SSHWindow sshWindow = new SSHWindow(null, 200, 200);
app.getMainWindow().addWindow(sshWindow);
SshClient client = SshClient.setUpDefaultClient();
client.start();
ClientSession session = null;
try {
session = client.connect(testHost, testPort).await().getSession();
} catch (Exception e) {
fail("Could not connect to host");
}
sshTerm = new SSHTerminal(sshWindow, session, 200, 200);
sshWindow.addComponent(sshTerm);
}
示例5: copyBootStrap
import org.apache.sshd.ClientSession; //导入依赖的package包/类
protected String copyBootStrap(ClientSession session) throws IOException {
String dest = BOOTSTRAP_FILE.get() + random.nextLong();
SftpClient sftpClient = session.createSftpClient();
try {
Handle handle = sftpClient.open(dest, EnumSet.of(OpenMode.Read, OpenMode.Write, OpenMode.Create, OpenMode.Exclusive));
Attributes attr = new Attributes().perms(0700);
sftpClient.setStat(handle, attr);
byte[] content = getBootstrapSource(BOOTSTRAP_SOURCE_OVERRIDE.get(), BOOTSTRAP_SOURCE.get());
sftpClient.write(handle, 0, content, 0, content.length);
sftpClient.close(handle);
return dest;
} finally {
if ( sftpClient != null ) {
sftpClient.close();
}
}
}
示例6: call
import org.apache.sshd.ClientSession; //导入依赖的package包/类
@Test(timeout = 10000L)
public void call() throws Exception {
final SshClient client = SshClient.setUpDefaultClient();
client.start();
try {
final ClientSession session = client.connect("localhost", 4222).await().getSession();
session.authPassword("jonathan", "secret");
final ClientChannel channel = session.createChannel("shell");
ByteArrayOutputStream sent = new ByteArrayOutputStream();
PipedOutputStream pipedIn = new TeePipedOutputStream(sent);
channel.setIn(new PipedInputStream(pipedIn));
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
channel.setOut(out);
channel.setErr(err);
channel.open();
pipedIn.write("properties\r\n".getBytes());
pipedIn.flush();
pipedIn.write("exit\r\n".getBytes());
pipedIn.flush();
channel.waitFor(ClientChannel.CLOSED, 0);
channel.close(false);
client.stop();
assertTrue(new String(sent.toByteArray()).contains("properties\r\nexit\r\n"));
assertTrue(new String(out.toByteArray()).contains("ServerService(id=ssh)"));
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
示例7: receiveFiles
import org.apache.sshd.ClientSession; //导入依赖的package包/类
@Override
public void receiveFiles(TransferJob job) throws FileNotFoundException, Exception {
log.debug("Receiving files from {}",job.getSourceUrl());
// TODO error handle URI parsing.
final URI url = new URI(job.getSourceUrl());
final String hostname = url.getHost();
final int port = url.getPort() != -1 ? url.getPort() : 22;
final String path = url.getPath();
ConnectFuture connectFuture = sshClient.connect(job.getSourceUsername(), hostname, port);
ClientSession session = connectFuture.await().getSession();
session.addPasswordIdentity(job.getSourcePassword());
session.auth().await();
SftpClient sftpClient = session.createSftpClient();
for(DirEntry dirEntry : sftpClient.readDir(path)){
if( dirEntry.attributes.isRegularFile()
&& FilenameUtils.wildcardMatch(dirEntry.filename, job.getSourceFilepattern())){
queueFile(dirEntry,path,sftpClient,job);
}
}
sftpClient.close();
session.close(false);
}
示例8: sendFile
import org.apache.sshd.ClientSession; //导入依赖的package包/类
@Override
public void sendFile(Message msg, TransferJob job, TransferEvent event) {
try {
final String filename = msg.getStringProperty("filename");
final URI url = new URI(job.getTargetUrl());
final String hostname = url.getHost();
final int port = url.getPort() != -1 ? url.getPort() : 22;
final String path = url.getPath();
ConnectFuture connectFuture = sshClient.connect(job.getTargetUsername(), hostname, port);
ClientSession session = connectFuture.await().getSession();
session.addPasswordIdentity(job.getTargetPassword());
session.auth().await();
SftpClient sftpClient = session.createSftpClient();
// TODO make it possible to rename the file according to some generic pattern.
//job.getTargetFilename() + RandomStringUtils.randomAlphanumeric(5);
log.debug("Saving file to SFTP: {}, job: {}", hostname + ":" + port + path + "/" + filename, job.getName() + "(" + job.getId() + ")");
OutputStream os = sftpClient.write(path + "/" + filename);
BufferedOutputStream bos = new BufferedOutputStream(os);
// This will block until the entire content is saved on disk
msg.setObjectProperty("JMS_AMQ_SaveStream", bos);
bos.close();
os.close();
event.setState("done");
event.setTimestamp(new DateTime());
event = eventRepo.saveAndFlush(event);
log.debug("File saved to SFTP: {}, job: {}", path + "/" + filename, job.getName() + "(" + job.getId() + ")");
} catch (Exception e) {
event.setState("send failed");
event.setTimestamp(new DateTime());
event = eventRepo.save(event);
log.warn("Error sending file {}, job: {}", event.getFilename(), job.getName() + "(" + job.getId() + ")");
log.warn("Error descr", e);
throw new RuntimeException("Rollback SFTP transaction");
}
}
示例9: authSuccess
import org.apache.sshd.ClientSession; //导入依赖的package包/类
protected boolean authSuccess(ClientSession session) {
int ret = session.waitFor(ClientSession.CLOSED | ClientSession.AUTHED | ClientSession.WAIT_AUTH, SSH_TIMEOUT.get());
if ((ret & ClientSession.AUTHED) != ClientSession.AUTHED) {
return false;
}
return true;
}
示例10: SshAgentConnection
import org.apache.sshd.ClientSession; //导入依赖的package包/类
public SshAgentConnection(long agentId, String uri, EventService eventService, SshAgentConnectionFactory factory,
ClientSession session, ChannelExec exec, int callbackPort) {
super(agentId, uri, eventService);
this.factory = factory;
this.session = session;
this.exec = exec;
this.callbackPort = callbackPort;
}
示例11: shutdown
import org.apache.sshd.ClientSession; //导入依赖的package包/类
public void shutdown() throws StartException {
try {
for(ClientSession session : clientSessions) {
session.close(false);
}
client.stop();
sshd.stop();
} catch (InterruptedException e) {
throw new StartException("Failed to stop SshServer", e);
}
}
示例12: createConnection
import org.apache.sshd.ClientSession; //导入依赖的package包/类
protected AgentConnection createConnection(Agent agent, SshConnectionOptions opts) throws IOException, InterruptedException {
SshClient client = getClient();
ClientSession session = null;
boolean success = false;
try {
session = connect(client, opts);
String hostIp = getIp(opts);
String script = copyBootStrap(session);
int port = setForwarding(session);
log.info("Allocated random port [{}] on [{}]", port, opts.getHost());
EofAwareChannelExec exec = callBootStrap(agent, session, String.format("%s --read-env --port %d --ip %s", script, port, hostIp));
final SshAgentConnection sshAgent = new SshAgentConnection(agent.getId(), agent.getUri(), eventService, this, session, exec, port);
success = true;
connections.add(sshAgent);
exec.onEof(new Runnable() {
@Override
public void run() {
sshAgent.close();
}
});
CloseListener listener = new CloseListener(sshAgent);
session.addListener(listener);
OpenFuture execOpen = exec.open();
execOpen.addListener(listener);
try {
execOpen.await(SSH_TIMEOUT.get(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new IllegalStateException("Interrupted waiting for script [" + script + "]", e);
}
if ( ! execOpen.isOpened() ) {
throw new IOException("Failed to start script [" + script + "]");
}
success = writeAuth(sshAgent);
if ( ! success ) {
sshAgent.close();
throw new IOException("Failed to write context info for agent [" + sshAgent.getAgentId() + "]");
}
return sshAgent;
} finally {
if ( ! success && session != null ) {
session.close(true);
}
}
}
示例13: setForwarding
import org.apache.sshd.ClientSession; //导入依赖的package包/类
protected int setForwarding(ClientSession session) throws IOException {
SshdSocketAddress address = session.startRemotePortForwarding(new SshdSocketAddress("localhost", 0), new SshdSocketAddress("localhost", 8080));
return address.getPort();
}
示例14: getSession
import org.apache.sshd.ClientSession; //导入依赖的package包/类
public ClientSession getSession() {
return session;
}
示例15: setSession
import org.apache.sshd.ClientSession; //导入依赖的package包/类
public void setSession(ClientSession session) {
this.session = session;
}