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

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

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 }...

Full Screen

Full Screen

Source:SshClientTest.java Github

copy

Full Screen

...64 }65 @Test(expectedExceptions = CitrusRuntimeException.class,expectedExceptionsMessageRegExp = ".*user.*")66 public void noUser() {67 client.getEndpointConfiguration().setUser(null);68 send();69 }70 @Test(expectedExceptions = CitrusRuntimeException.class,expectedExceptionsMessageRegExp = ".*knownHosts.*")71 public void strictHostCheckingWithoutKnownHosts() throws JSchException {72 strictHostChecking(true, null);73 send();74 }75 @Test(expectedExceptions = CitrusRuntimeException.class,expectedExceptionsMessageRegExp = ".*blaHosts.*")76 public void strictHostCheckingWithFaultyKnownHosts() throws JSchException {77 strictHostChecking(true, "classpath:/com/consol/citrus/ssh/blaHosts");78 send();79 }80 @Test(expectedExceptions = CitrusRuntimeException.class,expectedExceptionsMessageRegExp = ".*/does/not/exist.*")81 public void strictHostCheckingWithFaultyKnownHosts2() throws JSchException {82 strictHostChecking(true, "/file/that/does/not/exist");83 send();84 }85 @Test86 public void strictHostCheckingWithKnownHosts() throws JSchException, IOException {87 strictHostChecking(true, "classpath:com/consol/citrus/ssh/knownHosts");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);...

Full Screen

Full Screen

send

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.SshMessageType;4import org.springframework.context.support.ClassPathXmlApplicationContext;5public class 3 {6 public static void main(String[] args) {7 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");8 SshClient sshClient = context.getBean("sshClient", SshClient.class);9 SshMessage request = new SshMessage("ls");10 SshMessage response = sshClient.send(request, SshMessageType.RESULT);11 System.out.println(response.getPayload());12 }13}

Full Screen

Full Screen

send

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh.client;2import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;3import org.testng.annotations.Test;4public class SshClientJavaITest extends TestNGCitrusTestRunner {5 public void testSshClient() {6 variable("user", "user");7 variable("password", "password");8 variable("host", "localhost");9 variable("port", "22");10 variable("command", "ls");11 variable("commandOutput", "file1 file2");12 parallel().actions(13 ssh().client("sshClient")14 .send("command")15 .receive("commandOutput")16 );17 }18}19package com.consol.citrus.ssh.client;20import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;21import org.testng.annotations.Test;22public class SshClientJavaITest extends TestNGCitrusTestRunner {23 public void testSshClient() {24 variable("user", "user");25 variable("password", "password");26 variable("host", "localhost");27 variable("port", "22");28 variable("command", "ls");29 variable("commandOutput", "file1 file2");30 parallel().actions(31 ssh().client("sshClient")32 .send("command")33 .receive("commandOutput")34 );35 }36}37package com.consol.citrus.ssh.client;38import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;39import org.testng.annotations.Test;40public class SshClientJavaITest extends TestNGCitrusTestRunner {41 public void testSshClient() {42 variable("user", "user");43 variable("password", "password");44 variable("host", "localhost");45 variable("port", "22");46 variable("command", "ls");47 variable("commandOutput", "file1 file2");48 parallel().actions(49 ssh().client("sshClient")50 .send("command")51 .receive("commandOutput")52 );53 }54}

Full Screen

Full Screen

send

Using AI Code Generation

copy

Full Screen

1public class 3 {2 public static void main(String[] args) {3 SshClient sshClient = new SshClient();4 sshClient.setHost("localhost");5 sshClient.setUser("user");6 sshClient.setPassword("password");7 sshClient.setDefaultTimeout(5000L);8 sshClient.connect();9 sshClient.send("ls -l");10 sshClient.disconnect();11 }12}13public class 4 {14 public static void main(String[] args) {15 SshClient sshClient = new SshClient();16 sshClient.setHost("localhost");17 sshClient.setUser("user");18 sshClient.setPassword("password");19 sshClient.setDefaultTimeout(5000L);20 sshClient.connect();21 sshClient.send("ls -l", new SshMessageCallback() {22 public void onMessage(SshMessage message) {23 System.out.println("Received message: " + message);24 }25 });26 sshClient.disconnect();27 }28}29public class 5 {30 public static void main(String[] args) {31 SshClient sshClient = new SshClient();32 sshClient.setHost("localhost");33 sshClient.setUser("user");34 sshClient.setPassword("password");35 sshClient.setDefaultTimeout(5000L);36 sshClient.connect();37 sshClient.send("ls -l", new SshMessageCallback() {38 public void onMessage(SshMessage message) {39 System.out.println("Received message: " + message);40 }41 }, new SshMessageCallback() {42 public void onMessage(SshMessage message) {43 System.out.println("Received error message: " + message);44 }45 });46 sshClient.disconnect();47 }48}49public class 6 {50 public static void main(String[] args) {51 SshClient sshClient = new SshClient();52 sshClient.setHost("localhost");53 sshClient.setUser("user");54 sshClient.setPassword("password");55 sshClient.setDefaultTimeout(500

Full Screen

Full Screen

send

Using AI Code Generation

copy

Full Screen

1public class 3 {2 private SshClient sshClient;3 public void send() {4 sshClient.send("command");5 }6}7public class 4 {8 private SshClient sshClient;9 public void send() {10 sshClient.send("command", new SshMessageCallback() {11 public void doWithMessage(SshMessage message) {12 message.setCommand("command");13 message.setTimeout(10000L);14 message.setEncoding("UTF-8");15 }16 });17 }18}19public class 5 {20 private SshClient sshClient;21 public void send() {22 sshClient.send(new SshMessage("command"));23 }24}25public class 6 {26 private SshClient sshClient;27 public void send() {28 sshClient.send(new SshMessage("command"), new SshMessageCallback() {29 public void doWithMessage(SshMessage message) {30 message.setCommand("command");31 message.setTimeout(10000L);32 message.setEncoding("UTF-8");33 }34 });35 }36}37public class 7 {38 private SshClient sshClient;39 public void send() {40 sshClient.send(new SshMessage("command"), new SshMessageValidator() {41 public void validate(SshMessage message) {42 }43 });44 }45}46public class 8 {47 private SshClient sshClient;48 public void send() {

Full Screen

Full Screen

send

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import org.springframework.beans.factory.annotation.Autowired;4import org.testng.annotations.Test;5public class SshSampleIT extends TestNGCitrusTestDesigner {6 private SshClient sshClient;7 public void testSshSample() {8 send(sshClient).command("ls -l");9 receive(sshClient).message("total 0");10 }11}12package com.consol.citrus.ssh;13import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;14import org.springframework.beans.factory.annotation.Autowired;15import org.testng.annotations.Test;16public class SshSampleIT extends TestNGCitrusTestDesigner {17 private SshClient sshClient;18 public void testSshSample() {19 send(sshClient).command("ls -l");20 receive(sshClient).message("total 0");21 send(sshClient).command("ls -l");22 receive(sshClient).message("total 0");23 }24}25package com.consol.citrus.ssh;26import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;27import org.springframework.beans.factory.annotation.Autowired;28import org.testng.annotations.Test;29public class SshSampleIT extends TestNGCitrusTestDesigner {30 private SshClient sshClient;31 public void testSshSample() {32 send(sshClient).command("ls -l");33 receive(sshClient).message("total 0");34 send(sshClient).command("ls -l");35 receive(sshClient).message("total 0");36 }37}38package com.consol.citrus.ssh;39import com.consol.cit

Full Screen

Full Screen

send

Using AI Code Generation

copy

Full Screen

1public class 3 {2 private SshClient sshClient;3 public void testSshClient() {4 sshClient.send("pwd");5 }6}7public class 4 {8 private SshClient sshClient;9 public void testSshClient() {10 sshClient.send("pwd");11 }12}13public class 5 {14 private SshClient sshClient;15 public void testSshClient() {16 sshClient.send("pwd");17 }18}19public class 6 {20 private SshClient sshClient;21 public void testSshClient() {22 sshClient.send("pwd");23 }24}25public class 7 {26 private SshClient sshClient;27 public void testSshClient() {28 sshClient.send("pwd");29 }30}31public class 8 {32 private SshClient sshClient;33 public void testSshClient() {34 sshClient.send("pwd");35 }36}37public class 9 {38 private SshClient sshClient;

Full Screen

Full Screen

send

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh.sshclient;2import java.io.IOException;3import java.util.Arrays;4import org.testng.annotations.Test;5import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;6import com.consol.citrus.exceptions.CitrusRuntimeException;7import com.consol.citrus.ssh.client.SshClient;8import com.consol.citrus.ssh.server.SshServer;9public class SshClientTest extends TestNGCitrusTestDesigner {10 public void sshClientTest() {11 SshServer sshServer = new SshServer();12 sshServer.setPort(2222);13 sshServer.setHost("localhost");14 sshServer.setKeyPath("src/test/resources/ssh/localhost_rsa");15 sshServer.setKeyPassword("localhost");16 sshServer.setCommand("echo Hello World!");17 sshServer.start();18 SshClient sshClient = new SshClient();19 sshClient.setPort(2222);20 sshClient.setHost("localhost");21 sshClient.setKeyPath("src/test/resources/ssh/localhost_rsa");22 sshClient.setKeyPassword("localhost");23 sshClient.setCommand("echo Hello World!");24 sshClient.start();25 sshClient.send("echo Hello World!");26 sshServer.stop();27 }28}29package com.consol.citrus.ssh.sshserver;30import java.io.IOException;31import java.util.Arrays;32import org.testng.annotations.Test;33import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;34import com.consol.citrus.exceptions.CitrusRuntimeException;35import com.consol.citrus.ssh.client.SshClient;36import com.consol.c

Full Screen

Full Screen

send

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import org.springframework.beans.factory.annotation.Autowired;4import org.testng.annotations.Test;5public class SshJavaIT extends TestNGCitrusTestDesigner {6 private SshClient sshClient;7 public void sshJavaIT() {8 variable("command", "ls -l");9 variable("expected", "total 0");10 send(sshClient)11 .command("${command}");12 receive(sshClient)13 .payload("${expected}");14 }15}16package com.consol.citrus.ssh;17import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;18import org.springframework.beans.factory.annotation.Autowired;19import org.testng.annotations.Test;20public class SshJavaIT extends TestNGCitrusTestDesigner {21 private SshClient sshClient;22 public void sshJavaIT() {23 variable("command", "ls -l");24 variable("expected", "total 0");25 send(sshClient)26 .command("${command}");27 receive(sshClient)28 .payload("${expected}");29 }30}31package com.consol.citrus.ssh;32import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;33import org.springframework.beans.factory.annotation.Autowired;34import org.testng.annotations.Test;35public class SshJavaIT extends TestNGCitrusTestDesigner {36 private SshClient sshClient;37 public void sshJavaIT() {38 variable("command", "ls -l");39 variable("expected", "total 0");40 send(sshClient)41 .command("${command}");42 receive(sshClient)43 .payload("${expected}");44 }45}

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