public class NTCmdSessionAgent extends BaseAgent implements LocalizedClient
NTCmdSessionAgent ntcmdSessionAgent = new NTCmdSessionAgent("10.0.64.198", "root", "logomania", 10000, 10000, null) String result = ntcmdSessionAgent.doExecuteCommand("d:") result = ntcmdSessionAgent.doExecuteCommand("cd Mercury") result = mercDirStr = ntcmdSessionAgent.doExecuteCommand("dir") ntcmdSessionAgent.disconnect()
New features:
Session mode:
ability to work in session mode (like telnet).
This means that upon agent creation, a remote shell is launched on
the remote host, and each subsequent command is run in that same shell
(Unlike the previous agent that only lauched a single command and terminated
the shell).
Remember that in session mode you are already connected to the remote shell
when the agent is created. This means that the command you send using
doExecuteCommand
is already done on the remote shell and not
locally (executing HPCmd to run a remote command). See: "Flags" section
for controlling the way HPCmd is launched in session mode. (for exaple, copying
file to the remote host on connection).
Output matching (matchers):
Working in session mode introduces a new problem to NTCMD, that is known
to the TelnetAgent. It is the ability to tell when the output of a certain
command is over.
The mechanism that is always used is command timeout (controlled by
timeout
parameter to Agent).
Since timeout can be large, we want another faster way of recognizing
the end of output if possible.
The matchers list java.util.List
property specified by
AgentConstants.PROP_AGENT_OUTPUT_MATCHERS
tell the agent to look for the strings
found in this list in the output, and declare end of output upon seeing
one of them (it is up to the client to figure out which one caused it).
By default, in session mode, the agent changes the prompt of the remote
shell to a known string and adds it to the matchers list.
This means that regular shell commands to give control back to the shell
when finished, will be properly diagnosed as over (as long as the special
prompt string doesn't appear in the output, which is unlikely).
At any rate, if no matchers are found, the command will wait the full timeout and then return.
HPCmd flags
HPCmd gives the option to leave the service running on the other side etc.
These flags are given to HPCmd when it starts. In order to use these flags
in session mode use the property
AgentConstants.PROP_NTCMD_AGENT_START_FLAGS
and put a java.util.List
of java.util.String
that reflect the flags as they would appear in the command line. The
agent will enumerate these strings and concatenate them with a space
character between each String in the list.
NOTE: working in session mode makes it more important to call
disconnect()
on the agent when done with it.
AgentConstants.PROP_NTCMD_AGENT_LAUNCH_TIMEOUT
property.
In addition, there are commands which are expected to take less or
more time than others. In session mode, each command can specify its
own timeout using the property
AgentConstants.PROP_AGENT_OVERRIDE_CMD_TIMEOUT
.
Remember to set it back to null to use the default timeout given to the agent.
Debugging:
Use PROP_NTCMD_AGENT_DEBUG_MODE property set to true to get more verbose debugging info on services log.
Trim command output whitespace:Use PROP_NTCMD_AGENT_TRIM_WHITESPACE property set to trim command output. The command output is first searches for the command itself (echoed) and cuts this echo from the output. Then the prompt which signals the end of the output is also cut from the result. After both these have been cut, this flag is checked and if true, String.trim() is applied to the resulting output before returning to the caller. This flag is true by default. If whitespace before and after the output is important, set this flag to false.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
START_HPCMD_SVC_UNDER_USER |
static java.lang.String |
STR_CONNECTED |
ANSI_ENCODING, ENCODING, OEM_ENCODING
Constructor and Description |
---|
NTCmdSessionAgent(java.lang.String ipAddress,
java.lang.String userName,
java.lang.String password,
int connectTimeout,
int defaultCmdTimeout,
java.util.Properties properties,
ClientsFramework framework) |
NTCmdSessionAgent(java.lang.String ipAddress,
java.lang.String userName,
java.lang.String password,
int connectTimeout,
java.util.Properties properties) |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
cleanupCommandOutput(java.lang.String command,
java.lang.String output,
boolean trimWhitespace) |
void |
connectSession() |
void |
disconnect()
Disconnects from the remote server.
|
java.lang.String |
doExecuteCommand(java.lang.String command)
Invokes a command on the remote server.
|
java.lang.String |
doExecuteCommand(java.lang.String command,
boolean timeoutAllowd) |
java.lang.String |
doExecuteCommand(java.lang.String command,
boolean timeoutAllowd,
boolean appendErrorBuffer) |
java.lang.String |
doExecuteCommand(java.lang.String command,
int cmdTimeout,
boolean readUntilTimeout) |
java.lang.String |
doExecuteCommand(java.lang.String command,
int cmdTimeout,
boolean timeoutAllowed,
boolean appendErrorBuffer) |
DynamicParserTable |
doTableCommand(java.lang.String command)
This operation is not supported on this agent
|
byte[] |
getLastCommandOutputBytes() |
java.lang.String |
getShellCmdSeperator() |
static void |
main(java.lang.String[] args) |
void |
setCharset(java.nio.charset.Charset charset) |
getCharsetName, getMaxCommandLength, getSudoCommands, getSudoPaths, parseTable, setMaxCommandLength, setProperty
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getProperty
public static final java.lang.String STR_CONNECTED
public static final java.lang.String START_HPCMD_SVC_UNDER_USER
public NTCmdSessionAgent(java.lang.String ipAddress, java.lang.String userName, java.lang.String password, int connectTimeout, java.util.Properties properties) throws java.lang.Exception
ipAddress
- xuserName
- xpassword
- xconnectTimeout
- xproperties
- xjava.lang.Exception
- xpublic NTCmdSessionAgent(java.lang.String ipAddress, java.lang.String userName, java.lang.String password, int connectTimeout, int defaultCmdTimeout, java.util.Properties properties, ClientsFramework framework) throws java.lang.Exception
ipAddress
- xuserName
- xpassword
- xconnectTimeout
- xdefaultCmdTimeout
- xproperties
- xjava.lang.Exception
- xpublic java.lang.String getShellCmdSeperator()
getShellCmdSeperator
in class BaseAgent
public java.lang.String doExecuteCommand(java.lang.String command) throws java.lang.Exception
BaseAgent
doExecuteCommand
in interface Agent
doExecuteCommand
in class BaseAgent
command
- the command to invokejava.lang.Exception
- when the command could not operate.public java.lang.String doExecuteCommand(java.lang.String command, int cmdTimeout, boolean readUntilTimeout) throws java.lang.Exception
doExecuteCommand
in class BaseAgent
java.lang.Exception
public java.lang.String doExecuteCommand(java.lang.String command, boolean timeoutAllowd) throws java.lang.Exception
doExecuteCommand
in class BaseAgent
java.lang.Exception
public java.lang.String doExecuteCommand(java.lang.String command, boolean timeoutAllowd, boolean appendErrorBuffer) throws java.lang.Exception
java.lang.Exception
public java.lang.String doExecuteCommand(java.lang.String command, int cmdTimeout, boolean timeoutAllowed, boolean appendErrorBuffer) throws java.lang.Exception
command
- - to executecmdTimeout
- -timeoutAllowed
- - usually timeout is enforced and an error msg
returns as the output buffer, discarding any input received until the timeout occured.
if this flag is true, any input received until the timeout is returned to the caller. This is
used for cases where the NTCMD has to issue commands that have no matchers (usually when opening
another session)appendErrorBuffer
- xjava.lang.Exception
- xpublic static java.lang.String cleanupCommandOutput(java.lang.String command, java.lang.String output, boolean trimWhitespace)
public void connectSession() throws java.lang.Exception
java.lang.Exception
public DynamicParserTable doTableCommand(java.lang.String command) throws java.lang.Exception
doTableCommand
in interface Agent
doTableCommand
in class BaseAgent
command
- the command to invokejava.lang.Exception
- when the command could not operate.Agent.doTableCommand(String)
public byte[] getLastCommandOutputBytes() throws java.lang.Exception
getLastCommandOutputBytes
in interface LocalizedClient
java.lang.Exception
public void setCharset(java.nio.charset.Charset charset) throws java.lang.Exception
setCharset
in interface LocalizedClient
java.lang.Exception
public void disconnect()
BaseAgent
disconnect
in interface Agent
disconnect
in class BaseAgent
Agent.disconnect()
public static void main(java.lang.String[] args)
Documentation Feedback
Copyright 2012 Micro Focus or one of its affiliates.