Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 07-29-2015, 03:09 AM
netsecrets00 netsecrets00 is offline
Registered User
 
Join Date: Jul 2015
Posts: 10
Script issue for username password

Dear All, Pl help with the vbs script for passing username/ password to cisco switch. If user1/pass1 incorret then user2/pass2 if incorrect then user3/pass3.
then execute command.

below is my script.
------------------------------------------------------
#$language = "VBScript"
#$interface = "1.0"

crt.Screen.Synchronous = True

Sub Main

Dim vHosts(3)
vHosts(0) = "10.49.2.1"
vHosts(1) = "10.49.2.2"
vHosts(2) = "10.49.2.3"

For Each strHost In vHosts
If strHost = "" Then Exit For

crt.Screen.Send "telnet " & strHost & chr(13)

if Crt.Screen.WaitForString ("login: ",10) = True then

crt.Screen.Send "user1" & vbCr

crt.Screen.WaitForString "password: "

crt.Screen.Send "password1" & vbCr

if Crt.Screen.WaitForString ("login: ",10) = True then

crt.Screen.Send "user2" & vbCr

crt.Screen.WaitForString "password: "

crt.Screen.Send "password2" & vbCr

if Crt.Screen.WaitForString ("login: ",10) = True then

crt.Screen.Send "user3" & vbCr

crt.Screen.WaitForString "password: "

crt.Screen.Send "password3" & vbCr

End If
End If
End If


' crt.Screen.Send "Show isis nei | in PAR" & vbCr
' crt.Screen.WaitForString "#"
' crt.Screen.Send "exit" & vbCr

'crt.Screen.WaitForString "#"

'crt.Screen.Synchronous = false
End Sub
Reply With Quote
  #2  
Old 07-29-2015, 08:44 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi netsecrets00,

Thanks for the post. I would suggest changing how you handle authentication. We have an example script at the following location that might help:
https://forums.vandyke.com/showthrea...7087#post37087
You could add a counter and based on the count, you could send a different username and password.

Does this help you accomplish your goal?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 08-04-2015, 01:50 AM
netsecrets00 netsecrets00 is offline
Registered User
 
Join Date: Jul 2015
Posts: 10
Hi Todd,

Thanks for your reply. I have check the link you have shared. I have done the changes in script as below. Now its working for TACAS and Non TACAS username / Password. devices file have IP address and commands file having common command for all routers.

----------------------------------------------------------------------------------
#$language = "VBScript"
#$interface = "1.0"

crt.Screen.Synchronous = True

sub Main
Const DEVICE_FILE_PATH = "N:\xxxx\devices.txt"
Const COMMANDS_FILE_PATH = "N:\xxxx\commands.txt"

dim fso
set fso = CreateObject("Scripting.FileSystemObject")

dim fil
set fil = fso.OpenTextFile(DEVICE_FILE_PATH)

'turn on synchronous mode so we don't miss any data

crt.Screen.Synchronous = True

While Not fil.AtEndOfStream
ip = fil.ReadLine

crt.Screen.Send "telnet " & ip & vbcr

dim result

result = crt.Screen.WaitForStrings("username: ", "Username: ", 10)

If result = 1 Then
crt.Screen.Send "user1" & vbCr
crt.Screen.WaitForString "password: "
crt.Screen.Send "pass1" & vbCr
End If

If result = 2 Then
crt.Screen.Send "user2" & vbcr
crt.Screen.WaitForString "Password: "
crt.Screen.Send "pass2" & vbCr
End If

dim com
set com = fso.OpenTextFile(COMMANDS_FILE_PATH)

While Not com.AtEndOfStream

ioscommand = com.ReadLine
crt.Screen.Send ioscommand & vbcr
crt.Screen.WaitForString "#"
wend

com.Close

crt.Screen.Send "exit" & vbcr
crt.Screen.WaitForString "#"

wend

fil.Close

'turn off synchronous mode to restore normal input processing
crt.Screen.Synchronous = False

end Sub
-------------------------------------------------------------------------------------
Now I am facing issue, If any IP from devices list is not reachable, scripts halts. How can I resolve the issue if device is not reachable.

What I want is if device is not reachable then script should skip that device and continue with next IP and execute the script.
Reply With Quote
  #4  
Old 08-04-2015, 08:02 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi netsecrets00,

I pointed you to a good example script, but not the one that I intended to suggest. Here is the script that illustrates how to handle many different situations when connecting and authenticating to a remote server:
https://forums.vandyke.com/showthrea...7087#post37087
You will notice that the interaction happens in a loop waiting for any possible response that the remote server might send. This allows you to handle almost any situation.

Does this example help you get your script working the way that you want?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 08-04-2015, 10:15 AM
netsecrets00 netsecrets00 is offline
Registered User
 
Join Date: Jul 2015
Posts: 10
Hi Todd,

Thanks again, authentication related problem is resolved now.

Now I am facing issue, If any IP from devices list is not reachable, scripts halts. How can I resolve the issue if device is not reachable.

What I want is if device is not reachable then script should skip that device and continue with next IP and execute the script.
Reply With Quote
  #6  
Old 08-04-2015, 11:01 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi netsecrets00,

The example does have a timeout that should handle situations where authentication or connection fails.

Would you post the script you are using?
NOTICE: The requested troubleshooting data may include sensitive information (usernames, passwords, publicly-accessible host names or IP addresses, etc.).

Please redact sensitive information that would not be appropriate for a public forum prior to posting the requested information.

If there is sensitive information that must be conveyed in order to provide a complete picture of the scenario you're facing, please let us know and we will set up a secure upload mechanism that can be used.
If you cancel the script, what line is indicated?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #7  
Old 08-05-2015, 04:42 AM
netsecrets00 netsecrets00 is offline
Registered User
 
Join Date: Jul 2015
Posts: 10
Hi Todd,

Pl find the script as below.
----------------------------------------------------------------------------------
#$language = "VBScript"
#$interface = "1.0"

crt.Screen.Synchronous = True

sub Main
Const DEVICE_FILE_PATH = "N:\xxxx\devices.txt"
Const COMMANDS_FILE_PATH = "N:\xxxx\commands.txt"

dim fso
set fso = CreateObject("Scripting.FileSystemObject")

dim fil
set fil = fso.OpenTextFile(DEVICE_FILE_PATH)

'turn on synchronous mode so we don't miss any data

crt.Screen.Synchronous = True

While Not fil.AtEndOfStream
ip = fil.ReadLine

crt.Screen.Send "telnet " & ip & vbcr

dim result

result = crt.Screen.WaitForStrings("username: ", "Username: ", 10)

If result = 1 Then
crt.Screen.Send "user1" & vbCr
crt.Screen.WaitForString "password: "
crt.Screen.Send "pass1" & vbCr
End If

If result = 2 Then
crt.Screen.Send "user2" & vbcr
crt.Screen.WaitForString "Password: "
crt.Screen.Send "pass2" & vbCr
End If

dim com
set com = fso.OpenTextFile(COMMANDS_FILE_PATH)

While Not com.AtEndOfStream

ioscommand = com.ReadLine
crt.Screen.Send ioscommand & vbcr
crt.Screen.WaitForString "#"
wend

com.Close

crt.Screen.Send "exit" & vbcr
crt.Screen.WaitForString "#"

wend

fil.Close

'turn off synchronous mode to restore normal input processing
crt.Screen.Synchronous = False

end Sub
-------------------------------------------------------------------------------------
Reply With Quote
  #8  
Old 08-05-2015, 04:58 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi netsecrets00,

I would still suggest starting with an example script, and modifying it to meet your needs rather than trying to write your own script.

In the example script, rather than exit the loop in Case "6,7,8", you could send commands.

Another example that might be more along the lines of what you want to do would be:
https://forums.vandyke.com/showthrea...7294#post37294
I am not sure that I can tell you the specific cause for failure when you run your script, but you are assuming that you get connected to a device. You shouldn't send commands unless you know you are connected. When I use your code to interact with a remote server, I get disconnected from the main connection if I don't get connected to a remote server because you send the exit command without determining if you got connected.

You may be able to resolve the issue with your script by moving the commands you want to send into the If statements, and if you get result "0", don't do anything.

Does this help you get your script working?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #9  
Old 08-08-2015, 03:15 AM
netsecrets00 netsecrets00 is offline
Registered User
 
Join Date: Jul 2015
Posts: 10
Hi

below is the issue when script gets failed.
---------------------------------------------------------
XXXXXSSH002#telnet 172.16.54.191
Trying 172.16.54.191 ...
% Connection timed out; remote host not responding

XXXXXSSH002#

------------------------------------------------------------

I can not use Ctrl+C to cancel this. have to wait for 30 Sec to get
"XXXXXSSH002#" prompt.

I want to resume telnet to rest nodes.
Reply With Quote
  #10  
Old 08-08-2015, 12:21 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi,

In the last script you posted, you are specifically using WaitForString() with a timeout. This is how long the script should wait.

Is your script failing or is it just taking longer than you want?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #11  
Old 08-09-2015, 01:54 AM
netsecrets00 netsecrets00 is offline
Registered User
 
Join Date: Jul 2015
Posts: 10
Hi Todd,

My script fails if device is not reachable. I have tried with increasing timeout to 50. But result is same.

---------------------------------------------------------
XXXXXSSH002#telnet 172.16.54.191
Trying 172.16.54.191 ...
% Connection timed out; remote host not responding

XXXXXSSH002#
------------------------------------------------------------

After telnet ip command if device is no reachable then it takes @30-35 seconds to get back to XXXXXSSH002# prompt.

Pl help ... Need to collect data from 3000+ routers.
Reply With Quote
  #12  
Old 08-10-2015, 10:19 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi,

Previously I have suggested starting with a working example script and modifying it to meet your needs. Here is another approach.

This code in your script is very limited:
Code:
result = crt.Screen.WaitForStrings("username: ", "Username: ", 10)

If result = 1 Then
crt.Screen.Send "user1" & vbCr
crt.Screen.WaitForString "password: "
crt.Screen.Send "pass1" & vbCr
End If

If result = 2 Then 
crt.Screen.Send "user2" & vbcr
crt.Screen.WaitForString "Password: "
crt.Screen.Send "pass2" & vbCr
End If
This is part of the reason your script is not doing what you want.

I would suggest replacing the script code above with a modified version of this example code that meets your specific needs:
Code:
    Do
        objNewTab.Screen.WaitForStrings vPossibleShellPrompts, nAuthTimeout
        Select Case objNewTab.Screen.MatchIndex
            Case 0
                DisplayMessage "Authentication timed out!"
                Exit Sub
                
            Case 1,2 ' "ogin:", "name:"
                ' Send the username
                objNewTab.Screen.Send strUser & vbcr
                
            Case 3 ' "sword:"
                ' Send the password
                objNewTab.Screen.Send strPwd & vbcr
            
            Case 4,5 ' "Login incorrect", "authentication failed."
                ' Let user know, and attempt to get correct password
                strPwd = GetPasswordInput(_
                    "Password authentication to '" & strHost & "' as user '" & _
                    strUser & "' failed." & vbcrlf & vbcrlf & _
                    "Please specify the correct password for user " & _
                    "'" & strUser & "':",_
                    "Auth failure - Bad Password?",_
                    strPwd)
                If strPwd = "" Then
                    DisplayMessage "User cancelled auth. Script exiting."
                    Exit Sub
                End If
                
                ' If we have another password to try, automatically loop through
                ' to the top and try again...
            
            Case 6,7,8 ' "$", "]#", or "->" <-- Shell prompt means auth success
                objNewTab.Session.SetStatusText _
                    "Connected to " & strHost & " as " & strUser
                Exit Do
                
            Case Else
                DisplayMessage "Ooops! Looks like you forgot to add code " & _
                    "to handle this index: " & objNewTab.Screen.MatchIndex & _
                    vbcrlf & _
                    vbcrlf & _
                    "Modify your script code's ""Select Case"" block " & _
                    "to have 'Case' statements for all of the strings you " & _
                    "are passing to the ""WaitForStrings"" method."
                
                objNewTab.Session.Disconnect
                ' Only attempt to close the tab if it's not *the* script tab
                If crt.GetScriptTab().Index <> objNewTab.Index Then
                    objNewTab.Close
                End If
                
                Exit Sub
        End Select
    Loop
The other problem is whether you get connected or not, you still run your commands. As mentioned previously, you will need to modify your script so that the commands are only run if you actually get connected.

You could move the commands into Case 6,7,8 or you could set a variable that indicates that you are authenticated and then outside of the loop, you would have an If statement that only executes if the variable is a certain value.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #13  
Old 08-12-2015, 10:59 AM
netsecrets00 netsecrets00 is offline
Registered User
 
Join Date: Jul 2015
Posts: 10
Dear Todd,

After lot of struggle, I am able to use the script which you have suggested earlier (https://forums.vandyke.com/showthrea...7294#post37294).

Now i have some issue with that script.
1. log file is generated with remote host IP address ..(Host file is having IP address) .. i.e : name of log file starts with ##IP Address.
Required : If log file name can be changed to hostname of router.

2. For each command separate log file is generated. i.e: For 1st IP address present in host file, and say 5 commands present in command file, then total 5 log files generated. IP address-Command1. txt/IP address-Command2. txt likewise.
Required: Single log file having output of all 5 commands.

3. I have some devices with TACACS username/password and some with non TACACS username/password. How can I configure said script with additional non TACACS username/password. Presently i have only configured script with TACACS username/password in script under
-------------------------------------------------------------
' Username for authenticating to the remote system
g_strUsername = "user"
' Password for authenticating to the remote system
g_strPassword = "p4$$w0rd"
------------------------------------------------------------

Help me out.
Reply With Quote
  #14  
Old 08-12-2015, 05:03 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi,

Thanks for the update. I am glad to hear that you have the example script working even though you still have some changes that you will need to make.

What version of SecureCRT are you using?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #15  
Old 08-12-2015, 11:17 PM
netsecrets00 netsecrets00 is offline
Registered User
 
Join Date: Jul 2015
Posts: 10
Hi Todd, thanks to you as well for extending support.

I am using SecureCRT V7.0.0.0 (build326).

Pl help to modify the script to meet my requirements.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 PM.