Skip to content
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

Closed
dalgharib opened this issue May 3, 2023 · 9 comments

Comments

@dalgharib
Copy link

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,

		onCommandComplete: async function( command, response,sshObj,stream) {                  
			const result.output=response;						
		},
	};

return new Promise(( resolve, reject ) => {

let SSH = new SSH2Shell(host),
callback = function(sessionText){                    
	resolve(commandResponses);                    
}

ssh2shellInstances.push(SSH);
SSH.on('error', reject);
SSH.connect(callback);

SSH.on ('emitCloseStream', function onClose(){                
	this.emit('closeStream');
});


SSH.on ('commandProcessing', function onCommandProcessing( command, response, sshObj, stream ) {
	this.on('closeStream',()=>{                        
			stream.close();
			ssh2shellInstances.pop();                        
	}) 
});`

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.

@cmp-202
Copy link
Owner

cmp-202 commented May 3, 2023

Hi,
Okay so you seem to be reading in a local script file and adding the content to the end of the bash -s command to run on the host.
You can detect the bash -s command in onCommandComplete and remove it from the sessionText.

if (command.indexOf("bash -s") > -1) {sshObj.sessonText = sshObj.sessonText.replace(command,'')}

@cmp-202
Copy link
Owner

cmp-202 commented May 3, 2023

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);
});

@cmp-202
Copy link
Owner

cmp-202 commented May 3, 2023

I see you are working with multiple hosts but did you know that ssh2shell has that built in?

Multiple primary hosts
Test example

Tunnelling nested hosts
Test example

@cmp-202
Copy link
Owner

cmp-202 commented May 3, 2023

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.
Then with it I formed a command at the runtime to execute at the target:

echo "<>" | base64 -d | bash

and if you have command line inputs:
echo "<>" | base64 -d | bash -s

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.
"

@dalgharib
Copy link
Author

I see you are working with multiple hosts but did you know that ssh2shell has that built in?

Multiple primary hosts Test example

Tunnelling nested hosts Test example

Yes sure I'm aware of that and may implement it in the near future

@dalgharib
Copy link
Author

dalgharib commented May 4, 2023

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);
});

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?

image

@cmp-202
Copy link
Owner

cmp-202 commented May 4, 2023

Yes that will never match to remove it.
Normally the command is removed from output but the command in this case runs over multiple lines with the extra characters and so is part of the command output.

It probably needs a regular expression to match the beginning and end of the command content to remove it.
Only trouble is getting it to work with different scripts if you use more than one.

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),'')
        }
    }

@dalgharib
Copy link
Author

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.

@cmp-202
Copy link
Owner

cmp-202 commented May 8, 2023

My pleasure.

Andrew

@cmp-202 cmp-202 closed this as completed May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants