# SFTP node crashes server, the other one is not working

**URL:** https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757
**Category:** General
**Created:** [15 September 2022 16:22 UTC](https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757 "2022-09-15T16:22:02Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![smitterer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smitterer/32/59179_2.png) [@smitterer](https://discourse.nodered.org/u/smitterer)
#### Post date: [15 September 2022 16:22 UTC](https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757/1 "2022-09-15T16:22:02Z")

</div>

Hi!

We are using node-red to transfer files to another server via sftp.

For that, we were using [node-red-contrib-sftp (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-sftp) but this is now always only giving us "connecting" when we use the put function, without any debug or log output. (List and Get works)  
We then tried another node ([node-red-contrib-better-sftp (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-better-sftp)) and it even crashes the server with the following information in the log:

```auto
15 Sep 18:16:16 - [red] Uncaught Exception:
15 Sep 18:16:16 - [error] Error: : Connection ended unexpectedly by remote server
    at Object.formatError (/root/.node-red/node_modules/ssh2-sftp-client/src/utils.js:57:18)
    at Client.<anonymous> (/root/.node-red/node_modules/ssh2-sftp-client/src/index.js:1038:21)
    at Client.emit (node:events:513:28)
    at Socket.<anonymous> (/root/.node-red/node_modules/ssh2/lib/client.js:312:10)
    at Socket.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
nodered.service: Main process exited, code=exited, status=1/FAILURE
nodered.service: Failed with result 'exit-code'.

```

As both nodes are not working: might this be connected to any dependency which both nodes are using but which is not working or any linux problem?

The admin of the server where we want to put files on via ssh says that neither our user, nor the ip of the server where node red runs on is blocked.  
With the same credentials, we are able to login using filezilla / putty or other programs.

Thanks!

---

<div class="post-metadata">

### Author: ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)
#### Post date: [16 September 2022 11:35 UTC](https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757/2 "2022-09-16T11:35:21Z")

</div>

I had the same problem [here,](https://discourse.nodered.org/t/sftp-keeps-crashing-nr/66593) no solution found yet.

---

<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: [16 September 2022 13:02 UTC](https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757/3 "2022-09-16T13:02:47Z")

</div>

That node is VERY old. It references v2 of the support library - the current is v9!

So no great surprises that it isn't working I'm afraid.

You could try referencing the support library directly in a function node perhaps?

[theophilusx/ssh2-sftp-client: a client for SSH2 SFTP (github.com)](https://github.com/theophilusx/ssh2-sftp-client)

---

<div class="post-metadata">

### Author: ![smitterer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smitterer/32/59179_2.png) [@smitterer](https://discourse.nodered.org/u/smitterer)
#### Post date: [19 September 2022 08:04 UTC](https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757/4 "2022-09-19T08:04:55Z")

</div>

I'm currently trying to use [scp-promises - npm](https://www.npmjs.com/package/scp-promises) in a function node but had no success yet.

The node-sample imports CreateScpConnection from scp-promises but I wasn't able to do so in the node. (`import { CreateScpConnection } from 'scp-promises'`)  
I'm currently trying around with similar codes from [How to make PDF from HTML - #4 by Steve-Mcl](https://discourse.nodered.org/t/how-to-make-pdf-from-html/67688/4) and [Useage of contrib-npm - #6 by bakman2](https://discourse.nodered.org/t/useage-of-contrib-npm/67449/6) provided by @Steve-Mcl and @bakman2 (things like `const { scpPromises } = new CreateScpConnection("");`) and so on instead of using "import".

Also, I need to find a way to set the path and filename based on msgs.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [19 September 2022 10:46 UTC](https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757/5 "2022-09-19T10:46:46Z")

</div>

Try...

```auto
const scp = scpPromises.CreateScpConnection(...

```

If that doesn't work then try putting a node warn in there to figure out what is what...

```auto
node.warn({scpPromises})

```

Note: I am assuming you have named the npm import on the setup tab as `scpPromises`

---

<div class="post-metadata">

### Author: ![smitterer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smitterer/32/59179_2.png) [@smitterer](https://discourse.nodered.org/u/smitterer)
#### Post date: [19 September 2022 10:58 UTC](https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757/6 "2022-09-19T10:58:09Z")

</div>

Thanks!

With scp-promises it didn't work, but it seems to work with node-scp:

```auto
const Client = nodeScp.Client;

async function test() {
  try {
    const client = await Client({
      host: 'hostname',
      port: 22,
      username: 'user',
      password: 'pw',
      // privateKey: fs.readFileSync('./key.pem'),
      // passphrase: 'your key passphrase',
    })
    await client.uploadFile(
      '/sourcefile.csv',
      '/destinationfile.csv',
      // options?: TransferOptions
    )
    // you can perform upload multiple times
    //await client.uploadFile('./test1.txt', '/workspace/test1.txt')
    client.close() // remember to close connection after you finish
  } catch (e) {
    console.log(e)
  }
}

test()

return msg;

```

I will now try to dynamically set sourcefile and destinationfile.

---

<div class="post-metadata">

### Author: ![smitterer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smitterer/32/59179_2.png) [@smitterer](https://discourse.nodered.org/u/smitterer)
#### Post date: [19 September 2022 11:28 UTC](https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757/7 "2022-09-19T11:28:36Z")

</div>

Ok here is the working flow with dynamic paths:

```auto
[
    {
        "id": "7cfd133f7dc601d4",
        "type": "function",
        "z": "bf69329bcb962369",
        "name": "scp put 2",
        "func": "const Client = nodeScp.Client;\nconst remotepath = msg.remotepath;\nconst sourcepath = msg.sourcepath;\n\nasync function test() {\n try {\n const client = await Client({\n host: 'host',\n port: 22,\n username: 'un',\n password: 'pw',\n // privateKey: fs.readFileSync('./key.pem'),\n // passphrase: 'your key passphrase',\n })\n await client.uploadFile(sourcepath, remotepath)\n // you can perform upload multiple times\n //await client.uploadFile('./test1.txt', '/workspace/test1.txt')\n client.close() // remember to close connection after you finish\n } catch (e) {\n console.log(e)\n }\n}\n\ntest()\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [
            {
                "var": "nodeScp",
                "module": "node-scp"
            },
            {
                "var": "ssh2",
                "module": "ssh2"
            }
        ],
        "x": 800,
        "y": 780,
        "wires": [
            [
                "b56b8a9d3f8410fd"
            ]
        ]
    },
    {
        "id": "42a1217fd3ada431",
        "type": "inject",
        "z": "bf69329bcb962369",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 400,
        "y": 780,
        "wires": [
            [
                "be49f9ed614eacc1"
            ]
        ]
    },
    {
        "id": "b56b8a9d3f8410fd",
        "type": "debug",
        "z": "bf69329bcb962369",
        "name": "debug 52",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1020,
        "y": 780,
        "wires": []
    },
    {
        "id": "be49f9ed614eacc1",
        "type": "change",
        "z": "bf69329bcb962369",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "sourcepath",
                "pt": "msg",
                "to": "/home/test.csv",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "remotepath",
                "pt": "msg",
                "to": "/remote/test.csv",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 600,
        "y": 780,
        "wires": [
            [
                "7cfd133f7dc601d4"
            ]
        ]
    }
]

```

```auto
const Client = nodeScp.Client;
const remotepath = msg.remotepath;
const sourcepath = msg.sourcepath;

async function test() {
  try {
    const client = await Client({
      host: 'host',
      port: 22,
      username: 'un',
      password: 'pw',
      // privateKey: fs.readFileSync('./key.pem'),
      // passphrase: 'your key passphrase',
    })
    await client.uploadFile(sourcepath, remotepath)
    // you can perform upload multiple times
    //await client.uploadFile('./test1.txt', '/workspace/test1.txt')
    client.close() // remember to close connection after you finish
  } catch (e) {
    console.log(e)
  }
}

test()

return msg;

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [19 September 2022 12:07 UTC](https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757/8 "2022-09-19T12:07:45Z")

</div>

> [@smitterer](#):
>
> `console.log(e)`

If you want to see/catch errors in node-red, change the above to...

```auto
node.error(e, msg)

```

---

<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: [18 November 2022 12:08 UTC](https://discourse.nodered.org/t/sftp-node-crashes-server-the-other-one-is-not-working/67757/9 "2022-11-18T12:08:19Z")

</div>

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