Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 06-21-2018, 06:25 AM
winniec winniec is offline
Registered User
 
Join Date: Jun 2018
Posts: 5
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")
Reply With Quote
  #2  
Old 06-21-2018, 10:12 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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?)
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 06-21-2018, 09:44 PM
winniec winniec is offline
Registered User
 
Join Date: Jun 2018
Posts: 5
Quote:
Originally Posted by bgagnon View Post
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.
Reply With Quote
  #4  
Old 06-22-2018, 08:17 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730

Last edited by bgagnon; 06-22-2018 at 08:25 AM.
Reply With Quote
  #5  
Old 06-24-2018, 11:48 PM
winniec winniec is offline
Registered User
 
Join Date: Jun 2018
Posts: 5
Quote:
Originally Posted by bgagnon View Post
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")
Reply With Quote
  #6  
Old 06-25-2018, 07:26 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
Hi winniec,

Awesome, thanks for posting the update! Have a great week.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply

Tags
next tab , password , prompt

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 10:22 AM.