How to use connect method of com.consol.citrus.ssh.client.SshClient class

Best Citrus code snippet using com.consol.citrus.ssh.client.SshClient.connect

Source:SshClient.java Github

copy

Full Screen

...29import org.apache.sshd.client.keyverifier.KnownHostsServerKeyVerifier;30import org.springframework.util.*;31import java.io.*;32/**33 * Ssh client connects to ssh server and sends commands to that server.34 *35 * @author Roland Huss, Christoph Deppisch36 * @since 1.437 */38public class SshClient extends AbstractEndpoint implements Producer, ReplyConsumer {39 /** Store of reply messages */40 private CorrelationManager<Message> correlationManager;41 // Session for the SSH communication42 private Session session;43 // SSH implementation44 private JSch jsch = new JSch();45 /**46 * Default constructor initializing endpoint configuration.47 */48 public SshClient() {49 this(new SshEndpointConfiguration());50 }51 /**52 * Default constructor using endpoint configuration.53 * @param endpointConfiguration54 */55 protected SshClient(SshEndpointConfiguration endpointConfiguration) {56 super(endpointConfiguration);57 this.correlationManager = new PollingCorrelationManager<>(endpointConfiguration, "Reply message did not arrive yet");58 }59 @Override60 public SshEndpointConfiguration getEndpointConfiguration() {61 return (SshEndpointConfiguration) super.getEndpointConfiguration();62 }63 /**64 * Send a message as SSH request. The message format is created from65 * {@link com.consol.citrus.ssh.server.SshServer}.66 *67 * @param message the message object to send.68 * @param context69 */70 public void send(Message message, TestContext context) {71 String correlationKeyName = getEndpointConfiguration().getCorrelator().getCorrelationKeyName(getName());72 String correlationKey = getEndpointConfiguration().getCorrelator().getCorrelationKey(message);73 correlationManager.saveCorrelationKey(correlationKeyName, correlationKey, context);74 SshRequest request = (SshRequest) getEndpointConfiguration().getMessageConverter().convertOutbound(message, getEndpointConfiguration(), context);75 if (getEndpointConfiguration().isStrictHostChecking()) {76 setKnownHosts();77 }78 String rUser = getRemoteUser(message);79 connect(rUser);80 ChannelExec channelExec = null;81 ByteArrayOutputStream outStream = new ByteArrayOutputStream();82 ByteArrayOutputStream errStream = new ByteArrayOutputStream();83 int rc;84 try {85 channelExec = openChannelExec();86 channelExec.setErrStream(errStream);87 channelExec.setOutputStream(outStream);88 channelExec.setCommand(request.getCommand());89 doConnect(channelExec);90 if (request.getStdin() != null) {91 sendStandardInput(channelExec, request.getStdin());92 }93 waitCommandToFinish(channelExec);94 rc = channelExec.getExitStatus();95 } finally {96 if (channelExec != null && channelExec.isConnected()) {97 channelExec.disconnect();98 }99 disconnect();100 }101 SshResponse sshResp = new SshResponse(outStream.toString(),errStream.toString(),rc);102 Message response = getEndpointConfiguration().getMessageConverter().convertInbound(sshResp, getEndpointConfiguration(), context)103 .setHeader("user", rUser);104 correlationManager.store(correlationKey, response);105 }106 @Override107 public Message receive(TestContext context) {108 return receive(correlationManager.getCorrelationKey(109 getEndpointConfiguration().getCorrelator().getCorrelationKeyName(getName()), context), context);110 }111 @Override112 public Message receive(String selector, TestContext context) {113 return receive(selector, context, getEndpointConfiguration().getTimeout());114 }115 @Override116 public Message receive(TestContext context, long timeout) {117 return receive(correlationManager.getCorrelationKey(118 getEndpointConfiguration().getCorrelator().getCorrelationKeyName(getName()), context), context, timeout);119 }120 @Override121 public Message receive(String selector, TestContext context, long timeout) {122 Message message = correlationManager.find(selector, timeout);123 if (message == null) {124 throw new ActionTimeoutException("Action timeout while receiving synchronous reply message from ssh server");125 }126 return message;127 }128 @Override129 public Producer createProducer() {130 return this;131 }132 @Override133 public SelectiveConsumer createConsumer() {134 return this;135 }136 private void connect(String rUser) {137 if (session == null || !session.isConnected()) {138 try {139 if (StringUtils.hasText(getEndpointConfiguration().getPrivateKeyPath())) {140 jsch.addIdentity(getPrivateKeyPath(), getEndpointConfiguration().getPrivateKeyPassword());141 }142 } catch (JSchException e) {143 throw new CitrusRuntimeException("Cannot add private key " + getEndpointConfiguration().getPrivateKeyPath() + ": " + e,e);144 } catch (IOException e) {145 throw new CitrusRuntimeException("Cannot open private key file " + getEndpointConfiguration().getPrivateKeyPath() + ": " + e,e);146 }147 try {148 session = jsch.getSession(rUser, getEndpointConfiguration().getHost(), getEndpointConfiguration().getPort());149 if (StringUtils.hasText(getEndpointConfiguration().getPassword())) {150 session.setUserInfo(new UserInfoWithPlainPassword(getEndpointConfiguration().getPassword()));151 session.setPassword(getEndpointConfiguration().getPassword());152 }153 session.setConfig(KnownHostsServerKeyVerifier.STRICT_CHECKING_OPTION, getEndpointConfiguration().isStrictHostChecking() ? "yes" : "no");154 session.connect();155 } catch (JSchException e) {156 throw new CitrusRuntimeException("Cannot connect via SSH: " + e,e);157 }158 }159 }160 private void disconnect() {161 if (session.isConnected()) {162 session.disconnect();163 }164 }165 private ChannelExec openChannelExec() throws CitrusRuntimeException {166 ChannelExec channelExec;167 try {168 channelExec = (ChannelExec) session.openChannel("exec");169 } catch (JSchException e) {170 throw new CitrusRuntimeException("Cannot open EXEC SSH channel: " + e,e);171 }172 return channelExec;173 }174 private void waitCommandToFinish(ChannelExec pCh) {175 final long until = System.currentTimeMillis() + getEndpointConfiguration().getCommandTimeout();176 try {177 while (!pCh.isClosed() && System.currentTimeMillis() < until) {178 Thread.sleep(250);179 }180 } catch (InterruptedException e) {181 throw new RuntimeException("Interrupted", e);182 }183 if (!pCh.isClosed()) {184 throw new CitrusRuntimeException("Timeout: Channel not finished within " + getEndpointConfiguration().getCommandTimeout() + " ms");185 }186 }187 private void sendStandardInput(ChannelExec pCh, String pInput) {188 OutputStream os = null;189 try {190 os = pCh.getOutputStream();191 os.write(pInput.getBytes());192 } catch (IOException e) {193 throw new CitrusRuntimeException("Cannot write to standard input of SSH channel: " + e,e);194 } finally {195 if (os != null) {196 try {197 os.close();198 } catch (IOException e) {199 // best try200 }201 }202 }203 }204 private void doConnect(ChannelExec pCh) {205 try {206 if (getEndpointConfiguration().getConnectionTimeout() != 0) {207 pCh.connect(getEndpointConfiguration().getConnectionTimeout());208 } else {209 pCh.connect();210 }211 } catch (JSchException e) {212 throw new CitrusRuntimeException("Cannot connect EXEC SSH channel: " + e,e);213 }214 }215 private String getRemoteUser(Message message) {216 String rUser = (String) message.getHeader("user");217 if (rUser == null) {218 // Use default uses219 rUser = getEndpointConfiguration().getUser();220 }221 if (rUser == null) {222 throw new CitrusRuntimeException("No user given for connecting to SSH server");223 }224 return rUser;225 }226 private void setKnownHosts() {227 if (getEndpointConfiguration().getKnownHosts() == null) {228 throw new CitrusRuntimeException("Strict host checking is enabled but no knownHosts given");229 }230 try {231 InputStream khIs = FileUtils.getFileResource(getEndpointConfiguration().getKnownHosts()).getInputStream();232 if (khIs == null) {233 throw new CitrusRuntimeException("Cannot find knownHosts at " + getEndpointConfiguration().getKnownHosts());234 }235 jsch.setKnownHosts(khIs);236 } catch (JSchException e) {...

Full Screen

Full Screen

Source:SshClientTest.java Github

copy

Full Screen

...88 jsch.setKnownHosts(isA(InputStream.class));89 standardChannelPrepAndSend();90 }91 private void standardChannelPrepAndSend() throws JSchException, IOException {92 session.connect();93 prepareChannel(COMMAND, 0);94 disconnect();95 send();96 }97 @Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = ".*/that/does/not/exist.*")98 public void withUnknownPrivateKey() throws JSchException {99 strictHostChecking(false,null);100 client.getEndpointConfiguration().setPrivateKeyPath("/file/that/does/not/exist");101 doThrow(new JSchException("No such file")).when(jsch).addIdentity("/file/that/does/not/exist", (String) null);102 send();103 }104 @Test(expectedExceptions = CitrusRuntimeException.class,expectedExceptionsMessageRegExp = ".*/notthere\\.key.*")105 public void withUnknownPrivateKey2() throws JSchException {106 strictHostChecking(false,null);107 client.getEndpointConfiguration().setPrivateKeyPath("classpath:com/consol/citrus/ssh/notthere.key");108 jsch.addIdentity("classpath:com/consol/citrus/ssh/notthere.key",(String) null);109 send();110 }111 @Test112 public void withPrivateKey() throws JSchException, IOException {113 strictHostChecking(false,null);114 client.getEndpointConfiguration().setPrivateKeyPath("classpath:com/consol/citrus/ssh/private.key");115 jsch.addIdentity(isA(String.class), (String) isNull());116 strictHostChecking(false, null);117 standardChannelPrepAndSend();118 }119 @Test120 public void withPassword() throws JSchException, IOException {121 client.getEndpointConfiguration().setPassword("consol");122 session.setUserInfo(getUserInfo("consol"));123 session.setPassword("consol");124 strictHostChecking(false, null);125 standardChannelPrepAndSend();126 }127 @Test128 public void straight() throws JSchException, IOException {129 strictHostChecking(false, null);130 standardChannelPrepAndSend();131 }132 private void send() {133 client.send(createMessage(COMMAND, STDIN), context);134 }135 private void disconnect() throws JSchException {136 channel.disconnect();137 when(session.isConnected()).thenReturn(true);138 session.disconnect();139 when(session.openChannel("exec")).thenReturn(channel);140 }141 private void prepareChannel(String pCommand, int pExitStatus) throws JSchException, IOException {142 channel.setErrStream((OutputStream) any());143 channel.setOutputStream((OutputStream) any());144 channel.setInputStream((InputStream) any());145 channel.setCommand(pCommand);146 channel.connect(CONNECTTION_TIMEOUT);147 when(channel.getOutputStream()).thenReturn(outStream);148 when(channel.isClosed()).thenReturn(false);149 when(channel.isClosed()).thenReturn(true);150 when(channel.getExitStatus()).thenReturn(pExitStatus);151 when(channel.isConnected()).thenReturn(true);152 }153 private Message createMessage(String pCommand, String pInput) {154 SshRequest request = new SshRequest(pCommand,pInput);155 StringResult payload = new StringResult();156 new SshMarshaller().marshal(request, payload);157 return new DefaultMessage(payload.toString());158 }159 private void strictHostChecking(boolean flag,String knownHosts) {160 if (flag) {...

Full Screen

Full Screen

Source:ScpClient.java Github

copy

Full Screen

...93 }94 return FtpMessage.success();95 }96 @Override97 protected void connectAndLogin() {98 try {99 SshClient client = SshClient.setUpDefaultClient();100 client.start();101 if (getEndpointConfiguration().isStrictHostChecking()) {102 client.setServerKeyVerifier(new KnownHostsServerKeyVerifier(RejectAllServerKeyVerifier.INSTANCE, FileUtils.getFileResource(getEndpointConfiguration().getKnownHosts()).getFile().toPath()));103 } else {104 client.setServerKeyVerifier(AcceptAllServerKeyVerifier.INSTANCE);105 }106 ClientSession session = client.connect(getEndpointConfiguration().getUser(), getEndpointConfiguration().getHost(), getEndpointConfiguration().getPort()).verify(getEndpointConfiguration().getTimeout()).getSession();107 session.addPasswordIdentity(getEndpointConfiguration().getPassword());108 Resource privateKey = FileUtils.getFileResource(getPrivateKeyPath());109 if (privateKey instanceof ClassPathResource) {110 new ClassLoadableResourceKeyPairProvider(privateKey.getFile().getPath()).loadKeys().forEach(session::addPublicKeyIdentity);111 } else {112 new FileKeyPairProvider(privateKey.getFile().toPath()).loadKeys().forEach(session::addPublicKeyIdentity);113 }114 session.auth().verify(getEndpointConfiguration().getTimeout());115 scpClient = new DefaultScpClientCreator().createScpClient(session);116 } catch (Exception e) {117 throw new CitrusRuntimeException(String.format("Failed to login to SCP server using credentials: %s:%s:%s", getEndpointConfiguration().getUser(), getEndpointConfiguration().getPassword(), getEndpointConfiguration().getPrivateKeyPath()), e);118 }119 }120 @Override...

Full Screen

Full Screen

connect

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.ssh.client.SshClient;2import com.consol.citrus.ssh.message.SshMessage;3import com.consol.citrus.ssh.message.SshMessageHeaders;4import org.springframework.context.annotation.Bean;5import org.springframework.context.annotation.Configuration;6import org.springframework.context.annotation.Import;7import org.springframework.integration.annotation.MessageEndpoint;8import org.springframework.integration.annotation.ServiceActivator;9import org.springframework.integration.ssh.config.SshClientFactoryBean;10import org.springframework.integration.ssh.inbound.SshInboundChannelAdapter;11import org.springframework.integration.ssh.outbound.SshOutboundGateway;12import org.springframework.messaging.Message;13import org.springframework.messaging.MessageHandler;14import org.springframework.messaging.MessagingException;15import java.util.HashMap;16import java.util.Map;17public class SshClientTest {18 public SshClientFactoryBean sshClientFactoryBean() {19 SshClientFactoryBean sshClientFactoryBean = new SshClientFactoryBean();20 sshClientFactoryBean.setHost("localhost");21 sshClientFactoryBean.setPort(22);22 sshClientFactoryBean.setUser("user");23 sshClientFactoryBean.setPassword("password");24 return sshClientFactoryBean;25 }26 public SshOutboundGateway sshOutboundGateway() {27 SshOutboundGateway sshOutboundGateway = new SshOutboundGateway();28 sshOutboundGateway.setClient(sshClientFactoryBean().getObject());29 sshOutboundGateway.setCommand("ls");30 sshOutboundGateway.setRemoteDirectory("remoteDirectory");31 sshOutboundGateway.setLocalDirectory("localDirectory");32 sshOutboundGateway.setFileName("fileName");33 sshOutboundGateway.setFileMode("fileMode");34 sshOutboundGateway.setCharset("charset");35 sshOutboundGateway.setReplyTimeout(1000L);36 sshOutboundGateway.setReplyChannelName("replyChannelName");37 sshOutboundGateway.setRequestChannelName("requestChannelName");38 sshOutboundGateway.setExpectReply(true);39 sshOutboundGateway.setBeanFactory(null);40 return sshOutboundGateway;41 }42 public SshInboundChannelAdapter sshInboundChannelAdapter() {43 SshInboundChannelAdapter sshInboundChannelAdapter = new SshInboundChannelAdapter();44 sshInboundChannelAdapter.setClient(sshClientFactoryBean().getObject());45 sshInboundChannelAdapter.setCommand("ls");

Full Screen

Full Screen

connect

Using AI Code Generation

copy

Full Screen

1public class 3.java {2 public static void main(String[] args) {3 SshClient sshClient = new SshClient();4 sshClient.setHost("localhost");5 sshClient.setPort(22);6 sshClient.setUser("username");7 sshClient.setPassword("password");8 sshClient.connect();9 }10}11public class 4.java {12 public static void main(String[] args) {13 SshClient sshClient = new SshClient();14 sshClient.setHost("localhost");15 sshClient.setPort(22);16 sshClient.setUser("username");17 sshClient.setPassword("password");18 sshClient.connect();19 sshClient.execute("ls -l");20 }21}22public class 5.java {23 public static void main(String[] args) {24 SshClient sshClient = new SshClient();25 sshClient.setHost("localhost");26 sshClient.setPort(22);27 sshClient.setUser("username");28 sshClient.setPassword("password");29 sshClient.connect();30 sshClient.execute("ls -l");31 sshClient.disconnect();32 }33}34public class 6.java {35 public static void main(String[] args) {36 SshClient sshClient = new SshClient();37 sshClient.setHost("localhost");38 sshClient.setPort(22);39 sshClient.setUser("username");40 sshClient.setPassword("password");41 sshClient.connect();42 sshClient.execute("ls -l");43 sshClient.disconnect();44 }45}46public class 7.java {47 public static void main(String[] args) {48 SshClient sshClient = new SshClient();49 sshClient.setHost("localhost");50 sshClient.setPort(22);51 sshClient.setUser("username");52 sshClient.setPassword("password");53 sshClient.setCommandTimeout(10000);54 sshClient.connect();55 sshClient.execute("ls -l");

Full Screen

Full Screen

connect

Using AI Code Generation

copy

Full Screen

1SshClient sshClient = new SshClient();2sshClient.setDefaultPort(22);3sshClient.setDefaultTimeout(5000);4sshClient.setDefaultHost("localhost");5sshClient.setDefaultUsername("user");6sshClient.setDefaultPassword("password");7sshClient.connect();8SshClient sshClient = new SshClient();9sshClient.setDefaultPort(22);10sshClient.setDefaultTimeout(5000);11sshClient.setDefaultHost("localhost");12sshClient.setDefaultUsername("user");13sshClient.setDefaultPassword("password");14sshClient.connect();15sshClient.executeCommand("ls -l");16SshClient sshClient = new SshClient();17sshClient.setDefaultPort(22);18sshClient.setDefaultTimeout(5000);19sshClient.setDefaultHost("localhost");20sshClient.setDefaultUsername("user");21sshClient.setDefaultPassword("password");22sshClient.connect();23sshClient.executeCommand("ls -l");24sshClient.disconnect();25SshClient sshClient = new SshClient();26sshClient.setDefaultPort(22);27SshClient sshClient = new SshClient();28sshClient.setDefaultPort(22);29int port = sshClient.getDefaultPort();30SshClient sshClient = new SshClient();31sshClient.setDefaultTimeout(5000);32SshClient sshClient = new SshClient();33sshClient.setDefaultTimeout(5000);34int timeout = sshClient.getDefaultTimeout();35SshClient sshClient = new SshClient();36sshClient.setDefaultHost("localhost");37SshClient sshClient = new SshClient();38sshClient.setDefaultHost("localhost");39String host = sshClient.getDefaultHost();

Full Screen

Full Screen

connect

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.ssh.client.SshClient;2import com.consol.citrus.ssh.message.SshMessage;3import com.consol.citrus.ssh.server.SshServer;4import com.consol.citrus.ssh.server.SshServerBuilder;5import org.testng.annotations.Test;6public class SshClientTest {7 public void sshClientTest() {8 SshServer sshServer = new SshServerBuilder()9 .autoStart(true)10 .port(22)11 .build();12 SshClient sshClient = new SshClient();13 sshClient.connect("localhost", 22);14 SshMessage response = (SshMessage) sshClient.send("ls");15 System.out.println(response.getPayload());16 }17}18import com.consol.citrus.ssh.client.SshClient;19import com.consol.citrus.ssh.message.SshMessage;20import com.consol.citrus.ssh.server.SshServer;21import com.consol.citrus.ssh.server.SshServerBuilder;22import org.testng.annotations.Test;23public class SshClientTest {24 public void sshClientTest() {25 SshServer sshServer = new SshServerBuilder()26 .autoStart(true)27 .port(22)28 .build();29 SshClient sshClient = new SshClient();30 sshClient.connect("localhost", 22);31 SshMessage response = (SshMessage) sshClient.send("ls");32 System.out.println(response.getPayload());33 }34}35import com.consol.citrus.ssh.client.SshClient;36import com.consol.citrus.ssh.message.SshMessage;37import com.consol.citrus.ssh.server.SshServer;38import com.consol.citrus.ssh.server.SshServerBuilder;39import org.testng.annotations.Test;40public class SshClientTest {41 public void sshClientTest() {42 SshServer sshServer = new SshServerBuilder()

Full Screen

Full Screen

connect

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh.client;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;4import com.consol.citrus.ssh.client.SshClient;5import org.junit.Test;6import org.springframework.beans.factory.annotation.Autowired;7import org.springframework.core.io.ClassPathResource;8public class SshConnectTest extends JUnit4CitrusTestDesigner {9 private SshClient sshClient;10 public void sshConnectTest() {11 sshClient.connect();12 }13}14package com.consol.citrus.ssh.client;15import com.consol.citrus.annotations.CitrusTest;16import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;17import com.consol.citrus.ssh.client.SshClient;18import org.junit.Test;19import org.springframework.beans.factory.annotation.Autowired;20import org.springframework.core.io.ClassPathResource;21public class SshDisconnectTest extends JUnit4CitrusTestDesigner {22 private SshClient sshClient;23 public void sshDisconnectTest() {24 sshClient.disconnect();25 }26}27package com.consol.citrus.ssh.client;28import com.consol.citrus.annotations.CitrusTest;29import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;30import com.consol.citrus.ssh.client.SshClient;31import org.junit.Test;32import org.springframework.beans.factory.annotation.Autowired;33import org.springframework.core.io.ClassPathResource;34public class SshExecuteTest extends JUnit4CitrusTestDesigner {35 private SshClient sshClient;36 public void sshExecuteTest() {37 sshClient.execute("ls");38 }39}

Full Screen

Full Screen

connect

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh.client;2import java.io.IOException;3import org.testng.annotations.Test;4import com.consol.citrus.annotations.CitrusTest;5import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;6import com.consol.citrus.ssh.client.SshClient;7public class SshClientConnectTest extends TestNGCitrusTestRunner {8 public void sshClientConnectTest() {9 SshClient sshClient = new SshClient();10 sshClient.setHost("localhost");11 sshClient.setPort(22);12 sshClient.setUsername("username");13 sshClient.setPassword("password");14 sshClient.connect();15 }16}17package com.consol.citrus.ssh.client;18import java.io.IOException;19import org.testng.annotations.Test;20import com.consol.citrus.annotations.CitrusTest;21import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;22import com.consol.citrus.ssh.client.SshClient;23public class SshClientConnectTest extends TestNGCitrusTestRunner {24 public void sshClientConnectTest() {25 SshClient sshClient = new SshClient();26 sshClient.setHost("localhost");27 sshClient.setPort(22);28 sshClient.setUsername("username");29 sshClient.setPrivateKeyPath("src/test/resources/id_rsa");30 sshClient.connect();31 }32}33package com.consol.citrus.ssh.client;34import java.io.IOException;35import org.testng.annotations.Test;36import com.consol.citrus.annotations.CitrusTest;37import com.consol.c

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful