VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Python The new password can not pass on the next tab (https://forums.vandyke.com/showthread.php?t=13150)

winniec 06-21-2018 07:25 AM

Python The new password can not pass on the next tab
 
This is the purpose of the script:
1. Read the ip address of the SessionList.txt
e.g:
SessionList.txt
192.168.11.165
192.168.11.161

2. Connect each ip address via a separate tab.
3. Prompt user/password for the user to login. After that, the two variables can pass on the remaining tab. The user only need to input once.

However, I want the user/password window pop up again if I input a wrong user/pssword the first time.
I tried to accomplish the task like this. However, it could not pass to the second tab....Could you please let me know why? Thank you!

Code:

import re
import os
import subprocess
SCRIPT_TAB=crt.GetScriptTab()
SCRIPT_TAB.Screen.Synchronous=True
SCRIPT_TAB.Screen.IgnoreEscape = True

def CreateObjTab(user,password,session):
    objNewTab = crt.Session.ConnectInTab("/TELNET %s 23" % session)
    objNewTab.Screen.WaitForString("login:")
    objNewTab.Screen.Send(user + "\r")
    objNewTab.Screen.WaitForString("Password:")
    objNewTab.Screen.Send(password + "\r")
    while not objNewTab.Screen.WaitForString(">",3):
        x = Parent(crt.Dialog.Prompt("Enter user name:", "Login", "", False),
                  crt.Dialog.Prompt("Enter password:", "Login", "", True))
        user = x.u
        password = x.p
        objNewTab.Screen.WaitForString("login:")
        objNewTab.Screen.Send(user + "\r")
        objNewTab.Screen.WaitForString("Password:")
        objNewTab.Screen.Send(password + "\r")
    objNewTab.Screen.Send("enable\r")
    objNewTab.Screen.WaitForString("Password:")
    objNewTab.Screen.Send("casa\r")
    objNewTab.Screen.WaitForString("#")

class Parent():
    def __init__(self, user, password):
        self.u=user
        self.p=password


def AutoConnectTab(file):  ###Auto connect sessions from a txt
    ####This is to Open the "SessionList.txt" and get a list of the ip address
    if not os.path.exists(file):
        return
    sessionFile = open(file, "r")
    sessionArray = []
    for line in sessionFile:
        session = line.strip()
        if session:
            sessionArray.append(session)
    sessionFile.close()
    # Receive variable

    x = Parent(crt.Dialog.Prompt("Enter user name:", "Login", "", False),
              crt.Dialog.Prompt("Enter password:", "Login", "", True))
    user = x.u
    password = x.p

    for session in sessionArray[0:]:
        try:
            CreateObjTab(user,password,session)
        except ScriptError:
            pass
        if not SCRIPT_TAB.Session.Connected:
            return

AutoConnectTab(os.getcwd()+"\SessionList.txt")


bgagnon 06-21-2018 11:12 AM

Hi winniec,

Please elaborate on the behavior:
Quote:

However, it could not pass to the second tab.
Did the script fail with an error?

Or was the script still running? (ie: Is Cancel available from the Script menu and if so, when you choose it, what line of the script is indicated?)

winniec 06-21-2018 10:44 PM

Quote:

Originally Posted by bgagnon (Post 49764)
Hi winniec,

Please elaborate on the behavior:


Did the script fail with an error?

Or was the script still running? (ie: Is Cancel available from the Script menu and if so, when you choose it, what line of the script is indicated?)

Hi Brenda,

In the next tab, the window pops up again and I need to input the user/password again.

Thank you very much for you help.

bgagnon 06-22-2018 09:17 AM

Hi winniec,

It's clear your Python knowledge exceeds mine (use of classes and such), so I am afraid I won't be much help figuring out where the issue is in that script. :)

You might just use the tried and true debug method of sprinkling in some message boxes so that you can be sure the variables contain what you think they should (previously entered credentials).

If I get some free cycles and can determine the cause, I will post here.

My initial suspicion is the prompt for that connection maybe does not end in the character you think it does?
while not objNewTab.Screen.WaitForString(">",3):
That would seem to bring up the prompt for credentials when it's not expected.

winniec 06-25-2018 12:48 AM

Quote:

Originally Posted by bgagnon (Post 49771)
Hi winniec,

It's clear your Python knowledge exceeds mine (use of classes and such), so I am afraid I won't be much help figuring out where the issue is in that script. :)

You might just use the tried and true debug method of sprinkling in some message boxes so that you can be sure the variables contain what you think they should (previously entered credentials).

If I get some free cycles and can determine the cause, I will post here.

My initial suspicion is the prompt for that connection maybe does not end in the character you think it does?
while not objNewTab.Screen.WaitForString(">",3):
That would seem to bring up the prompt for credentials when it's not expected.

Thank you. I have figured it out. Please check the below.

Code:

class Account:
    def __init__(self):
        self.user = crt.Dialog.Prompt("Enter user name:", "Login", "", False)
        self.password = crt.Dialog.Prompt("Enter password:", "Login", "", True)

def CreateObjTab(user, password, session):
    objNewTab = crt.Session.ConnectInTab("/TELNET %s 23" % session)
    objNewTab.Screen.WaitForString("login:")
    objNewTab.Screen.Send(user + "\r")
    objNewTab.Screen.WaitForString("Password:")
    objNewTab.Screen.Send(password + "\r")
    while not objNewTab.Screen.WaitForString(">", 3):
        user = crt.Dialog.Prompt("Enter user name:", "Login", "", False)
        password = crt.Dialog.Prompt("Enter password:", "Login", "", True)
        objNewTab.Screen.WaitForString("login:")
        objNewTab.Screen.Send(user + "\r")
        objNewTab.Screen.WaitForString("Password:")
        objNewTab.Screen.Send(password + "\r")
        return user, password
    objNewTab.Screen.Send("enable\r")
    objNewTab.Screen.WaitForString("Password:")
    objNewTab.Screen.Send("casa\r")
    objNewTab.Screen.WaitForString("#")

def AutoConnectTab(file):  ###Auto connect sessions from a txt
    ####This is to Open the "SessionList.txt" and get a list of the ip address
    if not os.path.exists(file):
        return
    sessionFile = open(file, "r")
    sessionArray = []
    for line in sessionFile:
        session = line.strip()
        if session:
            sessionArray.append(session)
    sessionFile.close()
    # Receive variable

    account = Account()
    account.user,account.password =CreateObjTab(account.user, account.password, sessionArray[0])
    for session in sessionArray[1:]:
        try:
            CreateObjTab(account.user, account.password, session)
        except ScriptError:
            pass
        if not SCRIPT_TAB.Session.Connected:
            return


AutoConnectTab(os.getcwd() + "\SessionList.txt")


bgagnon 06-25-2018 08:26 AM

Hi winniec,

Awesome, thanks for posting the update! Have a great week. :)


All times are GMT -6. The time now is 11:19 PM.