使用 SSH,从另一个 perl 文件远程执行 perl 文件并传递包含空格的参数的最佳方法是什么? [英] Using SSH, what is the best way to remotely execute a perl file from another perl file and pass arguments that contain spaces?

查看:49
本文介绍了使用 SSH,从另一个 perl 文件远程执行 perl 文件并传递包含空格的参数的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所拥有的-服务器 A 上的 CGI 脚本获取在网络表单中输入的参数,创建一个远程 ssh 命令,该命令调用服务器 B 上的另一个 perl 文件,并将所有参数作为参数传递给 ash 命令.服务器 B 上的 Perl 文件解析参数 n 之后进行操作.

Here is what I have- A CGI script on server A gets the parameters entered in a webform, creates a remote ssh command that calls another perl file on server B and passes all parameters as arguments to the ash command. Perl file on server B parses arguments n operates thereafter.

现在,由于我不会一直填充所有参数,因此我在服务器 A 上和服务器 B 上处理之前从 CGI 的每个参数中传递了一个字符,例如=",我砍掉了最后一个字符,即一个=".

Now since I won't have all parameters populated at all times, I am passing a character such as "=" appended to each parameter from CGI on server A and before processing on Server B, I chop that last character which is an "=".

但是,对于可以有空格的参数,这会失败.为了解决这个问题,我可以在附加="之前将每个参数包含在///"(基本上是一个斜线转义然后是一个斜线来转义另一个斜线)(一旦我将每个参数都括起来,它可能会被丢弃)),但这是实现我想要实现的目标的最佳方式吗?

However, this fails for a parameter that can have space. To counter this, I can enclose each parameter within ///" (basically a slash to escape " and then a slash to escape the other slash) before I append the "="(which probably can be discarded once I enclose each param anyways), but is this the best way to do what I want to achieve?

推荐答案

与其调用命令并读取其输出,我建议使用 IPC::Open2IPC::Open3 并使用您的进程 B 作为过滤器:写入,读出.此时,您完全消除了外壳.而且,根据我的经验,消除外壳总是一件好事.

Rather than calling the command and reading its output, I'd suggest using IPC::Open2 or possibly IPC::Open3 and using your process B as a filter: write in, read out. At this point, you completely eliminate the shell. And, in my experience, eliminating the shell is always a good thing.

然后,为了让事情变得非常简单,你通过 IPC::Open[23] 运行命令,向它写入序列化数据(通过 JSON 或 Storable),关闭写入管道(这样另一端得到 eof),并等待数据.在另一端,读取 stdin,反序列化,使用它,然后再次序列化您的返回数据.回到 CGI 服务器,反序列化您收到的数据,然后就可以了.如果您需要进行两次或三次弹跳(ssh 通过一台机器 ssh 到另一台机器),这也非常有效.

And then, to make things really simple, you run the command via IPC::Open[23], write serialised data to it (via JSON or Storable), close the write pipe (so the other side gets eof), and wait for the data. At the other end, read stdin, deserialise it, use it, and serialise your return data again. Back at the CGI server, deserialise the data you received, and away you go. This also works really well if you need to do double or triple bouncing (ssh through one machine to ssh to another).

可以放入足够多的引号以使其按您想要的方式工作.这是中度疼痛.但是,这里有一些关于这个方向的提示.首先,在 CPAN 上找到一些可以为您处理报价的内容.String::ShellQuote 可能会起作用.其次,尽量避开外壳.也就是说,不是使用 open my $pipe, "ssh $server '$cmd' |",而是使用 open my $pipe, '-|', 'ssh', $server,$cmd.这将避免本地shell.这减少了您不必担心引用任何内容的次数.但是,每次执行另一个反弹时,您都必须重新引用所有内容,因为每台远程机器仍将使用远程 shell.

It is possible to put in enough quotes to get it to work the way you want. It's moderately painful. However, here are some hints in that direction. First, find something on CPAN which handles the quoting for you. String::ShellQuote might work. Second, avoid the shell as much as possible. That is, instead of using open my $pipe, "ssh $server '$cmd' |", use open my $pipe, '-|', 'ssh', $server, $cmd. This will avoid the local shell. And that reduces the number of times you have to worry about quoting anything. But you will have to re-quote everything every time you do another bounce as each remote machine will still be using the remote shell.

这篇关于使用 SSH,从另一个 perl 文件远程执行 perl 文件并传递包含空格的参数的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆