Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Expect receives no output, when requested via HTML

by Corion (Patriarch)
on Nov 24, 2020 at 13:09 UTC ( [id://11124133]=note: print w/replies, xml ) Need Help??


in reply to Expect receives no output, when requested via HTML

... but when the sub is invoked via the html interface, ...

As a first step, I would assume that the user your web server launches your program as is not the user that you're using on the console, or that it has less permissions. Also, it won't have an interactive terminal connected, so ssh will not interactively ask for user/passwords (maybe).

To test this "interactivity" idea, consider launching your program with STDIN redirected:

/path/to/program </dev/null

If that also fails, or in any case, I recommend moving away from using passwords with SSH and instead using a (passwordless) key. This is more or less the same security, but at least allows you to separate the permission to read the source code of your program from the permission to read the file allowing the remote access.

In any case, you should limit the key on the remote machine to the specific set of commands your program will be issuing.

Replies are listed 'Best First'.
Re^2: Expect receives no output, when requested via HTML
by Clive89 (Initiate) on Nov 24, 2020 at 14:13 UTC

    If have executed the following code with input redirected to dev/null

    my @cmds = ("sh int des"); # define comma +nds. my @hosts = ("XXX", "XXX"); # define hosts. my @params = (\@cmds, \@hosts); # construct pa +rameter array (currenlty mandatory for execution in a sub process). my $job = new Job(\&exec_on_each, \@params); # define job Dispatcher::query_job($job); # add jobs to +the dispatchers queue. my @out = Dispatcher::execute_jobs(); # execute all +jobs and retieve the result. print Tools::to_string(\@out); # print the re +sult in the console.
    Surprisingly this returns the result as expected. I think the user that executes the script via the website has insufficent rights to start SSH.

      You launch ssh without an absolute path name.

      Have you made sure that $ENV{PATH} is the same when run via the web server as when run via the console?

      Have you inspected the error message you get? $exp->error() might provide more information...

        Thank you so much for your assistance! The subprocess that starts the ssh connection set the $ENV{"PATH"} = "/usr/bin"; (required by expect because of possible tainted return) The $exp->error() returns -1 after each interaction. Unfortuantly i'm very limited on the system i use, and i do not have access to Net::SSH::Any or IPC.
        # assigns a job to a worker process. # :param 0: job. # :return: sub assign_job { my ($job) = @_; my $pid = open my $pipe => "-|"; # create worke +r process and connect it using a pipe. Debug::dlog("spawned process : $pid"); die "Failed to fork: $!" unless defined $pid; # check that c +reation was successfull. my $routine = $job->get_routine(); # get routine +to execute. my $params = $job->get_params(); # get paramete +rs to pass. unless ($pid) { # the followin +g code is only executed in the worker process: $ENV{"PATH"} = "/usr/bin"; # delete conte +xt. my $return = $routine->(@$params); # execute rout +ine. my $dump = Dumper($return); # serialize th +e returned object. print($dump); # print data s +tring to pipe. Debug::dlog("process ".$$." dumped ".length($dump)." chars"); exit; # terminate pr +ocess. } else { # only in pare +nt process: my $worker = new Worker($pipe, $pid); # construct ne +w worker object. push(@Dispatcher::workers, $worker); # save worker +object. } }
Re^2: Expect receives no output, when requested via HTML
by Clive89 (Initiate) on Nov 24, 2020 at 14:05 UTC

    Thanks for your reply! I think i understand the issue now. When there is no terminal, there can't be any interaction. Unfortunately i can not establish a passwordless connection (for reasons i would like to ommit). I will test as you advised. I just wonder, even if i connect with a key, will i be able to retrieve the devices response without any terminal? Is there another library for perl, that works without a terminal?

    Friendly Regards,

    Phil

      Personally, for interactive stuff, I would look at Net::SSH::Any or any of the modules that Net::SSH::Any uses. There you can conveniently send commands/input over a channel without needing Expect at all.

      Alternatively, you could look at IPC::Run for opening a child process (in your case, the ssh connection) for reading and writing, but this goes close to reimplementing Expect yourself and won't solve the password issue.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124133]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-18 15:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found