Escape bash variable in node over ssh

若如初见. 提交于 2020-07-10 08:28:12

问题


When executing a bash command in node and passing a dynamic parameter, the standard way to go is to use spawn and avoid escaping. That is:

const filename = 'file with spaces'
spawn('ls', [filename]) // All good, received 'file with spaces'

This is foolproof since filename is passed as a standalone variable to bash.

Now, what happens if I want to do the same through ssh? The following is not an option:

const filename = 'file with spaces'
spawn('ssh', [host, 'ls', filename]) // Wrong!! Received 'file' 'with' 'spaces'

Ssh is accepting ls and filename as vargars. Joining it and executing, which defeats the purpose.


回答1:


One way is to pass the value using base64 which has expected characters and then escape in bash

spawn('ssh', [host, 'ls', `"$(echo ${btoa(filename)} | base64 -d)"`])


来源:https://stackoverflow.com/questions/60204515/escape-bash-variable-in-node-over-ssh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!