在PERL中使用SSH运行bash命令 [英] running bash command using SSH in PERL

查看:172
本文介绍了在PERL中使用SSH运行bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请耐心等待。我对perl很陌生。我将HTML变量从HTML表单传递给PERL / CGI脚本。请参阅以下内容:

 #!/ usr / bin / perl 

use strict;使用警告;
使用CGI :: Carp; #向浏览器发送错误,而不是日志文件
使用CGI;
my $ cgi = CGI-> new(); #创建新的CGI对象
my $ name = $ cgi-> param('servername');
print $ cgi-> header('text / html');
print服务器名称是/ n,$ servername;
#system(/ var / www / cgi-bin / localbashscript $ servername);
#system(ssh $ servernameremotebashscript> localfile或显示回html);



基本上来自HTML表单,我需要传递服务器名称
我尝试使用system命令将servername传递给运行我的ssh命令的localbashscript。但是,我无法使其工作有人建议我在PERL中做SSH,但我不知道该怎么做。



总之,我必须调用一个bash脚本(remotebashscript)在远程服务器($ servername)上显示内容并返回到html,或者至少将其输出到本地文件。在运行remotebashscript之前,我需要做的第一件事就是设置我的环境变量。它在bash中。我通过获取.profile来设置我的env变量,然后执行remotebashscript并将其重定向到本地文件。

  ssh $ servername。 〜/ .profile; remotebashscript> localfile 

我不知道如何使用perl,并寻求你的帮助。我在下面试过并且没有工作。

pre $系统(ssh $ servername。〜/ .profile;

提前感谢您

解决方案

请:永远不要在系统调用中使用用户输入,至少不要试图消毒它们!假设用户不会通过输入字符串来破坏你的系统,这些字符串可以以某种方式逃避你想要做的事情并做其他事情。在这种情况下,类似于 192.168.0.1 rm -rf / 足以从你的ssh服务器中删除所有的文件请注意标准的做事方式,这是从来没有使用用户输入的执行命令。



有很多模块,甚至是标准的, Net :: SSH ,它可以为你做SSH。


Please be patient. I'm very new to perl. I'm passing a HTML variable from html form to a PERL/CGI script. Pls see the following

#!/usr/bin/perl

use strict; use warnings;
use CGI::Carp; # send errors to the browser, not to the logfile
use CGI;
my $cgi = CGI->new(); # create new CGI object
my $name = $cgi->param('servername');
print $cgi->header('text/html');
print "Server name is /n, $servername";
#system("/var/www/cgi-bin/localbashscript $servername");
#system("ssh $servername "remotebashscript" > localfile or display back to html );

Basically from the HTML form, I need to pass the server name. I tried using the system command to pass on the servername to "localbashscript" that runs my ssh command. But, I cannot get it to work. It was suggested that I do the SSH in PERL, but I have no idea how to do it.

In short, I have to call a bash script (remotebashscript) on the remote server ( $servername) and display the content back to the html or at least pipe it to a local file. The first thing I need to do before running the remotebashscript is to set my environment variables. This is how I would do it in bash. I set my env variable by sourcing the .profile and then I execute the remotebashscript and redirect it to a local file.

  ssh $servername ". ~/.profile;remotebashscript" > localfile

I have no idea how to achieve the same thing using perl and seek your help. I tried below and did not work

 system("ssh $servername ". ~/.profile; remotebashscript" ");

Thanking you in advance

解决方案

Please: Never, never, never use user input in a call to system, without at least trying to sanitize them! It is a terrible mistake to assume that the user won't break your system by entering strings that can somehow escape what you're trying to do and do something else. In this case, something like 192.168.0.1 rm -rf / would be sufficient to delete all files from your ssh server. Please heed the standard way of doing things, which is never using user input in a executed command.

There's plenty of modules, and even a standard one, Net::SSH, which can do SSH for you.

这篇关于在PERL中使用SSH运行bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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