# How to use ssh-client with connection details known only at run-time

**URL:** <https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837>\
**Category:** General\
**Created:** [5 February 2022 15:46 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837 "2022-02-05T15:46:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwhitele](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mwhitele](https://discourse.nodered.org/u/mwhitele)\
**Post date:** [5 February 2022 15:46 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/1 "2022-02-05T15:46:09Z")

</div>

I have a database containing ip addresses and credentials  
I can fetch the rows and execute a for-each loop  
Inside the loop, I want to use ssh-client or similar to connect to the IP address using credentials, execute a command then close the connection

How am I able to connect using variables , and not have to hardcode details?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 February 2022 16:00 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/2 "2022-02-05T16:00:32Z")

</div>

Welcome to the forum @mwhitele

How are you doing it with hard coded credentials?

**[Edit]** Are you using [node-red-contrib-ssh-client (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-ssh-client)? Looking at its github repository it hasn't been updated in three years, has no readme, and has unaddressed open issues, so I would advise against using that one.

Personally I would use an Exec node for this and execute ssh using that.

---

<div class="post-metadata">

**Author:** ![mwhitele](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mwhitele](https://discourse.nodered.org/u/mwhitele)\
**Post date:** [5 February 2022 16:29 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/3 "2022-02-05T16:29:01Z")

</div>

TY. Would you mind posting how I can Exec against hostname,username and password values that are stored in variables?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 February 2022 16:40 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/4 "2022-02-05T16:40:59Z")

</div>

How would you do it with fixed credentials if you were running ssh in a terminal?

**[Edit]** Hmm, you can't specify the password in the command line. Which would actually be a bad thing to do anyway.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 February 2022 16:52 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/5 "2022-02-05T16:52:14Z")

</div>

Can't you setup key authorisation? It would make it much simpler and more secure.

---

<div class="post-metadata">

**Author:** ![mwhitele](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mwhitele](https://discourse.nodered.org/u/mwhitele)\
**Post date:** [5 February 2022 17:28 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/6 "2022-02-05T17:28:17Z")

</div>

I’m writing the flow without knowing anything about the Linux endpoints that I want to use at runtime. I don’t know ip address username and password. I’d know, however, that I’ll be connecting using ssh protocol

At runtime, I will get the IP address, username and password from a database table

I’ll loop through the result set

For each iteration, I wish to execute some command (that does not matter for the sake of this question) on the Linux system loop.ipaddress, authenticated by loop.username and loop.password... and then close the connection

How do I get these loop variables into ssh-client or exec?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 February 2022 17:34 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/7 "2022-02-05T17:34:39Z")

</div>

> [@mwhitele](#):
>
> How do I get these loop variables into ssh-client or exec?

SSH is designed to stop you doing anything that will lead to you getting hacked, that is why you can't specify the password in the command line. You may be able to achieve what you want using sshpass. [https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/](https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/)

Why can't you use public/private keys and keep those in files, or even in the database I suppose?

---

<div class="post-metadata">

**Author:** ![mwhitele](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mwhitele](https://discourse.nodered.org/u/mwhitele)\
**Post date:** [5 February 2022 18:04 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/8 "2022-02-05T18:04:33Z")

</div>

Thanks, I’ll go research keys. In the meantime, how can I get my loop.hostname variable into ssh-client and exec ... I’d be interested in knowing for both ssh-client and Exec please

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 February 2022 18:07 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/9 "2022-02-05T18:07:19Z")

</div>

Don't know about ssh-client. For use in an exec node you just have to build the appropriate command and pass it to the exec node. So first work out what the command line you want to execute is, then you just have to build a string containing that.

---

<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:** [5 February 2022 21:45 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/10 "2022-02-05T21:45:43Z")

</div>

> [@mwhitele](#):
>
> get the IP address, username and password from a database table

This is a very bad idea from any security perspective.

You should be using at the very least a secure keystore.

If what you are connecting to has any kind of value to you, your organisation or your customer(s), you should get a security professional to review and report on the issues and recommend solutions.

> [@mwhitele](#):
>
> Exec

Exec allows msg properties to be passed to the command. Have a play with it, you will soon get the hang.

---

<div class="post-metadata">

**Author:** ![mwhitele](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mwhitele](https://discourse.nodered.org/u/mwhitele)\
**Post date:** [6 February 2022 01:18 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/11 "2022-02-06T01:18:42Z")

</div>

Thanks for the guidelines. I did research security keys and closed that hole. I’ll go play with Exec now

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 February 2022 10:08 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/12 "2022-02-06T10:08:53Z")

</div>

I suggest putting just `ssh` as the command in the exec node and then build the parameter string in the payload to pass to it.

---

<div class="post-metadata">

**Author:** ![mwhitele](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mwhitele](https://discourse.nodered.org/u/mwhitele)\
**Post date:** [7 February 2022 14:26 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/13 "2022-02-07T14:26:43Z")

</div>

I’ve been successful with Exec. The command I’m executing sometimes asks for confirmation to proceed with “Confirm?”. I can’t get spawn working such that it reads each line output from the command.

I would like to test a line for “Confirm?” then send “Y” back to the input stream. On completion, I want to send “exit”

Are there any examples or videos on how to do this sort of thing please?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 February 2022 14:47 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/14 "2022-02-07T14:47:57Z")

</div>

What is the command you are running and under what conditions does it ask? That is what is asking for the confirm so you must adjust it (or the system) so that it does not ask.

**[Edit]** If it is asking something like  
`RSA key fingerprint is 96:a9:23:5c:cc:d1:0a:d4:70:22:93:e9:9e:1e:74:2f.`  
`Are you sure you want to continue connecting (yes/no)?`

Then that is produced the first time you connect to a new host.

Add into the ssh command  
`-o StrictHostKeyChecking=accept-new`  
Try it in a terminal first to see if it works and prevents the confirmation request.

---

<div class="post-metadata">

**Author:** ![mwhitele](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mwhitele](https://discourse.nodered.org/u/mwhitele)\
**Post date:** [7 February 2022 14:59 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/15 "2022-02-07T14:59:04Z")

</div>

I’m after examples of “expect” processing.

1. Am I able to see each line as it comes back from the exec command? The description of exec spawn says I can, but I can’t get it working. Please post an example… if there is one. Does spawn actually work? So far, I’ve seen that is does not

2. “Confirm?” is one of any number of different “expect” messages coming back from from the server, each requiring a different response. I want to set up responses. I’m automating a current manual interactive process

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 February 2022 15:03 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/16 "2022-02-07T15:03:25Z")

</div>

In that case this is nothing to do with the subject line of the thread. I suggest you start a new thread and someone who knows how to do that will probably help. When you do that please post a flow that you think should work but does not.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 February 2022 15:11 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/17 "2022-02-07T15:11:16Z")

</div>

A final thought here, if the s/w is asking for input specifically on the keyboard rather than via stdin/stdout/stderror than I don't think it can be done, though I may be wrong. Nothing to do with node-red, you would have the same issue using any s/w.

---

<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:** [7 February 2022 15:38 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/18 "2022-02-07T15:38:37Z")

</div>

Perhaps you need something like one of these:

> **[GitHub - mscdex/ssh2: SSH2 client and server modules written in pure...](https://github.com/mscdex/ssh2)**
>
> SSH2 client and server modules written in pure JavaScript for node.js - GitHub - mscdex/ssh2: SSH2 client and server modules written in pure JavaScript for node.js

> **[GitHub - cmp-202/ssh2shell: Wrapper class for Node.js ssh2 shell command for...](https://github.com/cmp-202/ssh2shell)**
>
> Wrapper class for Node.js ssh2 shell command for running multiple commands - GitHub - cmp-202/ssh2shell: Wrapper class for Node.js ssh2 shell command for running multiple commands

---

<div class="post-metadata">

**Author:** ![mwhitele](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mwhitele](https://discourse.nodered.org/u/mwhitele)\
**Post date:** [7 February 2022 17:58 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/19 "2022-02-07T17:58:36Z")

</div>

@TotallyInformation I like the look of ssh2shell! Thank you for posting. How do I call that from Node-RED ... I do not see it "manage pallet" I've tried creating a function to call it, but no luck probably because I'm new to Node-RED

---

<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:** [7 February 2022 22:09 UTC](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837/20 "2022-02-07T22:09:03Z")

</div>

I've not tried it so you may need some messing to get it to do what you want.

The function node will take an external npm module and will do the necessary for you. Details in the node-red docs.

[Next page](https://discourse.nodered.org/t/how-to-use-ssh-client-with-connection-details-known-only-at-run-time/57837.md?page=2)
