Pages


Thứ Sáu, 27 tháng 9, 2013

Tự động hóa cài đặt phần mềm với Autoit

Tự động hóa cài đặt phần mềm với Autoit

Ở bài này mình sẽ không hướng dẫn tổng quát mà đi vào cụ thể một phần mềm nhất định đó là cài đặt Internet Download Manager (Chạy ngầm) ... sau đó các bạn tự tìm hiểu và thao tác tương tự với các phần mềm khác
Nguyên liệu cho bài này :
  •  Công cụ AutoIt Window Info có sẵn trong gói cài đặt Autoit
  • IDM bản mới nhất (không có cũng không sao , có thể thêm vào code download IDM)
  • Một bản patch IDM (không có cũng không sao vì mã nguồn đã có sẵn)




Hướng dẫn sử dụng công cụ AutoIt Window Info :

Công cụ AutoIt Window Info được dùng để lấy thông tin (tiêu đề , trạng thái , class vị trí) của cửa sổ , và các control phụ thuộc .

Sử dụng : kéo thả crosshair trong group Finder Tool vào control của cửa sổ hiện hành để lấy những thông tin cần thiết



Các bạn có thể tham khảo thêm hướng dẫn sử dụng công cụ này tại trang chủ của Autoit : AutoIt Window Information Tool

Sau khi đã biết cách sử dụng công cụ này chúng ta bắt đầu lập trình ... sau đây là mã nguồn thực hiện tự động hóa việc download , cài đặt , crack và đăng ký serial IDM hoàn chỉnh phiên bản mới nhất từ trang chủ :


Mã nguồn IDM Sillent Installer :

#RequireAdmin ; Sử dụng cho win 7 trở lên có UAC
#include<Inet.au3>
#include<String.au3>


;############## TÌm phiên bản mới nhất và download về máy tính ############
$LinkDownload=_StringBetween(_INetGetSource('http://internetdownloadmanager.com/download.html'),'http://mirror2.internetdownloadmanager.com/','.exe')
$vername=$LinkDownload[0]
$LinkDownload='http://mirror2.internetdownloadmanager.com/'&$vername&'.exe' ; Link download IDM bản mới nhất
_InetGetProgress($LinkDownload,@ScriptDir) ; Download về máy tính để bắt đầu cài đặt (có thanh trạng thái)
ShellExecute(@ScriptDir&'\'&$vername&'.exe') ; Thực thi file cài đặt IDM sau khi đã download xong
;#########################################################################

WinWaitActive('Internet Download Manager Installation Wizard')  ; Tạm ngưng script cho đến khi cửa sổ active
ControlFocus('Internet Download Manager Installation Wizard','','[CLASS:Button; INSTANCE:2]') ; Focus vào control cần click
ControlClick('Internet Download Manager Installation Wizard','','[CLASS:Button; INSTANCE:2]','left') ; Click vào control với class xác định bởi Autoit info tool
WinWaitActive('Please read IDM license')
ControlFocus('Please read IDM license','','[CLASS:Button; INSTANCE:2]')
ControlClick('Please read IDM license','','[CLASS:Button; INSTANCE:2]','left')
WinWaitActive('Choose Destination Location')
ControlFocus('Choose Destination Location','','[CLASS:Button; INSTANCE:2]')
ControlClick('Choose Destination Location','','[CLASS:Button; INSTANCE:2]','left')
WinWaitActive('Select Program Manager Group')
ControlFocus('Select Program Manager Group','','[CLASS:Button; INSTANCE:2]')
ControlClick('Select Program Manager Group','','[CLASS:Button; INSTANCE:2]','left')
WinWaitActive('Start Installation of Internet Download Manager')
ControlFocus('Start Installation of Internet Download Manager','','[CLASS:Button; INSTANCE:2]')
ControlClick('Start Installation of Internet Download Manager','','[CLASS:Button; INSTANCE:2]','left')
WinWaitActive('Installation Complete')
ControlFocus('Installation Complete','','[CLASS:Button; INSTANCE:3]')
ControlClick('Installation Complete','','[CLASS:Button; INSTANCE:3]','left')

ProcessWait('IDMan.exe') ; Tạm ngưng script cho đến khi process IDMan xuất hiện
ProcessClose('IDMan.exe') ; Kill process IDMan để tiến hành crack

;######### Tải bản Crack IDM 6.12 build 22 và đăng ký serial ##############
_InetGetProgress('https://dl.dropbox.com/u/55630051/IDMan.exe', 'C:\Program Files\Internet Download Manager')
RegWrite('HKEY_LOCAL_MACHINE\SOFTWARE\Internet Download Manager',"FName", "REG_SZ","Blog Autoit Viet - Yagami Raito's Blog")
RegWrite('HKEY_LOCAL_MACHINE\SOFTWARE\Internet Download Manager',"LName", "REG_SZ","(http://hocautoit.co.cc)")
RegWrite('HKEY_LOCAL_MACHINE\SOFTWARE\Internet Download Manager',"Serial", "REG_SZ","BGL4H-HJBXT-ABSUD-R31QJ")
RegWrite('HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main',"Start Page", "REG_SZ","http://hocautoit.co.cc")
;#########################################################################


ShellExecute('C:\Program Files\Internet Download Manager\IDMan.exe') ; Chạy chương trình IDM

WinWaitActive('IDM browser integration','',5)
ControlFocus('IDM browser integration','','[CLASS:Button; INSTANCE:1]')
ControlClick('IDM browser integration','','[CLASS:Button; INSTANCE:1]','left')


Func _InetGetProgress($sURL, $sDirectory = @ScriptDir)
    Local $hDownload, $iBytesRead = 0, $iFileSize, $iPercentage, $iSpeed = 0, $iTimer = 0, $sFilePath, $sProgressText, $sSpeed
    $sFilePath = StringRegExpReplace($sURL, "^.*/", "")
    If @error Then
        Return SetError(1, 0, $sFilePath)
    EndIf

    $sDirectory = StringRegExpReplace($sDirectory, "[\\/]+\z", "") & "\" & $sFilePath
    $iFileSize = InetGetSize($sURL, 1)
    $hDownload = InetGet($sURL, $sDirectory, 0, 1)
    If @error Then
        Return SetError(1, 0, $sFilePath)
    EndIf
    ProgressOn("", "")

    $sSpeed = "Current Speed: " & _ByteSuffix($iBytesRead - $iSpeed) & "/s"
    $iTimer = TimerInit()
    While InetGetInfo($hDownload, 2) = 0
        $iBytesRead = InetGetInfo($hDownload, 0)
        $iPercentage = $iBytesRead * 100 / $iFileSize
        $sProgressText = "Downloading " & _ByteSuffix($iBytesRead, 0) & " Of " & _ByteSuffix($iFileSize, 0) & @LF & $sSpeed
        ProgressSet(Round($iPercentage, 0), $sProgressText, "Downloading: " & $sFilePath)

        If TimerDiff($iTimer) > 1000 Then
            $sSpeed = "Current Speed: " & _ByteSuffix($iBytesRead - $iSpeed) & "/s"
            $iSpeed = $iBytesRead
            $iTimer = TimerInit()
        EndIf
        Sleep(100)
    WEnd
    InetClose($hDownload)
    ProgressOff()
    Return $sFilePath
EndFunc   ;==>_InetGetProgress

; #INTERNAL_USE_ONLY#============================================================================================================
Func _ByteSuffix($iBytes, $iRound = 2)
    Local $A, $aArray[9] = [" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]
    While $iBytes > 1023
        $A += 1
        $iBytes /= 1024
    WEnd
    Return Round($iBytes, $iRound) & $aArray[$A]
EndFunc   ;==>_ByteSuffix
; #INTERNAL_USE_ONLY#============================================================================================================



Chạy xong chương trình các bạn sẽ nhận ra là cửa sổ cài đặt vẫn hiện phải không ? , đấy là do mình muốn hiện cửa sổ cho các bạn có thể theo dõi các tác vụ tự động mình họa cho mã nguồn ...

Nếu muốn biến chương trình này thành silent install , các bạn chỉ đơn giản là ẩn icon và các cửa sổ cài đặt IDM là hoàn tất , thực hiện như sau :
  • Thêm #NotrayIcon vào dòng đầu tiên của code
  • Thay ShellExecute(@ScriptDir&'\'&$vername&'.exe') thành ShellExecute(@ScriptDir&'\'&$vername&'.exe','','','',@SW_HIDE)
Vậy là ta đã hoàn thành xong bài 11: Tự động hóa cài đặt phần mềm IDM , với việc cài đặt các phần mềm khác , các bạn làm hoàn toàn tương tự như IDM ^^

Ở bài sau mình sẽ hướng dẫn các bạn cách viết một phần mềm trả lời tự động tin nhắn Yahoo theo nội dung soạn sẵn , các bạn nhớ đón xem và theo dõi ^^




Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

Nhận Tin Qua Email

Cùng tham gia với hơn 1500 người đã đăng ký nhận tin qua Email với các Tin mới nhất,Nóng nhất trong ngày cập nhật liên tục 24h trên Tintuc.tv từ các Báo Mạng uy tín của Việt Nam!

Khi đăng ký nhận tin,Bạn sẽ nhận được Email từ chúng tôi.Đăng nhập email để hoàn tất quá trình đăng ký.

0 nhận xét

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
Posts RSSComments RSSBack to top
Copyright © 2013 by Autoit Việt ∙.
Yagami Raito 's Blog - Nơi khởi đầu cho các lập trình viên.