# Idea - Create SSH Session Nodes with collecting Command Output during the session \[Start | End\] like \[TCL/Expect\]

**URL:** <https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185>\
**Category:** Developing Nodes\
**Created:** [2 November 2020 02:15 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185 "2020-11-02T02:15:14Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudoritz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sudoritz/32/29806_2.png) [@sudoritz](https://discourse.nodered.org/u/sudoritz)\
**Post date:** [2 November 2020 02:15 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/1 "2020-11-02T02:15:14Z")

</div>

Im trying to find the best way to help create a SSH node scenario and when inside the pseudo-terminal to then act as After the command executes capture that response from output and Wait until you expect a $ or prompt for that type of device.

Start SSHlogin ------- [expect $]---[run CMD1]---[view results] exit ssh etc ----- [END SSH]

I simulated a super basic flow Idea. NOTE (this shows as http-in and http-out but this is SSH Start and SSH-Finish as a mockup (not including Wait for or Expect {chars})

 ![Screen Shot 2020-11-01 at 6.48.12 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a5461bc714d90297a0c594cb5278c1cae5f707a9.png)

Scenario could be going into a linux device run 2-3 commands Based on the output of cmd2 do Different testing or more . restart services etc etc.

I thought a Cool idea stemming from @BartButenaers awesome xTerm app [node-red-contrib-xterm]

my thought is Maybe simulate the ssh-start-end but also usinng xterm to view it execute live on the right side while Testing....

So it can test and perform on screen so while building this flow then when complete or) then it is safe to run but during flow build time we can take session over and maybe properly exit device if stuck so no Stuck SSH sessions.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [2 November 2020 18:36 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/2 "2020-11-02T18:36:14Z")

</div>

Hi @sudoritz,  
I'm not an SSH expert, so I will need to have some extra info to be able to understand it correctly.

1. Do I understand it corectly that you want to use Node-RED as an SSH server, and you want to connect to it from an SSH client?
2. (If 1 is correct) Can you simple connect via SSH to the http-in node? Because SSH is TCP based?
3. I don't see my xterm node in your flow. Where should it be located?
4. Do you want to use my xterm node to see the commands visually that are being executed? Or do uou want to halt them until approval? Or ...
5. Whag do you mean with _"simulate the ssh-start-end but also usinng xterm to view it execute live on the right side while Testing...."_?
6. What does the _"flow build time"_ mean?

Would be nice if you could add me some dummy-proof answers, because I think you have lots of experience in this domain.  
Bart

---

<div class="post-metadata">

**Author:** ![sudoritz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sudoritz/32/29806_2.png) [@sudoritz](https://discourse.nodered.org/u/sudoritz)\
**Post date:** [2 November 2020 19:23 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/3 "2020-11-02T19:23:38Z")

</div>

1. Do I understand it corectly that you want to use Node-RED as an SSH server, and you want to connect to it from an SSH client?

> My idea is use node-red SSH node to different hosts and run commands --- ssh [admin@10.10.10.10](mailto:admin@10.10.10.10) [logged in] run cmd {df -h} Gather that output {service show mysqld} gather.... then exit ssh session. (mid-session we could change depending output of cmd)

1. (If 1 is correct) Can you simple connect via SSH to the http-in node? Because SSH is TCP based?

> No sorry i was Just using the http-in node as a Reference to build out SSH-START and SSH-Compete (i never built a node so im trying to figure out how i could) but SSH start node will have Left And Right side connects. and SSH complete will only have Left side thus need a ssh\_start node. (the flow wont work without a start and an end.

1. I don't see my xterm node in your flow. Where should it be located?

> xterm would be used on the right side to help build the ssh automation setup. View it live when passing. (its not needed but would help inject and test before a working ssh automation.) im just not sure how STDIN from each command output would get gathered in the msg.payload from the latest command and How can we add Expect the bottom line to show $ or \_ or \> on the line or last character.

1. Do you want to use my xterm node to see the commands visually that are being executed? Or do uou want to halt them until approval? Or ...

> Possible 2 fold (IF its faiing we can jump into the [stuck] session persay and proper exit that session)

1. Whag do you mean with _"simulate the ssh-start-end but also usinng xterm to view it execute live on the right side while Testing...."_ ?

> this could be Separate but this could help withh when building this out (say API call or Inject button is pressed) xterm (terminal window) could show it logging in a device doing commands and then exiting device. Where the Node-red ssh start has both left/right coulld show it moving through the nodes (if status shows)

1. What does the _"flow build time"_ mean?

> While we are building out the flow and making sure it would work without manual input. use xterm as a way to see that session move through its Flow of steps.

the app doesnt have to use xterm (but i thought it could be a really helpful view while building each command/step through (mabye we can program start injects at different areas in case to troubleshoot.)

SSH device examples.

ssh [admin@10.0.0.1](mailto:admin@10.0.0.1)

```auto
{show interface ge1/2}
{show bgp sum}
{show ip route 10.11.11.3}
{exit}

```

ssh session exit

Scenario 2

[Function node extract results]

ssh [root@10.0.5.2](mailto:root@10.0.5.2)

```auto
{service mysqld status} 
{service mysqld restart}
{service mysqld status}
{df -h}
{free}
{exit}

```

Scenario 3

from API payload we can get an additional alarm issue and gather issue but PASS parameters to commands like show sub sesion ip {msg.payload.cmd}

ssh [admin@10.10.10.10](mailto:admin@10.10.10.10)  
`{show sub sess ip 10.22.22.11 }`  
[stay in ssh session gather input and make different command based on First command result)  
`{clear sub session id 10.22.11}`  
OR  
`{exit}`

---

<div class="post-metadata">

**Author:** ![sudoritz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sudoritz/32/29806_2.png) [@sudoritz](https://discourse.nodered.org/u/sudoritz)\
**Post date:** [2 November 2020 19:48 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/4 "2020-11-02T19:48:13Z")

</div>

So while working in developing / building out use xTerm to visually see the commands pass.

 ![Screen Shot 2020-11-02 at 12.42.20 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8fcd0bac88ec97eb94aef4f79a87aec127a88a24.png)

From picture (start ssh) run command it Visualy show as its runnning on the right side using xTerm.

If command takes 5 seconds to run. Maybe the command[function like] has a post-process Wait for matching character or message on last line but also capture. this could save 2 nodes to 1 node like command node like Value to send {param1} {param2} but then post process then can store into msg.payload.cmdxx.result and take tht postProcess result and d wait for # character to therefore go and run cmd 2 OR even through a switch IF output cmd 1 = good then proceed this way OR IF output = bad then do additional .

im thinking to use xterm as a automation helper but maybe dont need it for SSH nodes to work.

---

<div class="post-metadata">

**Author:** ![sudoritz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sudoritz/32/29806_2.png) [@sudoritz](https://discourse.nodered.org/u/sudoritz)\
**Post date:** [2 November 2020 19:53 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/5 "2020-11-02T19:53:17Z")

</div>

I Found a few nodes but most of them send setup a ssh connection Send a command and close the session right away.

> **[node-red-contrib-ssh-client](https://flows.nodered.org/node/node-red-contrib-ssh-client)**
>
> A Node-RED node for runing remote ssh command.

  

> **[node-red-contrib-ssh-coco](https://flows.nodered.org/node/node-red-contrib-ssh-coco)**
>
> A Node-RED node for running remote ssh commands and supports reconnection attempts. Based on https://github.com/yroffin/node-red-contrib-ssh

  

> **[node-red-contrib-bigssh](https://flows.nodered.org/node/node-red-contrib-bigssh)**
>
> A Big SSH

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 November 2020 21:30 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/6 "2020-11-02T21:30:35Z")

</div>

You can use BASH Here syntax to run multiple commands over SSH, something like:

```auto
ssh me@myserver1.example.com << EOF
 date
 hostname
 cat /etc/resolv.conf
EOF

```

Alternatively, you can pipe a BASH script to be run on the remote end:

```auto
$ cat remote-commands.bash | ssh me@myserver1.example.com

```

Not quite what you might be after but certainly a way to run more complex things remotely.

I agree though that some nodes to open and close an SSH session and a node to send commands and receive output would be a really nice thing to have & would let you control things via Node-RED rather than a BASH script.

* * *

There would seem to be some potential modules on npmjs that might form the basis for something useful?

> **[ssh - npm search](https://www.npmjs.com/search?q=ssh)**

And some potentially useful info here:

> <https://unix.stackexchange.com/questions/466837/how-to-execute-a-command-through-ssh-and-keep-the-connection-open>

---

<div class="post-metadata">

**Author:** ![sudoritz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sudoritz/32/29806_2.png) [@sudoritz](https://discourse.nodered.org/u/sudoritz)\
**Post date:** [2 November 2020 22:34 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/7 "2020-11-02T22:34:27Z")

</div>

> [@TotallyInformation](#):
>
> You can use BASH Here syntax to run multiple commands over SSH, something like:

Ya we could definitely do that and use expect / TCL for ssh and run a exec/ expect scripts we could just  
execute/run.

Im thinking for the community. how awesome to be able control at a per command flow and build flow and use the power of node-red for its awesomeness while in the SSH session and to make decisions based on command output.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [3 November 2020 22:32 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/8 "2020-11-03T22:32:34Z")

</div>

Hey @sudoritz,

Although I find your idea intriging, I don't think it is possible.  
Will try to explain how the xterm (node) works.

Suppose two users are running a flow editor session, to interact with a Node-RED server:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/45b9de55d26bf0982f101a6f58c459f1f2486ffc.jpeg)

1. User 1 enters a character (to enter a command), which will be send via an ajax call to the Node-RED server.
2. That character will be send to the (linux/windows/...) process which is reserved for that flow editor session (by the xterm node).
3. That character will be send back to the xterm node.
4. That character will be send (via websocket channel) back to the flow editor, where you will see it appear. Some notes:
  - So the character has done an entire _ **roundtrip** _ to the OS process and back to the browser, before you see it appear!!
  - As soon as an entire command has been entered, the process will also send the output (i.e. the result of the command) to the flow editor (via steps 4 and 5).
  - The xterm node will see to which flow editor session the characters (input/output) belong, to make sure that multiple users can work simultaneously in separate sessions (without the content being mixed): see steps 5 to 8.

However what you want to do:  
a) Inject a message to the Node-RED server  
b) That message travels on the Node-RED server between the nodes.  
c) You connect via an ssh node to a remote process to send a command (step 9)  
d) Then you get a result from that session (step 10)  
e) And then you send that result to my node (step 11).  
f) Suppose my xterm node would have an input, it would receive characters. But it has no idea which node originally has triggered the inject button. So it won't know where to send the characters, which means that both flow editors would show the characters.

And when you enter characters in the xterm terminal, then - suppose my xterm node should have an output - those characters should be send to the output, and wired again to the ssh node. But you wouldn't see your own letters appear, since I get no data back from the next node in the flow.

I hope you see that the xterm concept doesn't really fit your idea...

---

<div class="post-metadata">

**Author:** ![sudoritz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sudoritz/32/29806_2.png) [@sudoritz](https://discourse.nodered.org/u/sudoritz)\
**Post date:** [4 November 2020 16:08 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/9 "2020-11-04T16:08:55Z")

</div>

Ohhhhh wow Thanks for the deeeeeeeeeeep dive hehe.

So using xterm we wouldnt be able to triage since we dont have something like a pseudo -pid to attach to.

So if i were to go back and not use xterm for live output but use the debug node to view cmd data to build regex/function parsing.

if i were to try and code ssh/cmd Nodes could i have a start\_ssh---[cmd1]----[cmd2]----end\_ssh.

would that work on maybe the same concept as xterm where you need a pseudo terminal session-id and it creates its ssh session but control stdin/stdout.

i saw this

> **[empty - run processes and applications under pseudo-terminal (PTY) sessions...](http://empty.sourceforge.net/)**

but not sure if a node maybe in this scenario in-out so maybe not even work in a ssh-start-end node

but i would think it would be a easier way to control the session id

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 November 2020 16:25 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/10 "2020-11-04T16:25:41Z")

</div>

> [@sudoritz](#):
>
> if i were to try and code ssh/cmd Nodes could i have a start\_ssh---[cmd1]----[cmd2]----end\_ssh.

I think that some of the Node.js modules I shared will let you do that in Node.js and so should be readily incorporated into nodes.

---

<div class="post-metadata">

**Author:** ![sudoritz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sudoritz/32/29806_2.png) [@sudoritz](https://discourse.nodered.org/u/sudoritz)\
**Post date:** [4 November 2020 16:29 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/11 "2020-11-04T16:29:42Z")

</div>

i saw this one

> **[spectcl](https://www.npmjs.com/package/spectcl)**
>
> Spawns and interacts with child processes using spawn / expect commands

---

<div class="post-metadata">

**Author:** ![sudoritz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sudoritz/32/29806_2.png) [@sudoritz](https://discourse.nodered.org/u/sudoritz)\
**Post date:** [6 November 2020 04:38 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/12 "2020-11-06T04:38:08Z")

</div>

so im trying to study on how to create the nodes to add some LOVE to the community.

so for this setup i think I found a npm to add to help create nodes with. **spectcl**

. (so hopefully i can somehow install it with dependencies)

im not sure yet trying to figure it out now... But if I were to create nodes. and look at some githubs

**Spawn\_node** = `session.spawn` -- (setup the session)  
**Expect\_node** = `session.expect` -- (expect match)  
**send\_node** = `session.send` -- send CMD\r  
**expect+send\_[1]node** = {maybe build the expect AND Send in the same node with timeout options ) could save some screen re-estate space in flow connections but have like 2 areas in the function like code area

example snippet from the **NPM spectcl**

```auto
var Spectcl = require('../lib/spectcl')
var session = new Spectcl({timeout: 5000})

session.spawn('echo', ['hello'])
session.expect([
        '>', function(match, matched, cb){
            session.send('exit\r')
            console.log('output was:\n%s',session.expect_out.buffer)
            cb()
    },
], 
});

```

`session.spawn('node --interactive')` (not sure on that)  
`session.spawn('echo hello', [], {}, {noPty:true})` (setup noPty ? option)

|

- [is this buffer out the command ouput to msg.payload after]

`session.expect_out.buffer`

here's a screenshot of a mockup i'm thinking of building.

 ![node-red-mockup-TCL](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/3616d6298eb96709ffbbe5f15e1df2947bda1426.png)

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 November 2020 07:32 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/13 "2020-11-06T07:32:49Z")

</div>

> [@sudoritz](#):
>
> so hopefully i can somehow install it with dependencies

Yes of course.  
Suppose you use NPM _"package"_ named [Spectcl](https://www.npmjs.com/package/spectcl),  
Then you need to add it as a dependency to the package.json file of your project:

```auto
    "dependencies": {
       "spectcl": "^0.6.3"
    }, 

```

The `^` means that all other minor versions (0.x.x) are also ok to be downloaded, but not major versions (1.x.x) because those might contain breaking changes. So if the spectcl package would be updated next week, people just need to update your node-red node and automatically get the update of the spectcl library

But if I were you, I would start using for a similar package because this one hasn't been updated the last 4 years!! And if you would be able to implement your stuff with NodeJs modules (e.g. don't know if you can use a [child\_process](https://nodejs.org/api/child_process.html) ???), but that would even better because then it works without having to install extra dependencies...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 January 2021 07:33 UTC](https://discourse.nodered.org/t/idea-create-ssh-session-nodes-with-collecting-command-output-during-the-session-start-end-like-tcl-expect/35185/14 "2021-01-05T07:33:07Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
