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 02-16-2011, 09:42 AM
amatre_scripter amatre_scripter is offline
Registered User
 
Join Date: Feb 2011
Posts: 7
Question read from a file and write to current session

Hi,
Im brand new to this forum and i do appreciate that this forum exists. It would definitely help us share our ideas on scripts and make our lives easier. I must admit that i do not know scripting from scratch but what i learnt on the go.

My requirement is, i want to run a script which would take the username and password from a txt file and use it in the current session to log into a box. I have tried the following 2 scripts but it just would not go ahead as in it would not fill in the username and password, just waits at username.

First:
---------------------
Sub Main

crt.Screen.Synchronous = True

' connect to host on port 23 (the default telnet port)
'
crt.Session.Connect "/TELNET 152.110.210.30 1029"


Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objStream = objFSO.OpenTextFile("H:\Scripts\credentials.txt")

strFileData = objStream.ReadAll

objStream.Close
vLines = Split(strFileData, vbcrlf)
For each strLine in vLines

Next


crt.Screen.Send username
crt.Screen.WaitForString "assword:"
crt.Screen.Send password
crt.Screen.WaitForString "EMC Proxy>"
crt.Screen.Send "cn EU-CONERGY" & chr(13)

Dim ss
ss = crt.Dialog.Prompt("Enter host name")

cmd = "cd -n " & ss

crt.Screen.Send cmd & vbCr

End Sub
-------------

Second:
Sub Main

crt.Screen.Synchronous = True

' connect to host on port 23 (the default telnet port)
'
crt.Session.Connect "/TELNET 152.110.210.30 1029"

Dim g_strHostsFile, g_strDelimiter, g_strExampleHostsFile
g_strHostsFile = "H:\Scripts\credentials.txt"
g_strDelimiter = ","
strFieldDesignations = "username,password"

crt.Screen.Send username
crt.Screen.WaitForString "assword:"
crt.Screen.Send password
crt.Screen.WaitForString "EMC Proxy>"
crt.Screen.Send "cn EU-CONERGY" & chr(13)

Dim ss
ss = crt.Dialog.Prompt("Enter host name")

cmd = "cd -n " & ss

crt.Screen.Send cmd & vbCr

End Sub
-----------------
Information in the text file is:
username,password
user.name@abc.com,allowmeIn

Please could anyone provide some assistance.

Regards,
Gordon.
Reply With Quote
  #2  
Old 02-16-2011, 11:52 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
Hello amatre_scripter,

It looks like you have a good start with the scripts. It's a good idea to make sure that the remote server is ready to receive data before sending data. I noticed that your script sends a username, and I wonder if the server isn't quite ready to receive the username. If you don't see the username appear then it's likely that the username was sent earlier. A best practice is to use WaitForString to make sure the remote is at a known point and is ready to recieve data.

Also, you'll probably need to add VBCR or VBCRLF to the string you're sending.

Can you try the following code and see if you get better results?

Code:
' The following WaitForString assumes the server prompts for a username with "Username:"
crt.Screen.WaitForString "sername:"
crt.Screen.Send username & vbcr
crt.Screen.WaitForString "assword:"
crt.Screen.Send password & vbcr
crt.Screen.WaitForString "EMC Proxy>"
crt.Screen.Send "cn EU-CONERGY" & chr(13)
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]

Last edited by miked; 02-16-2011 at 11:54 AM.
Reply With Quote
  #3  
Old 02-16-2011, 01:08 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
amatre_scripter,

After reviewing your example scripts further it appears there are more problems than I initially saw. The username and password are never assigned a value, so even if you add WaitForString and VBCR the variables will be empty. Before sending the username and password, your script would currently need to assign values to the variables.
username = vlines(0)
password = vlines(1)

There is also an empty for loop which looks like the intent may have been to assign username and password to the information that was found in vLines. However, the script has hard coded the single server to connect to, so unless you wanted to log onto the same box using a variety of usernames, the for loop appears out of place.
If your external file (credentials.txt) contained the hostname as well, and if crt.Session.Connect referred to the hostname, then the loop might be able to serve some purpose.

Have you considered using Automate logon in Session Options / Connection / Logon Actions?

One advantage of using Automate logon is that you can use Expect and Send strings to automate doing what it looks like your script is trying to do.

Another advantage of using Automate logon is that your username and password would be encoded in the .ini file, instead of being exposed in a text file. From a security perspective, keeping usernames and passwords in a text file is very dangerous. We strongly encourage using an alternate method, such as Automate logon. You might also consider keeping the list of usernames and passwords in the same script file instead of a separate file, so that you don't have two files to secure.

If you are not concerned about the security risk in keeping a text file containing usernames and passwords, you may want to review the attached an example script, GetLoginInfoFromFile.txt, which retrieves a hostname, username, and password from a text file.
Attached Files
File Type: txt GetLoginInfoFromFile.txt (5.0 KB, 491 views)
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]

Last edited by miked; 02-16-2011 at 01:11 PM.
Reply With Quote
  #4  
Old 02-17-2011, 02:55 AM
amatre_scripter amatre_scripter is offline
Registered User
 
Join Date: Feb 2011
Posts: 7
Lightbulb Thanks, but still confused.

Hi Michael Decker,

I thank you for all the information you provided. It is very helpful. However to be honest im bit more confused. Let me explain, in the first place im not at all good at scripts and i do not understand wht the characters mean, for e.g. g_sz etc.

However the point is that i just need a simple script to help us (my team i mean) log into our customer networks.

Let me tell you my requirement:
We have a linux box which has got a list of customers configured on it. To log into this box only we need to give our username and password. This password changes every 30-45 days. This username/password can be kept in our private drives which is H: and nobody else has access to it.
Once logged into the linux box the user needs to connect to the individual customer using the command 'cn (customer name)' and once into the customer prompt they would need to connect to the device by providing 'cd -n (device name)'.
Thats it!

Your suggestion 1- Embedding the username/ password-
Now i have made scripts for each and every customer, around 14, (more to be added in future). So that is why the user will have to go to each of the scripts and edit the username and password.

Your suggestion 2- Using the session to automoate the login-
This secure crt is also used for other devices which have other username and password so it cant be used.

Once again thanks for all your advice. I do appreciate it.
Reply With Quote
  #5  
Old 02-17-2011, 12:56 PM
amatre_scripter amatre_scripter is offline
Registered User
 
Join Date: Feb 2011
Posts: 7
Quote:
Originally Posted by amatre_scripter
Hi Michael Decker,

I thank you for all the information you provided. It is very helpful. However to be honest im bit more confused. Let me explain, in the first place im not at all good at scripts and i do not understand wht the characters mean, for e.g. g_sz etc.

However the point is that i just need a simple script to help us (my team i mean) log into our customer networks.

Let me tell you my requirement:
We have a linux box which has got a list of customers configured on it. To log into this box only we need to give our username and password. This password changes every 30-45 days. This username/password can be kept in our private drives which is H: and nobody else has access to it.
Once logged into the linux box the user needs to connect to the individual customer using the command 'cn (customer name)' and once into the customer prompt they would need to connect to the device by providing 'cd -n (device name)'.
Thats it!

Your suggestion 1- Embedding the username/ password-
Now i have made scripts for each and every customer, around 14, (more to be added in future). So that is why the user will have to go to each of the scripts and edit the username and password.

Your suggestion 2- Using the session to automoate the login-
This secure crt is also used for other devices which have other username and password so it cant be used.

Once again thanks for all your advice. I do appreciate it.

i hope anybody can help on my above requirements.
Reply With Quote
  #6  
Old 02-17-2011, 01:09 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
Hi amatre_scripter,

Quote:
in the first place im not at all good at scripts and i do not understand wht the characters mean, for e.g. g_sz etc.
The naming conventions are simply conventions to help you know what the variable's purpose and scope is. When you see g_ means the variable is global. A global variable has scope (retains its value and meaning) across all functions and subroutines instead of only within a single function or subroutine. When you see sz or str means the variable holds a string. The n at the front of a variable means it's a number.

The example script I provided will parse hostnames, usernames, and passwords from a text file. This sounded like what you needed to do. Can you help me understand what part of the script is confusing, besides the names?

Quote:
Once logged into the linux box the user needs to connect to the individual customer using the command 'cn (customer name)' and once into the customer prompt they would need to connect to the device by providing 'cd -n (device name)'.
Using Expect/Send still seems like a good idea to me, especially if you're not very familiar with scripting and the only thing you need to do is send a few strings. If you create a separate session for each device name, everything could happen automatically. The user could be logged into the linux box, "cn customername", and "cd -n devicename" could all be sent. Would creating a separate session for each customer and automating the login using Expect/Send work?

We have a SecureCRT VBScript Scripting Manual that may help you if you'd like to learn more about scripting, and reading from files for example. However, this method does seem like more work (fun work, but still work - writing scripts requires your time and effort) and possibly overkill to perform a function that's already available with Expect and Send.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #7  
Old 02-18-2011, 02:51 AM
amatre_scripter amatre_scripter is offline
Registered User
 
Join Date: Feb 2011
Posts: 7
Tq

Hi Mike,

You have been most helpful once again. I did go through the manual and it has lot of information for running scripts. Also i am aware how to send the commands to the session. But i am not aware what script can i use to get username and password from a file (also what should be mentioned in the file ) and send it to the current session. Just as you have provided a script in your first reply, would it be possible to provide me with the script for this.
I tried using the script that you had attached, but it comes up with an error that the host is not available, etc.

Thanks.

Note: the content of my text file is as follows:

username,password
user.name@xyz.com,password
Reply With Quote
  #8  
Old 02-18-2011, 08:19 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
The example script that I provided in my first example is the closest example I have of doing what you need to do. It retrieves hostname, userame, password from a file. Once retrieve, you only need to use crt.Screen.Send username or crt.Screen.Send password after you discover it's time to send the username or password (perhaps by using crt.Screen.WaitForString "ogin:" or crt.Screen.WaitforString "assword:").

The example script searches the script using the hostname as the key to finding the correct username and password. You could modify the example script to search in a different way, if you want, but I'm not sure how you'll know which username and password to use.

How are you going to know which username and password to use for a host?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #9  
Old 02-20-2011, 06:25 AM
amatre_scripter amatre_scripter is offline
Registered User
 
Join Date: Feb 2011
Posts: 7
Red face Modified script still no results.

Hi Mike,

I have done some modifications to the script that you gave which is as below. But still the session waits at 'Enter user name:' I don't know why. The file credentials.txt is in the 'My documents folder'

# $language = "VBScript"
# $interface = "1.0"
' GetLoginInfoFromFile.vbs
'
' Description: Shows example of how to retrieve information
' from a flat file as a means of a simple database.

Set g_fso = CreateObject("Scripting.FileSystemObject")
Set g_shell = CreateObject("WScript.Shell")

g_szDBFile = g_shell.SpecialFolders("MyDocuments") & "\credentials.txt"

' # Info in text file is 1st line: username;;;password
' # 2nd line: user.name@xyz.com;;;password

g_szComment = "#"

g_szDelim = ";;;"

crt.Session.Connect "/TELNET 152.110.210.30 1029"

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Main()
Dim szUsername, szPassword


crt.Screen.WaitForString "name"

crt.Screen.Send username

crt.Screen.WaitForString "word"

crt.Screen.Send password

End Sub
also the path which is provided mentions special folders 'My documents', what if i need to give another path (other than 'My documents') what would the syntax be?
Reply With Quote
  #10  
Old 02-21-2011, 11:01 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
Hi amatre_scripter,

Can you send a raw log to support@vandyke.com with subject forum 7145 so that we can see the data that's being received, and try to determine the appropriate string to wait for?

To create a raw log:
1. Before connecting with the session, select "Raw Log Session" from SecureCRT's "File" menu.

2. In the "Select Log File" dialog, choose a folder and filename in which you would like the log text to be stored and press the "Save" button. At this point, if you open the "File" menu, you will notice that the "Raw Log Session" menu item now has a check-mark next to it; this indicates that raw logging is activated.

3. Now connect to the remote machine and perform the actions which cause the reported problem to occur.

4. Once the problem occurs, disconnect the session and select "Raw Log Session" from the "File" menu to turn off the raw logging feature.

5. Browse to the location of the raw log file and add the file as an attachment to your e-mail reply.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #11  
Old 02-22-2011, 07:26 AM
amatre_scripter amatre_scripter is offline
Registered User
 
Join Date: Feb 2011
Posts: 7
Exclamation some change

Hi,

I did some change to the script, i.e. i added & vbCr to the end of the line as follows. Now i see that it goes to the next line but the problem is picking up the username and password from the file.

crt.Screen.Send username & vbCr
crt.Screen.Send password & vbCr

I have already sent the log file to the email address you mentioned.
Reply With Quote
  #12  
Old 02-22-2011, 12:25 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
Hi amatre_scripter,

The examples I've provided aren't a 100% match to what you need. We don't provide custom scripts; yet we've provided a good number of examples that show a variety of tasks that one can learn from. Sometimes when you're first introduced to something it takes a while to get started and feel comfortable.

So, let's take this one step at a time...
  • You already know how to "send commands to the session".
  • You need to parse the username and password from the file.
  • In an earlier message you asked what should be mentioned in the file.
The second and third tasks are related. The method you've suggested in earlier posts for storing usernames and passwords in a file (usiing a comma as the delimiter) would make parsing usernames and passwords a little more complicated and could even become problematic (if a comma is part of a password then you have a problem), so I'm going to suggest an alternative. How you store the data is really up to you. If all you're doing is storing arbitrary data in the file, you can choose the pattern that fits your preference. Here are two ideas:

A) Store the username on the first line, and the password on the following line -- you only care about the first two lines of the file. If all you care about is a single username and password (not multiple user+pass pairs for multiple machines), this might be the best solution -- simple.

B) Store the username and the password on the same line -- the first line of the file. You would choose a "separator" that would help you know where the username ends and the password begins. The separator you choose would need to be unique enough that neither the username nor the password would contain it. In the "GetLoginInfoFromFile.txt" example we provided earlier, the "separator" used between each of the fields on a line within the file was three consecutive semi-colons (";;;"), which is less likely to be present in a username or a password (not impossible, just less likely). This may be a pretty decent solution in a final draft of the script, but is probably not a good starting point.

Let's assume you're going to use option A, for simplicity. Here's what your data file would look like:

myusername
mypassword
Say you've saved the above data to a file:
C:\Temp\MyDataFile.txt.
Section 7.2 of the aforementioned scripting manual gives the recipe (including code) for opening a file and reading the data. There's lots of ways to go about it, but here's what would work assuming you've got a file with just the username as the first line in the file and the password as the second line of the file:

Code:
' Get a reference to the FileSystemObject which allows
' you to read data from a file:
Set fso = CreateObject("Scripting.FileSystemObject")
Set s = fso.OpenTextFile("C:\Temp\MyDataFile.txt")

' Read the first line of the file and store the
' data in a variable named "strMyUsername":
strMyUsername = s.ReadLine

' Now, read the next line of the file and store
' that line's data in a variable named
' "strMyPassword" -- remember you get to choose
' the name of the variable; instead of
' strMyPassword, you could name it
' "ThisIsMyPassword" or whatever:
strMyPassword = s.ReadLine

' Since we only care about the first two lines
' of the file, and we've already read in those
' lines and stored the data from each line in
' its own variable we can use later, we'll close
' the file stream since we don't need it any more:
s.Close
If we omit all of the comments from the above code segments, it's really just 5 lines of code to read in the first two lines of text from a file and store the data into variables named such that we can remember what's in them for later use:
Code:
Set fso = CreateObject("Scripting.FileSystemObject")
Set s = fso.OpenTextFile("C:\Temp\MyDataFile.txt")
strMyUsername = s.ReadLine
strMyPassword = s.ReadLine
s.Close
The above 5 lines of code show how you can read the first two lines from a file into two variables:
strMyUsername, and strMyPassword
Now that you have the data in the variables, you can use them however you want. Here's how you could display the data in a popup window:
Code:
MsgBox "Username: " & strMyUsername
MsgBox "Password: " & strMyPassword
You specifically mentioned that you didn't quite know how to send the data to the current session. Here's how you'd do that:
Quote:
' Send the username to the session
crt.Screen.Send strMyUsername & vbcr

' Send the password to the session
crt.Screen.Send strMyPassword & vbcr
Naturally, the above two Send() statements would each be preceded by a call to crt.Screen.WaitForString() telling SecureCRT to wait for the username prompt to appear, then send the username, then wait for the password prompt to appear, then send the password, etc..
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]

Last edited by jdev; 02-22-2011 at 01:00 PM.
Reply With Quote
  #13  
Old 02-24-2011, 06:50 AM
amatre_scripter amatre_scripter is offline
Registered User
 
Join Date: Feb 2011
Posts: 7
Thumbs up Thanks

Hi Mike,

Ive tried the script. It works perfectly fine, just as expected. Lovely!. As usual you have been most helpful. Appreciate your support and patience on this :-)

Thanks mate, Until next time.
Good bye.
Reply With Quote
  #14  
Old 02-24-2011, 10:00 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
Hi Gordon, I'm glad that the example helps!
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
Reply

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 09:09 PM.