http://qs1969.pair.com?node_id=11145446

prad001 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have a situation where in I am calling the below Perl script

if (param("lremail") && param("lot")) { my $address=param("lremailaddress"); my $lot=param("lot"); print a({-href=>"$dir/new.pl"},"Back to Top"),br; print "Request submitted for $address.",br; print "Lot $lot",br; print "You will receive an e-mail with a link to the data when the + request is complete."; print end_html; system ("ssh SERVERNAME /test/abc.csh $lot $$ $address &"); exit(1);

The above script does not run because, when I execute the script its asking for a system password. Then I researched a little and wrote the below command..

expect -c 'spawn ssh SERVERNAME /test/abc.csh J213520 06 abc@gmail.com + "ls -lh file"; expect "Password:"; send "PASSWORD\r"; interact'

Now this command is executed successfully without any issue but from the command line only. When I incorporate the same(by replacing the system call) within the Perl script, it fails. How can I incorporate this line within the perl script?

Replies are listed 'Best First'.
Re: How to incorporate Expect in Perl Script? OR Pass the password via perl script?
by Corion (Patriarch) on Jul 12, 2022 at 15:16 UTC

    Consider using a (passwordless) SSH key to store the access credentials for the remote machine.

    Your expect call likely fails because expect or ssh wants a (pseudo-)terminal connected to do their stuff.

      The expect command works fine from the command line but I am not sure how to incorporate that in to the perl script that I have mentioned..!
Re: How to incorporate Expect in Perl Script? OR Pass the password via perl script?
by atcroft (Abbot) on Jul 13, 2022 at 03:11 UTC

    Just to reinforce what tybalt89 said, there are several Expect-related modules in perl. Among them are:

    If I've left out someone's favorite, I apologize--I think I have used each of those in the past on various projects. I should note there are also several modules that play nicely with Expect, such as Net::OpenSSH and its variants.

    I would suggest you take Corion's suggestion to heart and consider looking at (passwordless) SSH key access. Under OpenSSH keys can be locked down to allow only from a particular set of hosts and/or to allow execution of only certain commands you specify, both of which may prove useful in increasing the security of the system you are assembling.

    Hope that helps.

      There’s also IPC::Run which will let you drive a subprocess while looking for responses with regexen.

      And seconding being careful locking down what can be run especially something accessible via a web server. Might even consider a harder separation like maybe enqueuing requests that something else (not running as your httpd user) then runs with the elevated access separately (Minion, or dropping in a db, or a file in a directory monitored by something to run things, …).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Re: How to incorporate Expect in Perl Script? OR Pass the password via perl script?
by tybalt89 (Monsignor) on Jul 12, 2022 at 20:59 UTC

    perl has an Expect.pm

Re: How to incorporate Expect in Perl Script? OR Pass the password via perl script? (plz stop reposting)
by LanX (Saint) on Jul 12, 2022 at 15:08 UTC
    please stop reloading /reposting your question, youve already created half a dozen duplicates!

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery