如何在 Perl CGI 上使用 stdin

发布于 2024-09-15 15:10:46 字数 606 浏览 0 评论 0原文

我是 Perl 新手,我想在我的脚本上使用屏幕输入。这是我的脚本,我希望从键盘输入 IOS 命令。有人可以告诉我我错在哪里吗?我现在遇到的问题是脚本无法读取我的键盘输入,我不确定是否适用于我的情况。谢谢!!

# ### Show ######################################################### 
               $cmd = <STDIN>;
                chomp ($cmd);
               $show_error = "";
              if ($ssh_switch eq 'yes') {
                   ssh_access();
               }
               print "\n",h2($host . ' - ' . $cmd);
                @output=$cmd;
                print hr(),"\n";
               }
}
#########################################################################

I am new to perl and I want to use screen input on my script. here is my script and I want the IOS command to be enter from keyboard. can some one show me where I am wrong.the problem i have now the script not read my keyboard input, I am not sure if work on my case. thanks!!

# ### Show ######################################################### 
               $cmd = <STDIN>;
                chomp ($cmd);
               $show_error = "";
              if ($ssh_switch eq 'yes') {
                   ssh_access();
               }
               print "\n",h2($host . ' - ' . $cmd);
                @output=$cmd;
                print hr(),"\n";
               }
}
#########################################################################

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

信愁 2024-09-22 15:10:46

CGI 的设计目的是接收完整的 HTTP 请求,然后发出完整的 HTTP 响应。

它根本无法交互工作。

如果您想编写需要键盘输入的脚本,请不要使用 CGI。

CGI is designed to take in a complete HTTP request and then spit out a complete HTTP response.

It simply doesn't work interactively.

If you want to write a script that expects keyboard input, then don't use CGI.

可爱暴击 2024-09-22 15:10:46

事实上,CGI 确实使用了STDIN。它用于传递POST请求的主体。尝试这个脚本

#!/usr/bin/perl
print "Content-Type: text/plain\r\n\r\nYou entered: ";
print while <STDIN>;

并向其POST一些数据,例如

$ echo "Just test" | POST http://localhost/yourscript.pl
You entered: Just test

POST是来自LWP CPAN发行版的程序)

这样你就可以使用命令来指导你的脚本从 STDIN 读取,尽管它本身非常不安全!

In fact, CGI does uses STDIN. It is used to pass the body of POST request. Try this script

#!/usr/bin/perl
print "Content-Type: text/plain\r\n\r\nYou entered: ";
print while <STDIN>;

and POST some data to it, e.g.

$ echo "Just test" | POST http://localhost/yourscript.pl
You entered: Just test

(POST is a program from LWP CPAN distribution)

So you can direct your script with commands read from STDIN, although it is very unsecure as is!

如日中天 2024-09-22 15:10:46

CGI 确实允许通过 STDIN 输入;尝试CGI->new(\*STDIN)

尽管这可能不是您想要的输入方式。您能举例说明您的输入是什么样的吗?

啊,在我看来,您想要:

  1. 从命令行运行脚本,例如:perl scriptname 'Submit=1&Devices=router1&Devices=router2',然后提供STDIN 上的 cisco 命令(并获取 html 输出,这可能不方便),或从命令行

  2. 在浏览器中运行您的脚本,在这种情况下,您应该将 STDIN 用法替换为用于输入的输入标记命令并从该命名参数获取命令

再看一遍,我看到了您已经有一个输入标签“搜索”,您只是没有使用它。
尝试用 $cgi->param('search') 替换 并将搜索添加到“确保输入数据”检查中。

CGI does allow input through STDIN; try CGI->new(\*STDIN).

Though it may not be how you want to enter things. Can you give an example of what your input looks like?

Ah, it looks to me like you want to either:

  1. run your script from the command line, like: perl scriptname 'Submit=1&Devices=router1&Devices=router2' and then provide your cisco commands on STDIN (and get html output, which may be inconvenient), or

  2. run your script in a browser, in which case you should replace the STDIN usage with an input tag for entering commands and get the commands from that named parameter

Looking again, I see you already have an input tag "search", you just aren't using it.
Try replacing <STDIN> with $cgi->param('search') and adding search to the "make sure data was input" check.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文