New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Bash scripts on the fly is returning the script itself and the output within the response #104
Comments
Hi, if (command.indexOf("bash -s") > -1) {sshObj.sessonText = sshObj.sessonText.replace(command,'')} |
Here is an alternative to your code, may need tweaking, let host = {
server: {
host: config.host,
port: config.port,
userName: config.username,
password: config.password,
privateKey: config.privateKey,
tryKeyboard: true,
algorithms: algorithmObj
},
disableASCIIFilter:true,
disableColorFilter:true,
debug:hostdebug,
verbose:hostVerbose,
commands: [ bash -s ${fs.readFileSync('./script1.sh', 'utf-8')},
'Hello World',
'ls l'
],
idleTimeOut:30000,
showBanner: true,
onCommandComplete: function( command, response, sshObj) {
if (command.indexOf("bash -s") > -1){
sshObj.sessonText = response.replace(command,'')
}
},
};
return new Promise( resolve, reject ) => {
let SSH = new SSH2Shell(host),
callback = function(sessionText){
ssh2shellInstances.pop();
resolve(sessionText);
}
SSH.on('error', reject);
ssh2shellInstances.push(SSH);
SSH.connect(callback);
}); |
I see you are working with multiple hosts but did you know that ssh2shell has that built in? |
Last issue that was running scripts on the fly had this this final feedback: "I read the local script file & encoded into Base64 to avoid any special characters. echo "<>" | base64 -d | bash and if you have command line inputs: This worked for me without physically copying the script on target as well as running into any special character issues with echo or printf statement. Hopefully it can help others who are looking to solve the same problem. |
Yes sure I'm aware of that and may implement it in the near future |
When I used this code, I did not git the output alone but still the response including the bash script itself and the output; as clearly visible in the attached below screenshot: Accordingly, how we can tackle this issue and get only the output? |
Yes that will never match to remove it. It probably needs a regular expression to match the beginning and end of the command content to remove it. Something like this, again probably needs tweaking to match correctly. onCommandComplete: function( command, response, sshObj) {
if (command.indexOf("bash ") > -1){
//end match would most likely fail if there was a problem
sshObj.sessonText = response.replace(command.substring(0, 4)+"*"+command.substring(command.length - 3),'')
}
} |
Great! we used the replace function and works fine. using replace will provide many options to search with regular expression and remove the matching lines from the response. Thanks again. |
My pleasure. Andrew |
Hi Andrew,
I'm using ssh2shell in my project to execute several commands and bash scripts on a remote server, and ofcourse get each command response and use it in some analysis later.
What I`m facing is that, when executing the bash script on the remote server, I'm getting the response containing the script itself and the out put as well. I will post here part of the code to give more details about what is going on:
CODE HERE - ALL THE BELOW:
let host = { server: { host: config.host, port: config.port, userName: config.username, password: config.password, privateKey: config.privateKey, tryKeyboard: true, algorithms: algorithmObj, }, asciiFilter:"[^\r\n\x20-\x7e]", disableASCIIFilter:true, disableColorFilter:true, closedMessage:"Closed", debug:hostdebug, standardPrompt:">$%#", commands: [
bash -s ${fs.readFileSync('./script1.sh', 'utf-8')}`,'Hello World',
'ls l'
],
idleTimeOut:30000,
showBanner: true,
window: true,
return new Promise(( resolve, reject ) => {
Plz support on a way to get the response of bash script as its output only without the script itself after execution on the remote host.
The text was updated successfully, but these errors were encountered: