Jump to content

(SOLVED): Network Login Script (UNSOLVED): Debugging


Recommended Posts

Hey guys,

I am very new to using AutoIt and wrote my first little script today after reading bits and pieces on the docs.

The script is designed to make an ugly batch operation a bit more user friendly for the people in the office.

I have a batch file that consists of:

@echo off
net use B: /delete
net use B: \\10.10.10.10\Share /user:domain\username

It is used to log a user onto a secure share drive on my NAS. I have spoofed the share location etc. however.

My AutoIt Script is:

Local $passwd = InputBox("Attempting to Access B:", "Enter your password.", "", "*","M")
Run("C:\Users\User\Desktop\login.bat")
WinWaitActive("C:\Windows\system32\cmd.exe")
Send($passwd)

Now this works fine, however once I input my password into the GUI and it Sends that Password to the cmd window, I cannot make it send an "ENTER" Keystroke.

It isn't a HUGE issue as I can manually press enter once the cmd window shows up, but I would like it to input the password and do the carriage return straight away. I have tried using

Send("{ENTER}")
, and it returns an error. I have tried changing the way I implement the sending of {ENTER} in as many ways as I can think of.

I would greatly appreciate it if anyone can help me on this. Also, if anyone knows of a way to run the cmd window silently but still be able to send the password to it that would be even better!

Cheers

Edited by RedemptionBlue
Link to comment
Share on other sites

Hello!

Welcome to the forum.

You could send the password as a command-line parameter

ShellExecute("C:UsersUserDesktoplogin.bat", $passwd)

and then modify the batch file to utilize the passed parameter:

net use B: 10.10.10.10Share /user:domainusername %1

(I have no access to a network share presently, you may need to wrap the %1 in quotes)

But, I'd suggest it might be cleaner to eliminate the batch file entirely and execute the DOS commands directly from your Autoit script.

Edit: Regarding your second question. Look at the helpfile for InputBox(). Many Autoit commands set a special @error variable after execution. Test the value of @error after the call to InputBox() and react accordingly.

Edited by Spiff59
Link to comment
Share on other sites

Hi Spiff,

Thank you for your swift reply.

I would love to get rid of the batch alltogether, it would be much cleaner indeed.

I have not found too many tutorials as to how to use the AutoIt API and I keep seeing references to the @error value, yet I do not know how to check it's value, utilise it's value or even know anything about it other than the fact that it is returned by many functions.

I also do not quite get what you mean by sending the password as a command line parameter. The Batch file does the share authentication process perfectly, the reason I wanted to use AutoIt is because I work in an office and the Black and white CMD interface is scary for some of the end users. A 'nicer' GUI would be ideal.

Thank you for any help, it is all appreciated, learning is always great!

Link to comment
Share on other sites

Batch files allow for "command-line parameters" simply parameters included on the command-line. So, if from a command prompt you typed "login.bat parameter1 parameter2" then inside the batch file you could reference the parameters typed on the command line by using the built-in variables %1, %2, %3, etc. A line in a batch file that was:

@echo The first command-line parameter is: %1

would display

The first command-line parameter is: parameter1

The "net use" command allows for optional password parameter, instead of prompting the user.

So, we used ShellExecute to launch login.bat and pass it the value of $passwd. Inside the batch file we plugged that parameter (%1) into the proper location of the "net use" command, so it won't need to ask the user for it.

@error behaves much the same as any typical user-created variable that begins with $.

If @error = 2 Then Msgbox(0,"", "The InputBox timed out!")

or for a different look:

If @error = 1 Then
    Tooltip("Script Cancelled by User", @DesktopWidth / 2, @DesktopHeight / 2, "Network Login Script", 0, 2)
    Sleep(2000)
    Exit
Endif

Edit: The helpfile is a pretty good resource. You can find some basic stuff by googling "Autoit tutorial wiki"

Edited by Spiff59
Link to comment
Share on other sites

Thank you Spiff.

I managed to make it work using the command-line parameter method.

I would still like to eliminate the need for the .bat but I have played with executing the net use straight out of AutoIt and can't seem to get it.

I will use my knowledge gained today to work on my logout script now.

Thank you again. Is there a way that I can give you "rep" or something? You have been very helpful.

Cheers

Link to comment
Share on other sites

  • Moderators

Hi, RedemtionBlue. Have you taken a look at DriveMapAdd and DriveMapDel in the help file? It appears these could do the job of eliminating the batch file for you. Something like this, I would think:

DriveMapDel("B:")
DriveMapAdd("B:", '<path to share>', 8, "domain" & @UserName);<--8 automatically shows the authentication box for you
Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Having Trouble with DriveMapAdd.

It keep returning @error = 1

Here is my Code at the moment.

Local $user = InputBox("B:", "Enter your username", "", "", 215,137)
Local $passwd = InputBox("Attempting to Access B:", "Please enter your password", "", "*",215,137)
If @error = 1 Then
Tooltip("Script Cancelled by User", @DesktopWidth / 2, @DesktopHeight / 2, "Network Login Script", 0, 2)
Sleep(2000)
Exit
ElseIf @error = 2 Then
Msgbox(0,"", "The InputBox timed out!")
EndIf
DriveMapDel("B:")
DriveMapAdd("B:", '\\10.0.0.20\Beaches', 8, "domain\" & $user, $passwd)
If @error = 1 Then
MsgBox(0, "Failure", "Failed to Connect to B: - Unknown Error")
Exit
ElseIf @error = 2 Then
MsgBox(0, "Failure", "Failed to Connect to B: - Access is Denied")
Exit
ElseIf @error = 3 Then
MsgBox(0, "Failure", "Failed to Connect to B: - The Drive is Already Assigned")
Exit
ElseIf @error = 4 Then
MsgBox(0, "Failure", "Failed to Connect to B: - Invalid Device Name")
Exit
ElseIf @error = 5 Then
MsgBox(0, "Failure", "Failed to Connect to B: - Invalid Remote Share")
Exit
ElseIf @error = 6 Then
MsgBox(0, "Failure", "Failed to Connect to B: - Incorrect Password")
Exit
EndIf
MsgBox(1, "B:", "You have been logged into B:")

Every single time, I get the MsgBox that I set for @error = 1. Any Ideas why, is my code wrong(quite likely)?

Link to comment
Share on other sites

If @error = 1 what's the value of @extended then?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Use

If @error = 1 Then MsgBox(0, "Failure", "Failed to Connect to B: - Unknown Error, @extended = " & @extended)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Does it make sense to set the flag to 8 (Show authentication dialog if required) when you provide userid and password?

Could you try:

DriveMapAdd("B:", '10.0.0.20Beaches', 0, "domain" & $user, $passwd)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

My script works sometimes but not all the time.

Sometimes it logs me in no worries, other times it returns @error = 1 so I get the unknown error message with a value of @extended = 1219.

Any ideas? I think it is a windows networking issue because when I try to do the DOS command for adding a drive I get an error regarding mapped drives and multiple users, although often I am only logging in from the singular machine.

Windows' networking needs a rethink, I also get errors where it refuses to delete a mapped drive but also won't reconnect either. (Sorry i will stop ranting about windows as this is not the place).

My Code as of now:

Local $user = InputBox("B:", "Enter your username", "", "", 215,137)
If @error = 1 Then
    Tooltip("Script Cancelled by User", @DesktopWidth / 2, @DesktopHeight / 2, "Network Login Script", 0, 2)
    Sleep(2000)
    Exit
ElseIf @error = 2 Then
Msgbox(0,"", "The InputBox timed out!")
EndIf

Local $passwd = InputBox("Attempting to Access B:", $user & ", please enter your password", "", "*",215,137)
If @error = 1 Then
    Tooltip("Script Cancelled by User", @DesktopWidth / 2, @DesktopHeight / 2, "Network Login Script", 0, 2)
    Sleep(2000)
    Exit
ElseIf @error = 2 Then
Msgbox(0,"", "The InputBox timed out!")
EndIf
DriveMapDel("B:")
DriveMapAdd("B:", '10.0.0.20Beaches', 0, "domain" & $user, $passwd)
If @error = 1 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - Unknown Error Code: " & @extended)
   Exit
ElseIf @error = 2 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - Access is Denied")
   Exit
ElseIf @error = 3 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - The Drive is Already Assigned")
   Exit
ElseIf @error = 4 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - Invalid Device Name")
   Exit
ElseIf @error = 5 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - Invalid Remote Share")
   Exit
ElseIf @error = 6 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - Incorrect Password")
   Exit
EndIf
MsgBox(1, "B:", "You have been logged into B:")

The batch code that I use and sometimes get the error(Could be related):

net use B: /delete
net use B: 10.0.0.20Beaches /user:domainusername

FYI, username etc are spoofed.

Cheers

Link to comment
Share on other sites

@extended = 1219 is a WinAPI error code, see the remarks at DriveMapAdd

Multiple connections to a server or shared resource by the same user, using more

than one user name, are not allowed. Disconnect all previous connections to the

server or shared resource and try again..

So you're trying to map a network drive with invalid credentials.

Here's a list for future reference: System Errors

[center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF

Link to comment
Share on other sites

The credentials are 100% correct.

There seems to be problems with windows and authentication of these drives sometimes.

The error regarding bad creds is inconsistent and often the script will just work.

I find after a restart, sometimes it is fixed.

Also, there are only 3 users that have access to the drive and for the sake of well... because I can, I want to make it so if the username of either clare, ric or tony is not entered then the script gives you a MsgBox() and Exits.

Here is my code for it that I tried, but I have looked around on the forums, tutorials and google only to not find anyone else explaining how to do this.

Local $user = InputBox("B:", "Enter your username", "", "", 215,137)
If @error = 1 Then
    Tooltip("Script Cancelled by User", @DesktopWidth / 2, @DesktopHeight / 2, "Network Login Script", 0, 2)
    Sleep(2000)
    Exit
ElseIf @error = 2 Then
Msgbox(0,"", "The InputBox timed out!")
EndIf

If $user <> tony and $user <>clare and $user <> ric Then
Msgbox(0, "Failure", "The username is not recognised")
Exit
EndIf

Thanks for that

Link to comment
Share on other sites

Thank you for all the help so far, my "end-product" is getting closer to completion so to speak.

Just trying to make it so that if the username supplied is not one of the 3 that are allowed to gain access then it says something like "Unknown Username" Or what not.

My entire script is:

Local $user = InputBox("B:", "Enter your username", "", "", 215,137)
If @error = 1 Then
    Tooltip("Script Cancelled by User", @DesktopWidth / 2, @DesktopHeight / 2, "Network Login Script", 0, 2)
    Sleep(2000)
    Exit
ElseIf @error = 2 Then
Msgbox(0,"", "The InputBox timed out!")
EndIf

#cs
If $user <> tony and $user <>clare and $user <> ric Then
Msgbox(0, "Failure", "The username is not recognised")
Exit
EndIf
#ce

Local $passwd = InputBox("Attempting to Access B:", $user & ", please enter your password", "", "*",215,137)
If @error = 1 Then
    Tooltip("Script Cancelled by User", @DesktopWidth / 2, @DesktopHeight / 2, "Network Login Script", 0, 2)
    Sleep(2000)
    Exit
ElseIf @error = 2 Then
Msgbox(0,"", "The InputBox timed out!")
EndIf
DriveMapDel("B:")
DriveMapAdd("B:", '\\10.0.0.20\Beaches', 0, "domain\" & $user, $passwd)
If @error = 1 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - Unknown Error Code: " & @extended & @CRLF & "Please contact your IT Support")
   Exit
ElseIf @error = 2 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - Access is Denied")
   Exit
ElseIf @error = 3 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - The Drive is Already Assigned")
   Exit
ElseIf @error = 4 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - Invalid Device Name")
   Exit
ElseIf @error = 5 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - Invalid Remote Share")
   Exit
ElseIf @error = 6 Then
   MsgBox(0, "Failure", "Failed to Connect to B: - Incorrect Password" & @CRLF & "If you have forgotten your password please contact IT Support")
   Exit
EndIf
MsgBox(1, "B:", "You have been logged into B:")

As you can see I have placed my attempt at the username authentication in #cs/ce tags.

If I cannot do it in the simpler format that I have attempted I will just make it so that it is an if statement where if $user = tony Then execute script, else if $user = clare Then execute script, else if $user = ric execute script, else Exit.

I would rather not do that as I am a Java programmer and it seems to me like just sloppy code but I just do not quite have the syntax/semantics of if statements in autoit fully understood.

Cheers

Link to comment
Share on other sites

If you have only 3 usernames then your code is fine (for me).

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

You need quotes around the user names:

If $user <> "tony" and $user <> "clare" and $user <> "ric" Then
    Msgbox(0, "Failure", "The username is not recognised")
    Exit
EndIf

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...