Sign in to follow this  
Followers 0
IvanCodin

On and Off selection is detected

2 posts in this topic

#1 ·  Posted (edited)

I would like the user to select a choice of connection type. SSH or telnet. Only the selection they choose will be used to connect. I can't figure out how to do this in my script. The selections C1, C2, C3, and C4 are RAS DUN connections that have been created to connect to the PBX system for the user. I know there is a much more effective way to do this that I am using any suggestions?

CODE
#Include <GUIConstants.au3>

#include <Process.au3>

Opt('GUIOnEventMode','1')

GUISetState(@SW_SHOW)

; ================================>

; vars

$my_ver = "0.90.2"

$my_port = ""

$UseSSH = "No"

$UseTelnet = "No"

$RET = ""

Global $Width = ('400')

Global $Height = ('200')

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

GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit')

$Context_Menu = GUICtrlCreateContextMenu ()

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

; GUICtrlSetOnEvent ($Item_1, "$my_ver")

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,5)

$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')

$Button_1 = GUICtrlCreateButton ("Dial RAS",300,150,70,25)

GUICtrlSetOnEvent ($Button_1, "_CheckSelections")

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

GUICtrlSetOnEvent ($Button_2, "_KillRAS")

While 1

GUISetState(@SW_SHOW)

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Sleep (250)

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

MsgBox(0, "Notice - Error", "Please make required entries and Retry." & @CRLF & "Enties marked with * are required.", 0)

Return

EndIf

_CreateSCP()

_DiscRAS()

_DialRAS()

_Check()

_Connect()

EndFunc ;==>_CheckPort

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, " ; 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()

MsgBox(0, "Info", "RAS connection " & GUICtrlRead($Input_1) & " will be disconnected.", 6)

run("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 621

MsgBox(16, "RAS Error", "Cannot open the phone book file", 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 0

MsgBox(32, "RAS Info", "The RAS connection was successful", 3)

Case Else

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

EndSwitch

EndFunc ;==>_DialRAS

Func _Check()

$UseTelnet = "Yes"

MsgBox(0,"Info", "Telnet is set to " & $UseTelnet, 2)

EndFunc ;==>_Check

Func _Connect()

MsgBox(0, "Info", "RAS dial result was " & $RET, 3)

If $RET <> 0 Then

MsgBox(0, "Info", "RAS connection was unsuccessful.", 6)

Return

EndIf

EndFunc ;=>_Connect

Func _KillRAS()

If GUICtrlRead($Input_1) = "" Then

MsgBox(0, "Notice - Error", "No RAS port was selected! Please try again.", 0)

Return

EndIf

MsgBox(0, "Info", "The RAS connection on " &GUICtrlRead($Input_1) & ", if present, will be disconnected.", 6)

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

; _DialRAS()

EndFunc ;==>_KillRAS

Func _UseSSH()

$UseSSH = "Yes"

MsgBox(0,"Info", "SSH is set to " & $UseSSH, 2)

; Run("putty 10.1.1.241")

EndFunc ;==>_UseSSH

Func _UseTelnet()

$UseTelnet = "Yes"

MsgBox(0,"Info", "Telnet is set to " & $UseTelnet, 2)

; Sleep(5000)

; GUISetState(@SW_HIDE)

; Run("c:\windows\system32\telnet.exe")

; WinWaitActive("c:\windows\system32\telnet.exe")

; Sleep(3000)

; Replace XXXXX with the name of your server

; Send("op 192.168.1.1{ENTER}")

; WinWaitActive("Telnet 192.168.1.1")

EndFunc ;==>_TelnetRAS

Func GoodBye()

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

Exit

EndFunc ;==>GoodBye

Func _Exit ()

Exit

EndFunc ;==> Exit

From my script you can see I am still learning Autoit. If you have suggestions for better methods to do things they are welcome.

Thanks

CC

Edited by IvanCodin

Share this post


Link to post
Share on other sites



I got it kinda working but there must be a better way. Here is the new script with the changes I made. I am sure there is a more efficient way to create the telnet or ssh login buttons. Any suggestions?

CODE
#Include <GUIConstants.au3>

#include <Process.au3>

Opt('GUIOnEventMode','1')

GUISetState(@SW_SHOW)

; ================================>

; Ver 0.90.1 Initial version

; Ver 0.90.2 disconnect only selected ras connection. Original code killed all ras connected sessions.

;

; To Do

; After successful connection determine the Server RAS ip address and telnet to it automatically

; After successful connection minimize it to the system tray.

;

; vars

$my_ver = "0.90.2"

$my_port = ""

$UseSSH = "No"

$UseTelnet = "No"

$RET = ""

Global $Width = ('400')

Global $Height = ('200')

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

GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit')

$Context_Menu = GUICtrlCreateContextMenu ()

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

; GUICtrlSetOnEvent ($Item_1, "$my_ver")

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,5)

$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')

$Button_1 = GUICtrlCreateButton ("Dial RAS",300,150,70,25)

GUICtrlSetOnEvent ($Button_1, "_CheckSelections")

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

GUICtrlSetOnEvent ($Button_2, "_KillRAS")

While 1

GUISetState(@SW_SHOW)

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Sleep (250)

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

MsgBox(0, "Notice - Error", "Please make required entries and Retry." & @CRLF & "Enties marked with * are required.", 0)

Return

EndIf

_CreateSCP()

_DiscRAS()

_DialRAS()

_Check()

_Connect()

EndFunc ;==>_CheckPort

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, " ; 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()

MsgBox(0, "Info", "RAS connection " & GUICtrlRead($Input_1) & " will be disconnected.", 6)

run("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 621

MsgBox(16, "RAS Error", "Cannot open the phone book file", 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 0

MsgBox(32, "RAS Info", "The RAS connection was successful", 3)

Case Else

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

EndSwitch

EndFunc ;==>_DialRAS

Func _Check()

MsgBox(0,"Info", "Telnet is set to " & $UseTelnet & " SSH is set to " & $UseSSH, 2)

EndFunc ;==>_Check

Func _Connect()

;MsgBox(0, "Info", "RAS dial result was " & $RET, 3)

If $RET <> 0 Then

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

Return

EndIf

MsgBox(0,"Info", "Telnet is set to " & $UseTelnet & " SSH is set to " & $UseSSH, 2)

; Sleep(5000)

; GUISetState(@SW_HIDE)

; Run("c:\windows\system32\telnet.exe")

; WinWaitActive("c:\windows\system32\telnet.exe")

; Sleep(3000)

; Replace XXXXX with the name of your server

; Send("op 192.168.1.1{ENTER}")

; WinWaitActive("Telnet 192.168.1.1")

EndFunc ;=>_Connect

Func _KillRAS()

If GUICtrlRead($Input_1) = "" Then

MsgBox(0, "Notice - Error", "No RAS port was selected! Please try again.", 0)

Return

EndIf

MsgBox(0, "Info", "The RAS connection on " &GUICtrlRead($Input_1) & ", if present, will be disconnected.", 6)

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

; _DialRAS()

EndFunc ;==>_KillRAS

Func _UseSSH()

$UseSSH = "Yes"

$UseTelnet = "No"

;MsgBox(0,"Info", "SSH is set to " & $UseSSH & "Telnet is set to " & $UseTelnet, 2)

; Run("putty 10.1.1.241")

EndFunc ;==>_UseSSH

Func _UseTelnet()

$UseTelnet = "Yes"

$UseSSH = "No"

EndFunc ;==>_TelnetRAS

Func GoodBye()

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

Exit

EndFunc ;==>GoodBye

Func _Exit ()

Exit

EndFunc ;==> Exit

CC

Share this post


Link to post
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
Sign in to follow this  
Followers 0