Perl command-line based script, running it on a website? - perl

I have a Perl script that works perfectly when I run it from my command prompt. But when I upload it to the cgi-bin folder on my website, it doesn't work. Is there a special format it needs to be or something? I'm totally new to Perl.
Also note: the test hello page Perl script works.

When you run your perl script under cgi (which you indicated is working), the query is passed to your script as STDIN, and the output of your script is sent to the browser.
There will not be a prompt, or anything like that, you need something else (such as a web page) to prompt the user for anything you need and submit it to your script.
The next page on from Bill's link (http://www.lies.com/begperl/form_to_email.html) deals with this.

Related

get user machines current working directory from perl cgi

i am trying to get the current working directory path using Perl
when i execute from ubuntu: $root#ubuntu:/var/test/geek# firefox http:/localhost/test.html, i get /var/cgi-bin as output in perl cgi page instead of /var/test/geek.
used perl code:
my $pwd=cwd();
bla bla
print "<h1> pwd </h1>";
above code gives path of test.pl not users working directory path
Edit: When i run the script alone from the terminal it works fine. for example:
$root#ubuntu:/var/test/geek# /var/cgi-bin/test.pl
i get /var/test/geek. but when i call the script in html page using submit button it gives path of perl script.
Each process has its own working directory that it inherits from its parent when it gets created.
cwd() returns the current process's working directory.
For a CGI script, the browser doesn't pass its working directory to the server as part of the request. To obtain that, you need to have code running on the client system that submits it. That might be an application that the user download, or possibly, but unlikely, some in-browser code, like Javascript / a Java applet (This info is likely hidden from in-browser code for security reasons though).
(The rest assumes Linux, it will likely differ on other operating systems)
The part below assumes that you are looking for the working directory of a user on the server:
In order to get a specific shell for a specific user's working directory, you would need to identify the PID for the shell and get the working directory from the /proc/<pid>/cwd symlink (To read these, the process must belong to the user running the code, or the code must run as root (Which is a bad idea for a CGI script)...). To get the PID of the shell, you likely need to start from the w command output, or its data source, /var/run/utmp. Sys::Utmp might be useful for this... You might then also need to retreive a whole lot of extra info to find all the processes that might have the working directory that you are looking for.
I think you are mixing the web server and the local user. The web server has a working directory when you run the script, and that is the one that cwd() returns.

In Perl CGI, how can I use UNIX commands?

I'm trying to run ssh, mkdir from a Perl CGI script. It's not working. But in normal Perl script, it is working fine. Can any one tell me how to run commands in a Perl CGI script?
If you're running this script via a webserver, chances are the active user (e.g. "nobody", "www", etc) may not have the necessary privileges to execute commands like mkdir and ssh. If so, that is not something that perl can fix. You can test who the active user is with something like:
print "The active user is: ", `whoami`;
It is also a security concern, to have your web user privileges set to create files and perform commands.
system() or popen() are probably what you're looking for, if you're feeling dirty I think you can use back ticks too.
Do you need to run unix commands? Perl has a built-in mkdir, and there are modules to handle SSH. Normally a CGI process is going to have limited capabilities or access to the system. The more you can do in Perl the better.

PowerShell: send console output to file without deafening this console output

I have a lot of PowerShell script. One main, that calls other, child ones. Those PS scripts in their turn call windows CMD scripts, bash scripts and console applications. All these scripts and applications write messages to console. PowerShell scripts, for example, are using Write-Host scriptlet for this purpose.
Question: how can I easely redirect (send) all this console output to some file, while not deafening (canceling) this console output? I want to be able to see whats going on from console output and also have history of messages in log file.
Thanks.
You can use the tee equivalent of PowerShell : Tee-Object
PS: serverfault.com and/or superuser.com are more suitable for a question like this.
You can try Start-Transcript and Stop-Transcript. It has a couple of limitations like not capturing native exe output. It is also global to PowerShell session.
I've found script for grabbing console output: http://gallery.technet.microsoft.com/scriptcenter/e8fbffde-7d95-42d9-81de-5eb3d9c089e0. Script returns HTML to preserve colors.
The only big downside - you must call it at the end of your script to capture all console output it have made.
You'd probably need to write a custom host to do this. It's not a terribly hard thing to do, but it's does require some managed code.

How can I communicate with an application that does not return to the command prompt?

I need to build a test bench by sending appropriate inputs to an application. However,
once I launch the application, it takes control and does not return to the command
prompt (unless an exit command is executed from the application). In that case
is there any technique by which I can send a command to that application from the Perl
script and interpret the output from that application?
My operating system is Windows.
If it's a GUI application, take a look at the Win32::GuiTest module. It sends events to GUI applications - simulating user input.
For a command line application, I would normally recommend the Expect module. Unfortunately, Expect doesn't work under Windows.
If there is anyway to write or redirect the application output to a file, you can always open that file to process/interpret the output. If you are talking about a command-line application, it should be easy to redirect the terminal output to a file using the '>' and '>>' characters. It may not be as easy with a GUI app, though.

Start a groovy script directly from its url

I have a script that is available on the web, and I want to run it directly by entering its url, instead of downloading it and running later. Is there a way to do it ?
If you want to run a groovy script from a URL from the command line, you can do the following:
groovy -e 'evaluate( new URL( "http://yoururl.com/yourscript.groovy" ).text )'
Edit
Since Groovy 1.8.3, it is possible to do:
groovy http://myserver/myScript.groovy
Obviously care must be taken with doing this either way... Running stuff straight off the internet is never best advised ;-)