2

I want to run a command over SSH and make it loop until my variable has read all the lines from a file .

I have this :

$channel = $ssh->channel();
$channel->exec('echo -n "$command"') 
$channel->exec('rest of commands')

What I need to do is to run that echo command with the variable being each line from my local file /home/variables in a loop.

It should keep looping the echo command until all the lines from my file are finished before it moves to the rest of the script.

I thought I should use something like :

open my $enable, '<', '/home/variables';

while (my $command = <$enable>) {
chomp $command;
$channel->exec("echo -n $command");
last;
$channel->exec('next command');

It's not really looping though .

Thanks in advance

0

1 Answer 1

1

Try this:

#-- creating a channel
my $channel = $ssh->channel();
$channel->shell();

open my $enable, '<', 'x1';

while (my $command = <$enable>) {
  $channel->write( $command );
  }

$channel->write( $final_command );

Also note that you need a newline after each command so I left out the chomp.

From the Net::SSH2 documentation :

"Note that only one of these requests can succeed per channel (cp. "exec" in perlfunc); if you want to run a series of commands, consider using shell instead."

I hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.