ssh loop

This is the place for queries that don't fit in any of the other categories.

ssh loop

Postby avivfl » Sun Mar 30, 2014 8:32 am

Code: Select all
import socket
import libssh2

while True:
    try:
     username=raw_input("please enter username")
     password=raw_input("please enter password")
     sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect(("192.168.16.38",22))
     s=libssh2.Session()
     s.startup(sock)
     s.userauth_password(username,password)
     print "password incorrect or username"
    except s.userauth_authenticated(): break



i m trying to create loop that only when i enter the right username and password it will break, the look continue when i enterer the right username and always disconnect after 2 or 3 attempts, why?
10x
avivfl
 
Posts: 8
Joined: Sat Mar 29, 2014 8:13 am

Re: ssh loop

Postby tnknepp » Wed Apr 23, 2014 5:29 pm

What do you mean "always disconnect after 2 or 3 attempts"...you haven't even connected yet?
Is there really a need to loop in such a way? I have never used libssh2 (I use paramiko), so I do not fully understand its behavior (hence, I do not understand why you are using try/except here...but I assume it is for good reason). I'm not the best programmer, but this is what I would do (just add a counter).


Code: Select all
tries = 0
while tries < 3:
    try:
         tries+=1
         username=raw_input("please enter username")
         password=raw_input("please enter password")
         sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         sock.connect(("192.168.16.38",22))
         s=libssh2.Session()
         s.startup(sock)
         s.userauth_password(username,password)
         print "password incorrect or username"
     except s.userauth_authenticated():
         # On to the rest of what you want to do



This really depends on your application etc.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 123
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests