Myna. Shell

runs an interactive shell

Example

shell = new Myna.Shell({
command:"ssh://mark@192.168.1.112",
timeout:3,
})

shell.sendLine("ls");
shell.waitForPrompt()
shell.sendLine("cd /tmp")
shell.waitForPrompt()
shell.sendLine("/bin/ls")
shell.waitForPrompt()
shell.sendLine("exit")
shell.waitForClose();
Myna.println(shell.getResponseText());
Myna.println(shell.getErrorText());
Summary
Myna. Shellruns an interactive shell
Functions
Myna. ShellConstructor function for Shell class
Myna. Shell. escapeShEscapes a string to a quoted parameter for sh family shells
Myna. Shell. escapeCmdEscapes a string to a quoted parameter for windows cmd shell
sendsends input to the shell
sendLinesends input to the shell, including a hard return
waitForwaits for a text response from the process, or until a timeout occurs
waitForErrwaits for a text response from the process on the error stream, or until a timeout occurs
waitForPromptwaits for a command prompt response from the process, or until a timeout occurs
waitForClosewaits for a shell to close, or timeout
getExitValuereturns exit value of the shell, or throws an exception if shell is still running
getResponseText()returns the current stdout text buffer
getErrorText()returns the current stderr text buffer

Functions

Myna. Shell

Myna.Shell = function(options)

Constructor function for Shell class

Parameters

optionsObject that describes the options for this shell.  See below.  Options:
commandString Command to execute.  May be ssh://user@host to initiate an ssh session.
timeoutOptional default 30 seconds Integer default number of seconds to wait for expected output
promptOptional default ‘>’ on windows ‘$’ on others prompt to match via waitForPrompt
escapeFnOptional, default Myna.Shell.escapeCmd on windows or Myna.Shell.escapeSh on others The escape function to use for replacements.  See send and sendLine
privateKeyOptional, default file://{homedir}/.ssh/id_rsa path to private key for SSH sessions
publicKeyOptional, default {privateKey}.pub path to public key for SSH sessions
passwordOptional If connecting via ssh, a password can be sent.

Myna. Shell. escapeSh

Escapes a string to a quoted parameter for sh family shells

Parameters

text

Returns

text formatted as an escaped, quoted string

Example

This string is just a mess in bourne shell

$PATH bob!;echo `echo dude` sally's &

This function will escape it to

'$PATH bob!;echo `echo dude` sally'"'"'s &'

which will cause it to be interpreted as a single string literal parameter

Myna. Shell. escapeCmd

Escapes a string to a quoted parameter for windows cmd shell

Parameters

text

Returns

text formatted as an escaped, quoted string

Example

This string

%PATH% > tmp

becomes

"^%PATH^% ^> tmp"

which will cause it to be interpreted as a single string literal parameter

send

Myna.Shell.prototype.send=function(text,
replacements)

sends input to the shell

Parameters

texttext to send to the process
replacementsOptional Array of strings or JS object If defined, values will be sanitized via <escapeFn> and then applied to text as per String.format.

Note

This class can be easily exploited by passing system commands via web forms.  All user input should be checked for potential command injection before bing passed to a shell

Example

shell.send("echo {body} | mail -s {subject} {userEmail}\n",$req.rawData);
shell.waitForClose(10)

This will sanitize the “subject”,”body” and “userEmail” parameters before merging them into test to send to the shell.  This way if a user types “&& rm -rf /” as the “body” parameter, this will be sent as the body of the email rather than erasing the server’s hard drive.

returns this shell Object

sendLine

Myna.Shell.prototype.sendLine=function(text,
replacements)

sends input to the shell, including a hard return

Parameters

texttext to send to the process
replacementsOptional Array of strings or JS object If defined, values will be sanitized via <escapeFn> and then applied to text as per String.format.

Note

This class can be easily exploited by passing system commands via web forms.  All user input should be checked for potential command injection before bing passed to a shell

Example

shell.send("echo {body} | mail -s {subject} {userEmail}",$req.rawData);
shell.waitForClose(10)

This will sanitize the “subject”,”body” and “userEmail” parameters before merging them into test to send to the shell.  This way if a user types “&& rm -rf /” as the “body” parameter, this will be sent as the body of the email rather than erasing the server’s hard drive.

returns this Shell Object

waitFor

Myna.Shell.prototype.waitFor=function(text,
timeout)

waits for a text response from the process, or until a timeout occurs

Parameters

texttext to match
timeoutoptional, default: shell timeout Seconds to wait for a response.  If a timeout occurs an exception is thrown

returns this Shell Object

waitForErr

Myna.Shell.prototype.waitForErr=function(text,
timeout)

waits for a text response from the process on the error stream, or until a timeout occurs

Parameters

texttext to match
timeoutoptional, default: shell timeout Seconds to wait for a response.  If a timeout occurs an exception is thrown

returns this Shell Object

waitForPrompt

Myna.Shell.prototype.waitForPrompt=function(timeout)

waits for a command prompt response from the process, or until a timeout occurs

Parameters

timeoutoptional, default: shell timeout Seconds to wait for a response.  If a timeout occurs an exception is thrown

Note

the prompt expected is set in the constructor

returns this Shell Object

waitForClose

Myna.Shell.prototype.waitForClose=function(timeout)

waits for a shell to close, or timeout

Parameters

timeoutoptional, default: shell timeout Seconds to wait for shell to end.  If a timeout occurs an exception is thrown

returns this Shell Object

getExitValue

Myna.Shell.prototype.getExitValue=function()

returns exit value of the shell, or throws an exception if shell is still running

See waitForClose

getResponseText()

Myna.Shell.prototype.getResponseText=function()

returns the current stdout text buffer

getErrorText()

Myna.Shell.prototype.getErrorText=function()

returns the current stderr text buffer

Myna.Shell = function(options)
Constructor function for Shell class
Myna.Shell.prototype.send=function(text,
replacements)
sends input to the shell
Myna.Shell.prototype.sendLine=function(text,
replacements)
sends input to the shell, including a hard return
Myna.Shell.prototype.waitFor=function(text,
timeout)
waits for a text response from the process, or until a timeout occurs
Myna.Shell.prototype.waitForErr=function(text,
timeout)
waits for a text response from the process on the error stream, or until a timeout occurs
Myna.Shell.prototype.waitForPrompt=function(timeout)
waits for a command prompt response from the process, or until a timeout occurs
Myna.Shell.prototype.waitForClose=function(timeout)
waits for a shell to close, or timeout
Myna.Shell.prototype.getExitValue=function()
returns exit value of the shell, or throws an exception if shell is still running
Myna.Shell.prototype.getResponseText=function()
returns the current stdout text buffer
Myna.Shell.prototype.getErrorText=function()
returns the current stderr text buffer
Escapes a string to a quoted parameter for windows cmd shell
Escapes a string to a quoted parameter for sh family shells
String.prototype.format=function(values)
returns a string with parameters replaced