#1
|
|||
|
|||
![]()
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. |
#2
|
||||
|
||||
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. |
#3
|
||||
|
||||
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) 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.
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] Last edited by miked; 02-16-2011 at 01:11 PM. |
#4
|
|||
|
|||
![]()
Hi Michael Decker,
I thank you for all the information you provided. It is very helpful. However to be honest im bit more confused. ![]() 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. |
#5
|
|||
|
|||
Quote:
i hope anybody can help on my above requirements. |
#6
|
||||
|
||||
Hi amatre_scripter,
Quote:
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:
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] |
#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 ![]() 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 |
#8
|
||||
|
||||
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] |
#9
|
|||
|
|||
![]()
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? |
#10
|
||||
|
||||
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.
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#11
|
|||
|
|||
![]()
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. |
#12
|
||||
|
||||
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...
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: myusernameSay 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 Code:
Set fso = CreateObject("Scripting.FileSystemObject") Set s = fso.OpenTextFile("C:\Temp\MyDataFile.txt") strMyUsername = s.ReadLine strMyPassword = s.ReadLine s.Close strMyUsername, and strMyPasswordNow 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 Quote:
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] Last edited by jdev; 02-22-2011 at 01:00 PM. |
#13
|
|||
|
|||
![]()
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. |
#14
|
||||
|
||||
Hi Gordon, I'm glad that the example helps!
![]()
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|