Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ trait Shell implements SessionExtension {
assert settings != null, 'settings must not be null'
operations.shell(operationSettings + new OperationSettings(settings))
}

/**
* Performs an expect-like session
* @param interaction sequence of 'send' and 'expect' instructions
*/
void shellExpect(Closure interaction){
operations.shellExpect(interaction)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import org.hidetake.groovy.ssh.extension.settings.LocalPortForwardSettings
import org.hidetake.groovy.ssh.extension.settings.RemotePortForwardSettings
import org.hidetake.groovy.ssh.interaction.Interaction
import org.hidetake.groovy.ssh.session.BadExitStatusException
import org.hidetake.groovy.ssh.operation.expect.Expect
import com.jcraft.jsch.ChannelShell

import static org.hidetake.groovy.ssh.util.Utility.callWithDelegate

Expand Down Expand Up @@ -208,4 +210,20 @@ class DefaultOperations implements Operations {
break
}
}


@Override
void shellExpect(Closure interaction) {
ChannelShell channel = connection.createShellChannel(null)
Expect expectObj = new Expect(channel.getInputStream(), channel.getOutputStream())
channel.connect()
interaction.delegate = expectObj
try {
interaction.call()
}
finally {
expectObj.close()
channel.disconnect()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.hidetake.groovy.ssh.extension.settings.LocalPortForwardSettings
import org.hidetake.groovy.ssh.core.settings.OperationSettings
import org.hidetake.groovy.ssh.core.Remote
import org.hidetake.groovy.ssh.extension.settings.RemotePortForwardSettings
import org.hidetake.groovy.ssh.operation.expect.DryRunExpect

/**
* Dry-run implementation of {@link Operations}.
Expand All @@ -25,6 +26,13 @@ class DryRunOperations implements Operations {
log.info("[dry-run] Executing a shell")
}

@Override
void shellExpect(Closure interaction) {
DryRunExpect expectObj = new DryRunExpect()
interaction.delegate = expectObj
interaction.call()
}

@Override
String execute(OperationSettings settings, String command, Closure callback) {
log.info("[dry-run] Executing the command ($command)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ interface Operations {
* @return result of the closure
*/
def sftp(@DelegatesTo(SftpOperations) Closure closure)

void shellExpect(Closure interaction)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.hidetake.groovy.ssh.operation.expect

import groovy.util.logging.Slf4j

/**
* Dry-run implementation of {@link Expect}.
*
* @author Romano Zabini
*/
@Slf4j
class DryRunExpect {

def expectOrThrow(int timeout, Object... patterns){
log.info("waiting {} seconds for: {}", timeout, patterns)
}

def send(String command){
log.info("sending command: {}", command)
}
}
Loading