Jump to content

Suggestions to improve my script?


Recommended Posts

/edit describe script

I have finished what I consider to be my first working script.

This script was written to connect to PBX systems using RAS DUN connections called C1, C2, C3, and C4. These network connections are modified to used a script specified in the Security tab. The script is then selected in the Interactive Logon and scripting section. I use Rasdial commands in my script to connect to the PBX system. This script modifies the selected RAS connection to match what is required to call the PBX.

I don't get a great deal of time to work with this scripting language so it's not perfect. Could those of you who do know what you are doing provided me some feedback. What improvements can I make to the script to make it more efficient.

CODE
#Include <GUIConstants.au3>

#include <Process.au3>

Opt('GUIOnEventMode','1')

GUISetState(@SW_SHOW)

; vars

$my_ver = "0.90.4"

If WinExists("Connect Tool") Then

MsgBox(4096,"Info", "Only one copy of this application may be run at once!" & @CRLF & @CRLF & " Program will now exit.", 6)

Exit

Else

EndIf

$my_port = ""

$UseSSH = "No"

$UseTelnet = "No"

$RET = ""

Global $Width = ('400')

Global $Height = ('200')

GUICreate("Connect Tool", $Width, $Height)

GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit')

$Context_Menu = GUICtrlCreateContextMenu ()

$Item_1 = GUICtrlCreateMenuItem ("About", $Context_Menu)

GUICtrlSetOnEvent ($Item_1, "_About")

GuiCtrlCreateMenuitem ("", $Context_Menu)

$Item_2 = GUICtrlCreateMenuItem ("Exit", $Context_Menu)

GUICtrlSetOnEvent ($Item_2, "_Exit")

GUICtrlCreateLabel ("* PORT :",55,115)

$Input_1 = GUICtrlCreateInput ("",94,112,25,17)

GUICtrlSetLimit ($Input_1, '2','2')

GUICtrlSetState ($Input_1, $GUI_DISABLE)

GUICtrlCreateGroup ("",40,5,100,95)

$Radio_1 = GUICtrlCreateRadio("C1", 75, 15, 45)

GUICtrlSetOnEvent ($Radio_1, "_C1")

$Radio_2 = GUICtrlCreateRadio("C2", 75, 35, 45)

GUICtrlSetOnEvent ($Radio_2, "_C2")

$Radio_3 = GUICtrlCreateRadio("C3", 75, 55, 45)

GUICtrlSetOnEvent ($Radio_3, "_C3")

$Radio_4 = GUICtrlCreateRadio("C4", 75, 75, 45)

GUICtrlSetOnEvent ($Radio_4, "_C4")

GUICtrlCreateGroup ("",30,135,125,55)

GUICtrlCreateLabel ("Connect to PBX using:",37,146)

$Radio_8 = GUICtrlCreateRadio("Telnet", 40, 160, 50)

GUICtrlSetOnEvent ($Radio_8, "_UseTelnet")

$Radio_9 = GUICtrlCreateRadio("SSH", 100, 160, 45)

GUICtrlSetOnEvent ($Radio_9, "_UseSSH")

GUICtrlCreateLabel ("* Telephone Number",238,4)

$Input_3 = GUICtrlCreateInput ("",210,20,150,20,$ES_NUMBER)

GUICtrlSetLimit ($Input_3, '23','23')

GUICtrlCreateLabel ("* RAS Login",255,45)

$Input_4 = GUICtrlCreateInput ("",210,60,150,20)

GUICtrlSetLimit ($Input_4, '23','23')

GUICtrlCreateLabel ("* RAS Password",245,85)

$Input_5 = GUICtrlCreateInput ("",210,100,150,20)

GUICtrlSetLimit ($Input_5, '23','23')

GUICtrlCreateLabel ("RAS IP Address",248,130)

$Input_2 = GUICtrlCreateInput ("",210,145,150,20)

GUICtrlSetLimit ($Input_2, '15','15')

$Button_1 = GUICtrlCreateButton ("Dial PBX",300,170,70,25)

GUICtrlSetOnEvent ($Button_1, "_CheckSelections")

$Button_2 = GUICtrlCreateButton ("Disconnect RAS",205,170,90,25)

GUICtrlSetOnEvent ($Button_2, "_KillRAS")

While 1

GUISetState(@SW_SHOW)

$msg = GUIGetMsg()

If $msg = $Item_2 or $msg = -3 or $msg = -1 Then ExitLoop

Select

Case $msg = $GUI_EVENT_CLOSE

Exit

;ExitLoop

Sleep (200)

EndSelect

WEnd

Func _CheckSelections()

$my_port = GUICtrlRead($Input_1) ; set vars based on input

$login = GUICtrlRead($Input_4)

$passwd = GUICtrlRead($Input_5)

If GUICtrlRead($Input_1) = "" or GUICtrlRead($Input_3) = "" or GUICtrlRead($Input_4) = "" or GUICtrlRead($Input_5) = "" Then

SplashTextOn("Notice - Error", "!!! Missing Input !!!" & @CRLF & "Entries marked with * are required.", 380, 80)

Sleep(5000)

SplashOff()

Return

EndIf

If GUICtrlRead($Input_2) <> "" And $UseTelnet = "No" And $UseSSH = "No" Then

SplashTextOn("Notice - Error", " You have entered an RAS IP address." & @CRLF & "No connection method was selected.", 380, 55)

Sleep(3000)

SplashOff()

Return

EndIf

If $UseTelnet = "Yes" and GUICtrlRead($Input_2) = "" Then

SplashTextOn("Notice - Error", " You selected telnet." & @CRLF & "No RAS IP address was entered.", 380,55)

Sleep(3000)

SplashOff()

Return

EndIf

If $UseSSH = "Yes" and GUICtrlRead($Input_2) = "" Then

SplashTextOn("Notice - Error", " You selected SSH." & @CRLF & "No RAS IP address was entered.", 380,55)

Sleep(3000)

SplashOff()

Return

EndIf

SplashTextOn("Info", @CRLF & "Please wait ... Attempting connection to PBX", 380, 55)

Sleep(3000)

SplashOff()

_Main()

EndFunc ;==>_CheckSelections

Func _Main()

_CreateSCP()

_DiscRAS()

_DialRAS()

_Connect()

EndFunc ;=>_Main

Func _C1 ()

GUICtrlSetData ($Input_1, 'C1')

EndFunc

Func _C2 ()

GUICtrlSetData ($Input_1, 'C2')

EndFunc

Func _C3 ()

GUICtrlSetData ($Input_1, 'C3')

EndFunc

Func _C4 ()

GUICtrlSetData ($Input_1, 'C4')

EndFunc

Func _CreateSCP()

$script = "c:\windows\system32\ras\test.scp"

$rasscriptpath = "c:\windows\system32\ras\"

$file = FileOpen($script, 10) ; Check if file is opened for writing successfully

If $file = -1 Then

MsgBox(0, "Error", "Unable to create the script file." & $file)

Exit

EndIf

FileWrite($file, " ; PBX Connect Tool " & $my_Ver & @CRLF)

FileWrite($file, " ; Automated Logon Tool" & @CRLF)

FileWrite($file, "proc main" & @CRLF)

FileWrite($file, ' transmit "^M" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' waitfor "login: " '& @CRLF)

FileWrite($file, ' transmit "' & GUICtrlRead($Input_4) &'^M" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' waitfor "Password: " ' & @CRLF)

FileWrite($file, ' transmit "' & GUICtrlRead($Input_5) &'^M" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' waitfor "Start PPP now!" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' transmit "/go:pppconnect^M" '& @CRLF)

FileWrite($file, "endproc" & @CRLF)

FileClose($file)

EndFunc ;==>_CreatSCP

Func _DiscRAS()

_RunDOS("rasdial " & $my_port & " /disconnect")

Sleep(3000)

EndFunc ;==>_DiscRAS

Func _DialRAS()

$RET = _RunDOS("rasdial " & GUICtrlRead($Input_1) & " /phone:" & GUICtrlRead($Input_3))

Switch $RET

Case 720

MsgBox(16, "RAS Error", "Login script error", 3)

Case 680

MsgBox(16, "RAS Error", "No dialtone", 3)

Case 678

MsgBox(16, "RAS Error", "There was no answer", 3)

Case 677

MsgBox(16, "RAS Error", "A person answered instead of a modem", 3)

Case 676

MsgBox(16, "RAS Error", "Line is busy", 3)

Case 635

MsgBox(16, "RAS Error", "Unknown Error", 3)

Case 623

MsgBox(16, "RAS Error", "A RAS DUN Connection named " & GUICtrlRead($Input_1) & " does not exist"& @CRLF & "Please create the connect befoe trying again.",4 )

Case 619

MsgBox(16, "RAS Error", "The login or password is incorect", 3)

Case 618

MsgBox(16, "RAS Error", "The port is not open", 3)

Case 617

MsgBox(16, "RAS Error", "The port or device is allready disconnecting", 3)

Case 606

MsgBox(16, "RAS Error", "The port is not connected", 3)

Case 86

MsgBox(16, "RAS Error", "The password is incorrect", 3)

Case 0

SplashTextOn("RAS Info", "The RAS connection was successful", 380, 55)

Sleep(3000)

SplashOff()

Case Else

MsgBox(32, "RAS Info", "An unknown error has occured", 3)

EndSwitch

If $RET <> 0 Then

MsgBox(0, "Info", "RAS connection attempt was unsuccessful. Please try again.", 6)

Return

EndIf

EndFunc ;==>_DialRAS

Func _Connect()

If $UseTelnet = "Yes" Then

;_RunDOS("telnet " & GUICtrlRead($Input_2)) ; this doesn't run!!

Run("telnet " & GUICtrlRead($Input_2))

If @error Then

MsgBox(0, "Error", "Your telnet command did not work!!", 5)

EndIf

EndIf

If $UseSSH = "Yes" Then

_RunDOS("putty " & GUICtrlRead($Input_2))

If @error Then

MsgBox(0, "Error", "Your ssh command did not work!!", 5)

EndIf

EndIf

;Return

EndFunc ;=>_Connect

Func _KillRAS()

If GUICtrlRead($Input_1) = "" Then

SplashTextOn("Notice - Error", @CRLF & "No RAS port was selected! Please try again.", 380, 55)

Sleep(3000)

SplashOff()

Return

EndIf

SplashTextOn("Info", "The RAS connection on " & GUICtrlRead($Input_1) & " will be disconnected.", 380, 55)

Sleep(3000)

SplashOff()

_RunDOS("rasdial " & $my_port & " /disconnect")

EndFunc ;==>_KillRAS

Func _UseSSH()

$UseSSH = "Yes"

$UseTelnet = "No"

EndFunc ;==>_UseSSH

Func _UseTelnet()

$UseTelnet = "Yes"

$UseSSH = "No"

EndFunc ;==>_TelnetRAS

Func _About()

MsgBox(0, "About", "Network Connect Version " & $my_ver)

EndFunc ;=>_About

Func GoodBye()

MsgBox(4096,"", "Goodbye!")

Exit

EndFunc ;==>GoodBye

Func _Exit ()

Exit

EndFunc ;==> Exit

You feedback would be appreciated. :D

CC

Edited by IvanCodin
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...