Net::SSH::Perl Help

V

vendion

I am trying to make a script that loops 26 times each time using SSH
to remote into a different machine every time and run a second
script. When I run the first one with SSH I get this error:

Permission denied at /home/e-307-20/bin/upgrader.pl line 13

I know that I can SSH into the machine because SSH alone works fine.
My code is as follows (user name and password has been replaced for
security reasons)

#!/usr/bin/perl
#upgrader.pl This is the server side of a two part perl script that
uses ssh to
#get into the lab PCs and have them run the upgrade.
use warnings;
use strict;
use Net::SSH::perl;

my $computer = '01'; #set the script for the first out of 26 computers
that use
#10.18.1.1xx ip scheme

while ($computer <= 26) {
my $ssh = Net::SSH::perl->new("10.18.1.1$computer");
$ssh->login('user', 'password');
my ($stdout, $stderr, $exit) = $ssh->cmd('upgrade.pl');
$computer ++;
}

exit 0

Any know what I am doing wrong with the authentication with the
script?
 
J

Jens Thoms Toerring

vendion said:
I am trying to make a script that loops 26 times each time using SSH
to remote into a different machine every time and run a second
script. When I run the first one with SSH I get this error:
Permission denied at /home/e-307-20/bin/upgrader.pl line 13
I know that I can SSH into the machine because SSH alone works fine.
My code is as follows (user name and password has been replaced for
security reasons)
#!/usr/bin/perl
#upgrader.pl This is the server side of a two part perl script that
uses ssh to
#get into the lab PCs and have them run the upgrade.
use warnings;
use strict;
use Net::SSH::perl;
my $computer = '01'; #set the script for the first out of 26 computers
that use
#10.18.1.1xx ip scheme
while ($computer <= 26) {
my $ssh = Net::SSH::perl->new("10.18.1.1$computer");

Did you print out the IP addresses? The first time round you
get 10.18.1.101, the seond time 10.18.1.12, etc. until $computer
is 10, then you get 10.18.1.110, 10.18.1.111 etc. That's because
in order to increment $computer it's converted to an integer,
and then the leading '0' goes missing.

I would rather guess that this wasn't your intention. Perhaps youd
should replace this with

my $ssh = Net::SSH::perl->new(sprintf("10.18.1.1%02d",$computer));

to get 101, 102, 103, etc. as the last part of the IP address.

Regards, Jens
 
J

John W. Krahn

vendion said:
I am trying to make a script that loops 26 times each time using SSH
to remote into a different machine every time and run a second
script. When I run the first one with SSH I get this error:

Permission denied at /home/e-307-20/bin/upgrader.pl line 13

I know that I can SSH into the machine because SSH alone works fine.
My code is as follows (user name and password has been replaced for
security reasons)

#!/usr/bin/perl
#upgrader.pl This is the server side of a two part perl script that
uses ssh to
#get into the lab PCs and have them run the upgrade.
use warnings;
use strict;
use Net::SSH::perl;

my $computer = '01'; #set the script for the first out of 26 computers
that use
#10.18.1.1xx ip scheme

while ($computer <= 26) {
my $ssh = Net::SSH::perl->new("10.18.1.1$computer");
$ssh->login('user', 'password');
my ($stdout, $stderr, $exit) = $ssh->cmd('upgrade.pl');
$computer ++;
}

Would be better written as:

for my $computer ( '01' .. '26' ) {
my $ssh = Net::SSH::perl->new("10.18.1.1$computer");
$ssh->login('user', 'password');
my ($stdout, $stderr, $exit) = $ssh->cmd('upgrade.pl');
}

Or:

for my $computer ( 1 .. 26 ) {
my $ssh = Net::SSH::perl->new( sprintf '10.18.1.1%02d', $computer );
$ssh->login('user', 'password');
my ($stdout, $stderr, $exit) = $ssh->cmd('upgrade.pl');
}

Or:

for my $computer ( 101 .. 126 ) {
my $ssh = Net::SSH::perl->new("10.18.1.$computer");
$ssh->login('user', 'password');
my ($stdout, $stderr, $exit) = $ssh->cmd('upgrade.pl');
}



John
 
J

John W. Krahn

Jens said:
Did you print out the IP addresses? The first time round you
get 10.18.1.101, the seond time 10.18.1.12, etc. until $computer
is 10, then you get 10.18.1.110, 10.18.1.111 etc. That's because
in order to increment $computer it's converted to an integer,
and then the leading '0' goes missing.

Really?

$ perl -le'
my $computer = "01";
print $computer;
$computer ++;
print $computer;
$computer ++;
print $computer;
'
01
02
03




John
 
T

Tad J McClellan

John W. Krahn said:
Really?

$ perl -le'
my $computer = "01";
print $computer;
$computer ++;
print $computer;
$computer ++;
print $computer;
'
01
02
03


For the description of what values $computer will get? Yes really.

----------------
#!/usr/bin/perl
use warnings;
use strict;

my $computer = '01';

while ( $computer <= 26 ) {
print "10.18.1.1$computer\n";
$computer++;
}
----------------

For the reason why it gets those values? No, not really.

It is not the incrementing that converts to a number it is the
logical test that causes the conversion.

----------------
#!/usr/bin/perl
use warnings;
use strict;

my $computer = '01';

foreach ( 1 .. 26 ) {
print "10.18.1.1$computer\n";
$computer++;
}
 
V

vendion

Did you print out the IP addresses? The first time round you
get 10.18.1.101, the seond time 10.18.1.12, etc. until $computer
is 10, then you get 10.18.1.110, 10.18.1.111 etc. That's because
in order to increment $computer it's converted to an integer,
and then the leading '0' goes missing.

I would rather guess that this wasn't your intention. Perhaps youd
 should replace this with

my $ssh = Net::SSH::perl->new(sprintf("10.18.1.1%02d",$computer));

to get 101, 102, 103, etc. as the last part of the IP address.

                              Regards, Jens

No I didn't think of that I didn't know that it would be persevere the
leading 0, but I got the permission error almost right after I ran it
which lead me to think that it didn't even connect to the first
computer which has the 10.18.1.101 ip. For interest in making this
script better I have applied one of the suggestions from the next
post:

for my $computer ( 01 .. 26 ) {
my $ssh = Net::SSH::perl->new("10.18.1.1$computer");
$ssh->login('user', 'password');
my ($stdout, $stderr, $exit) = $ssh->cmd('upgrade.pl');

}

A quick question on $stdout and $stderr will I need to add a separate
print line for each of those if I want there contents to be
displayed? It would be nice to be able to see what is being printed
out on the remote machines and see if anything fails while the second
script is running.
 
V

vendion

No I didn't think of that I didn't know that it would be persevere the
leading 0, but I got the permission error almost right after I ran it
which lead me to think that it didn't even connect to the first
computer which has the 10.18.1.101 ip.  For interest in making this
script better I have applied one of the suggestions from the next
post:

for my $computer ( 01 .. 26 ) {
        my $ssh = Net::SSH::perl->new("10.18.1.1$computer");
        $ssh->login('user', 'password');
        my ($stdout, $stderr, $exit) = $ssh->cmd('upgrade.pl');

}

A quick question on $stdout and $stderr will I need to add a separate
print line for each of those if I want there contents to be
displayed?  It would be nice to be able to see what is being printed
out on the remote machines and see if anything fails while the second
script is running.

Yes by adding

print "Now connection to machine $computer\n"; (using the 01 .. 26
number scheme)

I was able to verify that the permission problem is happening when
connecting to the first machine.
 
V

vendion

Quoth vendion <[email protected]>:





This is not what was suggested.

    ~% perl -le'print for 01..05'
    1
    2
    3
    4
    5
    ~% perl -le'print for "01".."05"
    01
    02
    03
    04
    05
    ~%

See "Range Operators" and "Auto-increment and Auto-decrement" in perlop
for the details.

Ben

Yea I caught onto that after I posted and I enclosed 01 and 26 in
single quotes which works, but I am still getting the permission
problems.
 
T

Tad J McClellan

Yea I caught onto that after I posted


If you get in the habit of executing code before you copy/paste
it into a post, you won't send folks off into the weeds for problems
that do not really exist.

but I am still getting the permission
problems.


Does the user you are logging in as have execute permission on upgrade.pl?

Can you execute some other program, say a shell script, that
has the same permissions and ownership as upgrade.pl?
 
J

J. Gleixner

vendion said:
[...]
Yea I caught onto that after I posted and I enclosed 01 and 26 in
single quotes which works, but I am still getting the permission
problems.

Be more precise.

SSH permission problem or permission problem running upgrade.pl?

For a quick test, change 'upgrade.pl' to 'date'. If that works,
then your SSH is OK and you have a permission problem running
the script. Make sure it's executable by the 'user'. To ensure
the permissions are the same on each server, you could change
'upgrade.pl' to something like: '/bin/ls -l upgrade.pl'.
 
V

vendion

If you get in the habit of executing code before you copy/paste
it into a post, you won't send folks off into the weeds for problems
that do not really exist.

Sorry about that, I try to but it slipped my mind at that moment.
Does the user you are logging in as have execute permission on upgrade.pl?

Can you execute some other program, say a shell script, that
has the same permissions and ownership as upgrade.pl?

The user I am working with on the remote machines is the root user
because the upgrade.pl script deals with the package management system
but the upgrade.pl script is user executable (0755 with chmod)
 
V

vendion

Be more precise.

SSH permission problem or permission problem running upgrade.pl?

When I run the script in question all I get is "Permission denied at /
home/e-307-20/bin/upgrader.pl line 13"
 
T

Tad J McClellan

vendion said:
When I run the script in question all I get is "Permission denied at /
home/e-307-20/bin/upgrader.pl line 13"


Did you lookup the message in perldiag.pod?

=item Permission denied

(F) The setuid emulator in suidperl decided you were up to no good.



Did you try the quick test?



Did it work?



Did you try that too?
 
V

vendion

Do you realise how much of a bad idea this is? You *really* shouldn't
even be allowing ssh access as root directly.

A much better idea would be to put instructions about the work to be
done (say, a list of packages to be added or removed) into a file
somewhere, and have a cronjob running as root that picks this up and
does the work. You need to be very careful you don't end up executing
arbitrary shell commands from across the network.

Ben

The machines I am working with are dual boot machines and are in the
Linux boot at different times making writing a cronjob that will have
the package management system, this case libzypp in SUSE, go out and
install any security/important updates, and any updates to KDE 4.2.x
hard. When they are in Linux boot jumping between 26 machines in
person to do this work gets very tedious which is why I went this
route to make the job easier. The SSH port is blocked on the network
firewall so SSH access out side of the LAN is not a problem, even then
if something did go wrong all I have to do is re-clone the affected
machines.
 
V

vendion

Please post a *short* *complete* script we can all run, the output you
expected to see and the output you actually got. 'Permission denied' is
not a message produced by perl itself, so we have no way of knowing what
you've done to provoke it unless you show us your code.

Ben

Sure here the code:

#!/usr/bin/perl
#upgrader.pl This is the server side of a two part perl script that
uses ssh to
#get into the lab PCs and have them run the upgrade.
use warnings;
use strict;
use Net::SSH::perl;

for my $computer ( '01' .. '26' ) { #loop once for each computer,
total 26 times
print "Making connection to E-307-$computer\n";
my $ssh = Net::SSH::perl->new("10.18.1.1$computer");
$ssh->login('user', 'password');
my ($stout, $sterr, $exit) = $ssh->cmd('upgrade.pl');
}
exit 0

and here is the output I get with this code:

e-307-20@E-307-20:~> upgrader.pl
Making connection to E-307-01
Permission denied at /home/e-307-20/bin/upgrader.pl line 11

The only thing is different is the line that it errors on because of
the change in the loop, also change the command upgrade.pl to date
gives the same permission error.
 
T

Tad J McClellan

vendion said:
#upgrader.pl This is the server side of a two part perl script that
uses ssh to


Is that one line or two lines?

for my $computer ( '01' .. '26' ) { #loop once for each computer,
total 26 times


Is that one line or two lines?

Permission denied at /home/e-307-20/bin/upgrader.pl line 11


Should we be looking at the 11th line or the 13th line of what
you posted?

Please either:

Disable word wrap when posting long lines

or, better, do not *have* any long lines in your code


#upgrader.pl This is the server side of a two part perl script
# that uses ssh to


for my $computer ( '01' .. '26' ) { #loop once for each computer,
# total 26 times
 
V

vendion

Is that one line or two lines?

Yes that is one line
Is that one line or two lines?

Same for that
Should we be looking at the 11th line or the 13th line of what
you posted?

If you diff what I recently posted to what I posted in my first
message (yes I did post my working code there too) then you will see
that in the recent copy I changed from a while loop to a for loop that
cut down on the number of lines in my code affecting where the "error"
is happening it is the same problem just now the line is 11 instead of
13.
 
J

J. Gleixner

vendion said:
That's the thing when I use just SSH same username and password
authentication is correct and I am able to login to the computers.

Are you running this program as the same user you used to
test SSH? Or is it secretly running as a CGI, or as some
other user?


I'd say, at this point, you should simplify your code and
ask for more verbose output.

use warnings;
use strict;
use Net::SSH::perl;

my $computer = '10.18.1.101';
print "Making connection to computer\n";
my $ssh = Net::SSH::perl->new($computer, debug => 1);
$ssh->login('user', 'password');
my ($stout, $sterr, $exit) = $ssh->cmd('date');
print "stout: $stout\n",
"sterr: $sterr\n",
"exit=$exit\n";

Possibly it's using a different ssh (e.g. /usr/bin/ssh vs
/usr/local/bin/ssh ) or SSH-2 or something is different
compared to running it in your shell.

Compare the output against running 'ssh -v' in your shell.

If that doesn't help, then fire up the debugger and step
through the code when it gets to the cmd method. That way
you can print out "@cmd" to see exactly what is being called.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top