ssh2 foreach loop issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ssh2 foreach loop issue
# 1  
Old 09-24-2012
ssh2 foreach loop issue

Hello Everyone,

I have the following codes that only works for the first login element. I can't get it work with the second and third login credentials. Can anyone here see the problem?

I ran the code with host1, it works as expect. see below:
Code:
# ./test.pl host1

Trying to connect host1...
Logging in.....user1=>pass1
Login sucess


But when run with host2, it fails with 3 logins,  which should be passed with the second set of login credentials.  See below:
# ./test.pl host2

Trying to connect host2...
Logging in.....user1=>pass1
  Error: unable to login with user: user1=>pass1

Trying to connect host2... 
Logging in.....user2=>pass2
  Error: unable to login with user: user2=>pass2
  
Trying to connect host2...
Logging in.....user3=>pass3
  Error: unable to login with user: user3=>pass3

Error: Failed to login to host2

Code:
my @logins = ("user1 pass1", "user2 pass2", "user3 pass3");
my $ok = 0;
while ( $ok == 0 ) {
foreach (@logins) { 
my ($user, $pass) = split('\s+'); 
print "\nTrying to connect $host...\n"; 
$ssh2->connect($host);
print "Logging in.....$user=>$pass\n";
unless ( $ssh2->auth_password($user,$pass) ) {
print "  Error: unable to login with user: $user=>$pass\n";
next;
} else {
print "Login sucess\n";
$ok = 1;
last;
}
}
# failed to login
if ($ok == 0) {
print STDERR "\nError: Failed to login to $host\n";
exit 2;
}


Last edited by Corona688; 09-24-2012 at 06:27 PM.. Reason: Code Tags
# 2  
Old 09-24-2012
Just a hip shot, but the first connection may be still connected expecting another password on the second pass, so the second connect is discarded. You may need to destroy the object and make anew. Why not just select the right one by host, since it has embedded passwords already, and ssh login is slow enough!
# 3  
Old 09-24-2012
You should be using ssh keys, not perl modules, to automate this. Stored plaintext passwords are almost impossible to keep safe.
# 4  
Old 09-25-2012
Thanks everyone!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using the Foreach loop, Needing help

I am trying to make a script for my Counter-Strike: Source servers. What i am wanting it to do is for it to restart each server, the only way i can think of doing this in through for each. Years what i have at the moment. server_start() { START=`ps x | grep SCREEN | grep $SRV | cut -d '?' -f... (5 Replies)
Discussion started by: grahamn95
5 Replies

2. Shell Programming and Scripting

Foreach issue

Hello, I found that this foreach should work with two lists (source: Wikipedia.org) foreach i {1 2 3} j {a b c} { puts "$i $j"} == I try smth. like: With two text files: first.part second.part foreach first (`cat first.part`) second (`cat second.part`) toolcommand $first... (22 Replies)
Discussion started by: unknown7
22 Replies

3. Shell Programming and Scripting

Perl ssh2 login issue.

Hi Experts, I came across this interesting situation. I have following ssh script login to multiple server. This works fine for one set of servers (linux) but on my sun boxes i am getting. error #!/usr/bin/perl -w use Net::SSH::Perl; use POSIX; use Term::ANSIColor qw(:constants); use... (2 Replies)
Discussion started by: mtomar
2 Replies

4. Shell Programming and Scripting

Using sed with a foreach loop

So I am back again beating my head against the wall with a shell script and getting a headache! I want to change each year in a file (1980, 1981, 1982, 1983, etc.) to the same year followed by a tab. The input is "blah blah (1980) blah blah". I want to get "blah blah (1980 ) blah blah".... (2 Replies)
Discussion started by: Peggy White
2 Replies

5. Shell Programming and Scripting

ssh2-keygen trust issue

I have two systems SysA & SysB having the same userid sharing the home directory via NFS mount. I need to know the steps to setup ssh trust between these two systems given that both share the home dir. I have tried all the steps to generate the keys & then creating identification &... (2 Replies)
Discussion started by: deo_kaustubh
2 Replies

6. UNIX for Advanced & Expert Users

Problem with foreach loop

Hi All, Is there any problem with the below 'foreach' loop? foreach risk_factor ($(cat "$rf_list")) where "rf_list=$SCRIPT/Utility/rflist.txt " I'm wondering, it is throwing below error message: syntax error at line 34: `(' unexpected Any idea/suggestions ? Thanks in advance /... (7 Replies)
Discussion started by: ganapati
7 Replies

7. Shell Programming and Scripting

foreach loop

Hi everyone Does anyone know what is wrong with this script. i keep getting errors foreach filename (`cat testing1`) set string=$filename set depth=`echo "$string" echo $depth end the error is the following testing: line 1: syntax error near unexpected token `(' testing: line 1:... (3 Replies)
Discussion started by: ROOZ
3 Replies

8. Shell Programming and Scripting

foreach loop + 2 variables

In a foreach loop, is it possible for the loop to go through 2 arguments instead of one i.e. instead of foreach i (do stuff for i), we have foreach i j(do stuff for i; do stuff for j) I am working under BASH and TCSH shell environments cheers (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. Shell Programming and Scripting

foreach loop

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 Replies

10. Shell Programming and Scripting

Foreach loop

What am I doing wrong with this foreach loop? foreach var ($argv) @sum = $sum + $var (4 Replies)
Discussion started by: haze21
4 Replies
Login or Register to Ask a Question