Sign in to follow this  
Followers 0
i3igmind

New to AutoIt need help telnet/ssh

4 posts in this topic

Hello everyone,

I am new to AutoIt and i would like to ask for some guide please:

Purpose:

I use my windows box to telnet/ssh to unix machines to perform daily duties! run command and backups.

Question:

can i design an AutoIt GUI and code it so that i can assign a command to a button, then when i click on that button it will ssh or telnet to a specific unix box and perform the task?

Thank you in advance

i would like to make a GUI

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Hi,

I have written a AutoIt script which uses plink tool in the backend which connect to a Linux/ Solaris PC for SSH connection.

#include <GUIConstants.au3>
#include <Process.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <file.au3>

GUICreate("Testcase Run Window", 550, 570, (@DesktopWidth - 550) / 2, (@DesktopHeight - 570) / 2)

GUISetBkColor(0x00CC00)

$filemenu = GuiCtrlCreateMenu ("File")
$fileitem = GuiCtrlCreateMenuitem ("Open Testcase",$filemenu)
$recentfilesmenu = GuiCtrlCreateMenu ("Recent Files",$filemenu)
$separator1 = GuiCtrlCreateMenuitem ("",$filemenu)
$exititem = GuiCtrlCreateMenuitem ("Exit",$filemenu)


$TC_EditTab=GUICtrlCreateTab (0,0, 550,530); 5,10,540,535

$Tab1=GUICtrlCreateTabitem ("SSH Window")
Dim $File21=GuiCtrlCreateLabel("Enter Command:", 20, 100)
Dim $IP_Add=GuiCtrlCreateLabel("IP Address:", 20, 40)
Dim $IP_Add_Val=GUICtrlCreateInput ( "", 85, 35, 160, 20)
Dim $UserID=GuiCtrlCreateLabel("SSH User:", 20, 70)
Dim $UserID_Val=GUICtrlCreateInput ( "", 85, 65, 160, 20)
Dim $UserPassword=GuiCtrlCreateLabel("Password:", 270, 70)
Dim $UserPassword_Val=GUICtrlCreateInput ( "", 330, 65, 160, 20)
Dim $h_fileIn = GUICtrlCreateInput("", 20,120, 380, 20)
Dim $SSH_Run = GUICtrlCreateButton("Run", 430,120, 100, 20)
Dim $h_In2 = GUICtrlCreateEdit("", 20, 150, 510, 290)
;Dim $h_In2 = GUICtrlCreateEdit("", 20, 110, 510, 320, $ES_AUTOVSCROLL + $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL)

GUICtrlCreateGroup("AutoIt Help", 20, 450, 240, 50)
$h_Help = GUICtrlCreateButton("HELP", 30, 468, 50, 20)
Dim $File36=GUICtrlCreateInput ( "", 90, 468, 160, 20)

Dim $right = 0
Dim $Movinglabel=GUICtrlCreateLabel ( "** Developed By Amitav **", 10,535)


GuiSetState()
Dim $search= ""
Dim $wait = 20; wait 20ms for next progressstep
Dim $s = 0; progressbar-saveposition
Dim $i
Dim $m
Dim $value1,$initialDir
Dim $TraceHandle = -1

ProcessClose("plink.exe")

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop

        Case $msg = $h_Help
            $search=$File36
            Dim $rc = _RunDos("start http://www.google.co.in/search?hl=en&source=hp&q="&$search&"&meta=&rlz=1R2ADSA_enIN340&aq=f&oq=")
            If $rc>0 Then
                MsgBox(0, "Success", "Hurray!")
            ElseIf  $rc = 0 Then
                MsgBox(0, "error", "Cannot open search request - sorry")
            EndIf

        Case $msg = $exititem
            ExitLoop

        Case $msg = $SSH_Run
            GUICtrlSetData($h_In2, "")
            ;MsgBox(0, "INFO","Inside RUN case")
        If $h_fileIn="" Then
            MsgBox(0, "HELP","Please Enter Shell Command!")
        Else
            Dim $data2 = GUICtrlRead($h_fileIn)
            SSH($data2)
        EndIf
    EndSelect
WEnd

GUIDelete()

Exit



Func SSH($data)
Trace ($data)
Dim $rc_val
Dim $User=GUICtrlRead($UserID_Val)
Dim $Password=GUICtrlRead($UserPassword_Val)
Dim $SSH_IP=GUICtrlRead($IP_Add_Val)
Dim $OutputFile="output.txt"
Dim $CommandFile="Dummy.txt"
Dim $TraceFileName="C:\Documents and Settings\Dummy.txt"
    Dim $TraceHandle1 = FileOpen($TraceFileName, 0)
    ; Check if file opened for writing OK
    If $TraceHandle1 = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    FileClose($TraceHandle1)
$rc_val=_RunDOS("plink -ssh -l " & $User & " -pw "& $Password & " -m .\" &$CommandFile & " " & $SSH_IP & " > "&$OutputFile)

Dim $TraceHandle2 = FileOpen($OutputFile, 0)
    ; Check if file opened for writing OK
    If $TraceHandle2 = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
Dim $str3 = FileRead($OutputFile)
FileClose($TraceHandle2)

    Dim $Tut_text
    If Not _FileReadToArray($OutputFile, $Tut_text) Then ; read the file to an array.
        MsgBox(4096, "FileReadToArray Error", " Error reading log to Array     error:" & @error)
        Return
    EndIf
    For $x = 1 To $Tut_text[0] ; for loop to place the read file into the edit.
        GUICtrlSetData($h_In2, $Tut_text[$x] & @CRLF, 1)
    Next
EndFunc



;=========================================================================================
; Trace(): Used for identifing the trace level.
; Input Arguments : Trace level, sentences & values to be written into the log.
;=========================================================================================
Func Trace ($data1)
    Dim $TraceFileName="C:\Documents and Settings\Dummy.txt"
    $TraceHandle = FileOpen($TraceFileName, 2)
    ; Check if file opened for writing OK
    If $TraceHandle = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    FileWrite($TraceHandle, $data1 & @CRLF)
    FileClose($TraceHandle)
EndFunc

To do list:

a) Also create 3 empty files in the DIR: C:\Documents and Settings\

1. Dummy.txt

2. output.txt

3. command.txt

:( Also download plink.exe from internet and copy it to C:\Documents and Settings\

Now run the above script....

Enjoy and do let me know if you want something more.......

Regards

Amitav Nayak

Edited by AmitavNayak

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