I seem to be stuck here. I want to send a system request from my script to another server via SSH, checking if a folder there exists. A folder path is passed from another script, stored in a variable and might have a space character in it. Since I couldn't replace the space with another character, to avoid a "not found" on folder like "foo bar", I need to pass something like
ls '/folderpath/foo bar'
to other server's shell.
Sample code looks like this:
$cmd = 'ssh -i id_pub $ssh_addr ls $remote_dir';
if (system($cmd) == 0) {
do something
}
I've exhausted all possible options - tired to escape the possible space with \ before passing it to the command, tried to pass it with ' ', " ", inside and adding both before passing it into $cmd. But I always end up with something like this:
ls \folderpath\foo\\ bar or ls \' \folderpath\foo bar\'
but not ls '\folderpath\foo bar'
I'm not that good with Perl, possible someone more experienced can recommend a workaround?
ssh
parses everything as remote code regardless, so you can't avoid needing to trust yourremote_dir
value if you aren't giving it an extra layer of shell-safe escaping (only one layer for the remote system needed if you stop usingsystem()
and invoke ssh with an explicit argv, but right now, you need to escape against both local and remote shells).["ls", $remote_dir]
into a single shell-safe string, and runningsystem("ssh", "-i", "id_pub", $ssh_addr, $safely_quoted_string)
.