|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.foxsmart.csu.CiscoRouter
public class CiscoRouter
The CiscoRouter class represents a Cisco Router device and the underlying communication with the router.
This class should be used to obtain retrieve and update a router configuration. Note the default values for the "stop updates on config error", "rollback on config error", and "update NVRAM config" flags when making configuration updates. Note that this class will not work if the router prompt or the router hostname are set to the empty string or a string with only spaces. (e.g. 'hostname " "' or 'prompt ""').
| Field Summary | |
|---|---|
protected static java.lang.String |
ACCESS_DENIED
|
protected static char |
BREAK
|
protected static java.lang.String |
CONFIGURE_MEMORY
|
protected static java.lang.String |
CONFIGURE_TERMINAL
|
protected static java.lang.String |
CR
|
protected static char |
CR_CHAR
|
static int |
DEFAULT_NO_DATA_READ_DELAY
The number of 1/10 second increments that must pass without getting data before we consider a new data read complete. |
static int |
DEFAULT_PROTOCOL_TIMEOUT_SECONDS
The default operation timeout in seconds. |
protected int |
defaultNoDataReadDelay
|
protected java.lang.Integer |
defaultProtocolTimeout
|
protected static java.lang.String |
ENABLE_COMMAND
|
protected static java.lang.String |
END
|
protected static java.util.List<java.lang.String> |
errorMessages
|
static java.lang.String |
FOXSMART_PROXY_PASSWORD_KEY
|
static java.lang.String |
FOXSMART_PROXY_USERNAME_KEY
|
protected static java.lang.String |
HELP
|
protected org.apache.commons.logging.Log |
log
|
protected static java.lang.String |
NO
|
protected static java.lang.String |
NO_PASSWORD_SET
|
protected static java.lang.String |
NO_TERM_LENGTH_COMMAND
|
protected static java.lang.String |
NO_TERM_WIDTH_COMMAND
|
protected static java.lang.String |
PASSWORD_PROMPT
|
static java.lang.String |
PROXY_HOST_KEY
|
static java.lang.String |
PROXY_PORT_KEY
|
static java.lang.String |
PROXY_SET_KEY
|
protected static java.lang.String |
SERVICE_PROMPT_CONFIG
|
protected static java.lang.String |
SET_CLOCK_COMMAND
|
protected static java.lang.String |
SHOW_CLOCK_COMMAND
|
protected static java.lang.String |
SHOW_RUN_CONFIG_COMMAND
|
protected static java.lang.String |
SHOW_VERSION_COMMAND
|
protected static java.lang.String |
UNRECOGNIZED_COMMAND
|
protected static java.lang.String |
USERNAME_PROMPT
|
protected static java.util.List<java.lang.String> |
warningMessages
|
protected static java.lang.String |
WRITE_MEMORY
|
| Constructor Summary | |
|---|---|
CiscoRouter(java.lang.String hostname,
java.lang.String username,
java.lang.String password,
java.lang.String enablePassword)
Constructs a Cisco Router object. |
|
| Method Summary | |
|---|---|
protected void |
checkAccess(boolean enableRequired)
Checks that we are connected to the router and that we have the proper access to run a command. |
protected void |
checkForRecognizedCommand(java.lang.String command)
Checks to see if the command is recognized. |
protected void |
connect()
Makes a connection with the router and sets the active protocol handler. |
protected void |
ensureActiveProtocolNotPresent()
Checks to ensure an active protocol handler is not present. |
protected void |
ensureActiveProtocolPresent()
Checks to ensure an active protocol handler is present and is a supported protocol. |
java.lang.String |
expectPrompt(java.lang.String match,
boolean includeMatch,
java.lang.Integer timeout)
Reads data from the device until the expected string is found. |
ProtocolHandler.Protocol |
getActiveProtocol()
Gets the active protocol that is being used while connected to the router. |
ProtocolHandler |
getActiveProtocolHandler()
Gets the currently active protocol handler. |
java.lang.String |
getEnablePassword()
Gets the enable password. |
boolean |
getEnablePasswordUsed()
Gets whether the enable password was used for authentication. |
static java.util.List<java.lang.String> |
getErrorMessageList()
Returns the list of error messages that don't begin with a "%" character. |
static java.util.List<java.lang.String> |
getGlobalPackagesStatic()
Gets the global list of configuration packages via a static convenience method. |
boolean |
getHasEnableAccess()
Determines whether enable access is available when logged in to the router. |
java.lang.String |
getHostname()
Gets the hostname of the router. |
java.lang.String |
getPassword()
Gets the standard password. |
boolean |
getPasswordUsed()
Gets whether the password was used for authentication. |
java.lang.String |
getPrompt()
Gets the router prompt after the login is successful. |
java.util.List<ProtocolHandler> |
getProtocolHandlers()
Gets the list of protocol handlers. |
boolean |
getRollbackOnConfigErrorFlag()
Gets the "rollback on config error" flag. |
CiscoRouterConfig |
getRunningConfiguration()
Gets the router running configuration as a CiscoRouterConfig object. |
java.lang.String |
getRunningConfigurationAsString()
Gets the router running configuration as the original IOS string. |
boolean |
getStopUpdatesOnConfigErrorFlag()
Gets the "stop updates on config error" flag. |
java.util.Calendar |
getTime()
Gets the router date and time. |
Commands |
getUpdateConfiglet(CiscoRouterConfig ciscoRouterConfig)
Gets the configlet commands that can be used to update a router configuration based on the passed in new router configuration. |
boolean |
getUpdateNvRamConfigFlag()
Gets the "update NVRAM config" flag. |
java.lang.String |
getUsername()
Gets the username of the router. |
boolean |
getUsernameUsed()
Gets whether the username was used for authentication. |
java.lang.String |
getVersionInfo()
Gets the router version information. |
static java.util.List<java.lang.String> |
getWarningMessageList()
Returns the list of warning messages that begin with a "%" character. |
void |
login()
Logs into the router and establishes enable capabilities if an enable password is present. |
void |
logout()
Logs out of the router. |
protected void |
sendCommand(java.lang.String command)
Sends a command to the router. |
protected void |
sendCommand(java.lang.String command,
boolean containsPassword)
Sends a command to the router. |
java.lang.String |
sendCommandWithResults(java.lang.String command)
Sends a command to the router and returns the results of the command being executed. |
java.lang.String |
sendCommandWithResults(java.lang.String command,
boolean checkForRecognizedCommand)
Sends a command to the router and returns the results of the command being executed. |
protected com.foxsmart.csu.PromptAndResponse |
sendConfigCommandWithResults(java.lang.String command,
java.lang.String hostname)
Sends a config command to the router and returns the results of the command being executed. |
protected void |
setActiveProtocolHandler(ProtocolHandler protocolHandler)
Sets the active protocol handler. |
void |
setDefaultNoDataReadDelay(int delay)
Modifies the default no data read delay in 1/10 seconds for all protocol handlers. |
void |
setDefaultProtocolTimeout(java.lang.Integer timeout)
Modifies the default operation protocol timeout value for all protocol handlers. |
static void |
setGlobalPackagesStatic(java.util.List<java.lang.String> globalPackages)
Sets the global list of configuration packages via a static convenience method. |
void |
setProtocolHandlers(java.util.List<ProtocolHandler> protocolHandlers)
Sets the list of protocol handlers to use when connecting to the router in order of preference. |
void |
setProtocolSupport(boolean useSsh,
boolean useTelnet,
boolean useHttp,
ProtocolHandler.Protocol preferredProtocol)
Sets the supported protocols for the router. |
void |
setRollbackOnConfigErrorFlag(boolean rollbackOnConfigErrorFlag)
Sets the "rollback on config error" flag. |
void |
setStopUpdatesOnConfigErrorFlag(boolean stopUpdatesOnConfigErrorFlag)
Sets the "stop updates on config error" flag. |
void |
setTime(java.util.Calendar calendar)
Sets the router date and time in Coordinated Universal Time (UTC). |
void |
setUpdateNvRamConfigFlag(boolean updateNvRamConfigFlag)
Sets the "update NVRAM config" flag. |
protected void |
unsupportedProtocolHandler()
Throws a runtime exception indicating that the active protocol handler does not support the requested operation. |
ConfigUpdateResult |
writeConfiglet(Commands commands)
Writes the specified commands to the router. |
ConfigUpdateResult |
writeConfiglet(java.lang.String configlet)
Writes the specified configlet commands to the router. |
ConfigUpdateResult |
writeConfiguration(CiscoRouterConfig newRouterConfig)
Writes an updated configuration to the router. |
ConfigUpdateResult |
writeConfigurationAsString(java.lang.String routerConfig)
Writes an updated configuration as a full IOS configuration string to the router. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected org.apache.commons.logging.Log log
public static final int DEFAULT_PROTOCOL_TIMEOUT_SECONDS
public static final int DEFAULT_NO_DATA_READ_DELAY
protected java.lang.Integer defaultProtocolTimeout
protected int defaultNoDataReadDelay
protected static final java.lang.String CR
protected static final char CR_CHAR
protected static final java.lang.String HELP
protected static final char BREAK
protected static final java.lang.String USERNAME_PROMPT
protected static final java.lang.String PASSWORD_PROMPT
protected static final java.lang.String NO_PASSWORD_SET
protected static final java.lang.String ACCESS_DENIED
protected static final java.lang.String UNRECOGNIZED_COMMAND
protected static final java.lang.String ENABLE_COMMAND
protected static final java.lang.String NO_TERM_WIDTH_COMMAND
protected static final java.lang.String NO_TERM_LENGTH_COMMAND
protected static final java.lang.String SHOW_RUN_CONFIG_COMMAND
protected static final java.lang.String SHOW_VERSION_COMMAND
protected static final java.lang.String SHOW_CLOCK_COMMAND
protected static final java.lang.String SET_CLOCK_COMMAND
protected static final java.lang.String CONFIGURE_TERMINAL
protected static final java.lang.String CONFIGURE_MEMORY
protected static final java.lang.String WRITE_MEMORY
protected static final java.lang.String SERVICE_PROMPT_CONFIG
protected static final java.lang.String END
protected static final java.lang.String NO
public static final java.lang.String FOXSMART_PROXY_USERNAME_KEY
public static final java.lang.String FOXSMART_PROXY_PASSWORD_KEY
public static final java.lang.String PROXY_SET_KEY
public static final java.lang.String PROXY_HOST_KEY
public static final java.lang.String PROXY_PORT_KEY
protected static java.util.List<java.lang.String> errorMessages
protected static java.util.List<java.lang.String> warningMessages
| Constructor Detail |
|---|
public CiscoRouter(java.lang.String hostname,
java.lang.String username,
java.lang.String password,
java.lang.String enablePassword)
throws java.lang.Exception
hostname - The hostname of the router. This parameter is required.username - The username that is used to connect to the router. This parameter is required only when aaa
new-model is used.password - The standard password that is used to connect to the router. This parameter is optional when aaa
new-model is used and the username has been configured to not require a password.enablePassword - The enable password to gain extra permissions on the router. This paramter is optional. If
an enable password is not provided, router commands that require this access will throw a security exception.
java.lang.Exception - if any licensing problems are encountered.| Method Detail |
|---|
public static java.util.List<java.lang.String> getErrorMessageList()
public static java.util.List<java.lang.String> getWarningMessageList()
public void setProtocolSupport(boolean useSsh,
boolean useTelnet,
boolean useHttp,
ProtocolHandler.Protocol preferredProtocol)
throws java.lang.Exception
useSsh - True if the SSH protocol should be used to connect to the router or false if not.useTelnet - True if the Telnet protocol should be used to connect to the router or false if not.useHttp - True if the HTTP protocol should be used to connect to the router or false if not.preferredProtocol - Optionally specifies which protocol (SSH, Telnet, or HTTP) is the preferred protocol for
this router. If null is specified, SSH will be the preferred protocol.
java.lang.Exception - if a protocolpublic java.util.List<ProtocolHandler> getProtocolHandlers()
public void setProtocolHandlers(java.util.List<ProtocolHandler> protocolHandlers)
protocolHandlers - the list of protocol handlers.public ProtocolHandler.Protocol getActiveProtocol()
protected void ensureActiveProtocolPresent()
throws java.lang.RuntimeException
java.lang.RuntimeException - if an active protocol handler is not present.
protected void ensureActiveProtocolNotPresent()
throws java.lang.RuntimeException
java.lang.RuntimeException - if an active protocol handler is present.public void setDefaultProtocolTimeout(java.lang.Integer timeout)
timeout - The default timeout value in seconds to wait for the data to be returned. If null is passed, no
timeout will be used. Beware that if an expected match string is not found or if the device doesn't respond, the
expect method will not return.public void setDefaultNoDataReadDelay(int delay)
delay - The default no data read delay.public ProtocolHandler getActiveProtocolHandler()
protected void setActiveProtocolHandler(ProtocolHandler protocolHandler)
protocolHandler - the active protocol handler.
protected void connect()
throws java.lang.Exception
java.net.UnknownHostException - If the hostname cannot be resolved.
java.net.ConnectException - if a connection couldn't be established with the destination device.
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
AuthenticationFailedException - when the username and/or password specified did not provide access to the
router.
java.lang.Exception - if any other unexpected error occurs.
public void login()
throws java.lang.Exception
java.net.UnknownHostException - If the hostname cannot be resolved.
java.net.ConnectException - if a connection couldn't be established with the destination device.
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
AuthenticationFailedException - when the username and/or password specified did not provide access to the
router.
InvalidEnablePasswordException - when the enable password did not provide enable access.
java.lang.Exception - if any other unexpected error occurs.
public void logout()
throws java.io.IOException
java.io.IOException - if an I/O error occurs when closing the socket to the router.public boolean getUsernameUsed()
public boolean getPasswordUsed()
public boolean getEnablePasswordUsed()
public CiscoRouterConfig getRunningConfiguration()
throws java.lang.Exception
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
EnablerAccessRequiredException - if enabler access has not been obtained. The getHasEnableAccess method can
be used to determine if enabler access is currently present.
InvalidConfigurationException - if the IOS configuration can not be parsed for any reason.
java.lang.Exception - if any other unexpected error occurs.
public java.lang.String getRunningConfigurationAsString()
throws java.lang.Exception
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
java.lang.Exception - if any other unexpected error occurs.
public ConfigUpdateResult writeConfiguration(CiscoRouterConfig newRouterConfig)
throws java.lang.Exception
newRouterConfig - The new target router configuration.
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
EnablerAccessRequiredException - if enabler access has not been obtained. The getHasEnableAccess method can
be used to determine if enabler access is currently present.
InvalidConfigurationException - if the router configuration is invalid for any reason.
java.lang.Exception - if any other unexpected error occurs.
public ConfigUpdateResult writeConfigurationAsString(java.lang.String routerConfig)
throws java.lang.Exception
routerConfig - The new target router configuration as a complete IOS string.
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
EnablerAccessRequiredException - if enabler access has not been obtained. The getHasEnableAccess method can
be used to determine if enabler access is currently present.
InvalidConfigurationException - if the router configuration is invalid for any reason.
java.lang.Exception - if any other unexpected error occurs.
public Commands getUpdateConfiglet(CiscoRouterConfig ciscoRouterConfig)
throws java.lang.Exception
ciscoRouterConfig - The new target router configuration.
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
EnablerAccessRequiredException - if enabler access has not been obtained. The getHasEnableAccess method can
be used to determine if enabler access is currently present.
InvalidConfigurationException - if the router configuration is invalid for any reason.
java.lang.Exception - if any other unexpected error occurs.
public ConfigUpdateResult writeConfiglet(java.lang.String configlet)
throws java.lang.Exception
configlet - The configlet to write to the router.
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out reading or writing data from/to the router.
EnablerAccessRequiredException - if enabler access has not been obtained. The getHasEnableAccess method can
be used to determine if enabler access is currently present.
InvalidConfigurationException - if the router configuration is invalid for any reason.
java.lang.Exception - if any other unexpected error occurs.
public ConfigUpdateResult writeConfiglet(Commands commands)
throws java.lang.Exception
commands - The commands to send to the router.
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out reading or writing data from/to the router.
EnablerAccessRequiredException - if enabler access has not been obtained. The getHasEnableAccess method can
be used to determine if enabler access is currently present.
InvalidConfigurationException - if the router configuration is invalid for any reason.
java.lang.Exception - if any other unexpected error occurs.
public java.lang.String getVersionInfo()
throws java.lang.Exception
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
java.lang.Exception - if any other unexpected error occurs.
public java.util.Calendar getTime()
throws java.lang.Exception
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
java.lang.Exception - if any other unexpected error occurs.
public void setTime(java.util.Calendar calendar)
throws java.lang.Exception
calendar - The new date/time to set
java.io.IOException - If the socket could not be opened or there was a problem communicating with the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
java.lang.Exception - if any other unexpected error occurs.public java.lang.String getPrompt()
protected void checkAccess(boolean enableRequired)
throws java.io.IOException,
EnablerAccessRequiredException
enableRequired - If true, enable access is required to run the command. Otherwise, only normal access is
required.
java.io.IOException - if we are not connected to the router.
EnablerAccessRequiredException - if enabler access is required, but not established.public boolean getHasEnableAccess()
protected void sendCommand(java.lang.String command)
throws java.lang.Exception
command - The command to send to the router.
java.lang.Exception - if there was a problem running the command.
protected void sendCommand(java.lang.String command,
boolean containsPassword)
throws java.lang.Exception
command - The command to send to the router.containsPassword - true if the command contains a password or false if not. Send true to cause the password
to only be logged in "trace" mode.
java.lang.Exception - if there was a problem running the command.
public java.lang.String sendCommandWithResults(java.lang.String command)
throws java.lang.Exception
command - The command to send to the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
java.io.IOException - if there was a problem running the command.
UnrecognizedCommandException - if the command is not recognized by the router. This can be because the
command is invalid or because the logged in user does not have the appropriate access.
java.lang.Exception - if the returned data buffer contains unexpected data present or if any other unknown error was
encountered.
public java.lang.String sendCommandWithResults(java.lang.String command,
boolean checkForRecognizedCommand)
throws java.lang.Exception
command - The command to send to the router.checkForRecognizedCommand - If true, the command to be issued will be checked to see if it is recognized.
Otherwise, the command will be sent as is without being checked first.
ThreadTimeoutException - if we timed out while waiting for data from the router.
java.io.IOException - if there was a problem running the command.
UnrecognizedCommandException - if the command is not recognized by the router. This can be because the
command is invalid or because the logged in user does not have the appropriate access.
java.lang.Exception - if the returned data buffer contains unexpected data present or if any other unknown error was
encountered.
protected void checkForRecognizedCommand(java.lang.String command)
throws java.lang.Exception
command - the command to check.
UnrecognizedCommandException - if the command is not recognized by the router. This can be because the
command is invalid or because the logged in user does not have the appropriate access.
ThreadTimeoutException - if the expected string was not found within the passed in timeout period.
java.io.IOException - if there was an error reading data from the device or sending data to the device.
java.lang.Exception - if any other unexpected error occurs.
protected com.foxsmart.csu.PromptAndResponse sendConfigCommandWithResults(java.lang.String command,
java.lang.String hostname)
throws java.lang.Exception
command - The command to send to the router.hostname - The hostname of the router.
ThreadTimeoutException - if we timed out while waiting for data from the router.
java.lang.Exception - if the returned data buffer contains unexpected data present or if any other unknown error was
encountered.
java.io.IOException - if there was a problem running the command.
public java.lang.String expectPrompt(java.lang.String match,
boolean includeMatch,
java.lang.Integer timeout)
throws java.lang.Exception
match - The string to search for.includeMatch - If true, the match string is returned in the resulting data string. If false, the match
string is not returned as part of the resultant string.timeout - The timeout value in seconds to wait for the data to be returned. If null is passed, no timeout
will be used. Beware that if the expected match string is not found or if the device doesn't respond, this method
will not return.
ThreadTimeoutException - if the expected string was not found within the passed in timeout period.
java.io.IOException - if there was an error reading data from the device.
java.lang.Exception - if any other unexpected error occurs.
protected void unsupportedProtocolHandler()
throws java.lang.RuntimeException
java.lang.RuntimeException - always.public java.lang.String getHostname()
public java.lang.String getUsername()
public java.lang.String getPassword()
public java.lang.String getEnablePassword()
public static java.util.List<java.lang.String> getGlobalPackagesStatic()
public static void setGlobalPackagesStatic(java.util.List<java.lang.String> globalPackages)
globalPackages - the global list of packages.public void setStopUpdatesOnConfigErrorFlag(boolean stopUpdatesOnConfigErrorFlag)
stopUpdatesOnConfigErrorFlag - The "stop updates on config error" flag.public boolean getStopUpdatesOnConfigErrorFlag()
public void setRollbackOnConfigErrorFlag(boolean rollbackOnConfigErrorFlag)
rollbackOnConfigErrorFlag - The "rollback on config error" flag.public boolean getRollbackOnConfigErrorFlag()
public void setUpdateNvRamConfigFlag(boolean updateNvRamConfigFlag)
updateNvRamConfigFlag - The "update NVRAM config" flag.public boolean getUpdateNvRamConfigFlag()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||