how to use multiple lines in executeSSHCommand ?

Started by Millenium7, January 19, 2022, 09:32:55 AM

Previous topic - Next topic

Millenium7

I'm trying to use SSH to push commands to devices through netXMS
I'm using $node->executeSSHCommand to push commands, but how do I use multiple lines of text? I know on MikroTik I can separate commands with a semi-colon, i.e.

$node->executeSSHCommand(":log warning test;:log warning test2;:log warning test3");

This isn't viable for very long commands especially those with scripts in them that might be 50 lines or more. I can't compress them into a single line
So how do I do something like....

$node->executeSSHCommand("
:log warning test
:log warning test2
:log warning test3
");


Or even inside a variable

MyBigScript = {
:log warning test
:log warning test2
:log warning test3
};
$node->executeSSHCommand(MyBigScript);


This syntax is not valid in NetXMS. So how do I do it? I need SSH scripts to be human readable and editable. Impossible with very long single liners

Millenium7

#1
I think I figured this out nope there's a problem, see next post


$node->executeSSHCommand("log warning \"This is line1\"\n".
"log warning \"This is line2\"\n".
"log warning \"This is line3\"\n");


Can also be parsed inside a string parameter. But the rules are
1) all quotations to be parsed need to be escaped with a '\'
2) the end of each line needs to have \n inside the quotations (otherwise they just continue on the same line i.e. "log warning This is line1log warning This is line2")
3) each line needs to end with . to concatenate the lines together

It's not the end of the world, but its a lot of manual manipulation of script files. Would be nice if I could just paste text blocks inside parenthesis but I imagine this is a limitation of NetXMS scripting language. If there is a better/cleaner method i'm all ears

edit: to make life easy, in notepad++ paste the script then do the following...
search->replace, pick extended mode
1)
find what: "
Replace with: \"
replace all

2)
find what: \r\n
replace with: \\n".\r\n"
replace all

3) add (" to beginning of first line

4) add "); to very last line

Now copy/paste into NetXMS

This is fast and effective enough for me, some of my scripts that I want to use NetXMS with are well over a hundred lines long. Doing this manually would have been a flapping nightmare

Millenium7

So this has a problem. According to https://track.radensolutions.com/issue/NX-1649 I imagine the command 'executeSSHCommand' was implemented to get around the 1024 character limit is this correct? or is there another command? Since the bug tracker doesn't say which command was implemented

But it seems there's actually a ~232 character limit on executeSSHCommand. At least I can't get it to run anything at all when I exceed that

command = (":log info \"this is line01\"\n".
":log info \"this is line02\"\n".
":log info \"this is line03\"\n".
":log info \"this is line04\"\n".
":log info \"this is line05\"\n".
":log info \"this is line06111111111111111111\"\n".
"");

$node->executeSSHCommand(command);


This is the longest possible string I can parse to the command, 1 character longer and nothing executes at all. Is this a bug or am I just doing this whole thing incorrectly?
I have some scripts that are well over 2500 characters in length so this is a big problem

Victor Kirhenshtein

Hi,

unfortunately it is a limitation inside executeSSHCommand function implementation. I've added issue to fix it: https://track.radensolutions.com/issue/NX-2200

Best regards,
Victor

Millenium7

Thanks, would be great if this was prioritized as this will massively enhance the potential of NetXMS. One of its greatest strengths for us is the ability to very rapidly run summary DCI's to get all sorts of useful information in a simple clean page. Being able to then select multiple nodes out of compliance and simply right click->tools->run SSH script to resolve issues or push mass config updates is very powerful

Ideally i'd like to see further updates to SSH down the track, such as credentials list, config backups, diff notifications (against last, or template baseline). But for now a simple working SSH command execution would be nice  ;D