# ----------------------------------------------------------------------------- # EXPECT NOTLARI # ----------------------------------------------------------------------------- expect komutunun kullanımı hakkındaa notlar. # ----------------------------------------------------------------------------- # KURULUM # ----------------------------------------------------------------------------- apt-get install expect # ----------------------------------------------------------------------------- # autoexpect # ----------------------------------------------------------------------------- Bir oturumu izleyip expect dosyası hazırlar. # Kullanımı autoexpect komut # Parametreler -f oluşacak dosyanın ismi. Belirtilmezse script.exp oluyor. -c conservative mode Cevabı göndermeden önce kısa beklemeler koyuyor. Bu bekleme olmazsa bazı programlar hızlı veren cevapları dikkate almıyor. -p prompt mode Gelen çıktıların tamamını değiş, sadece son satırı dikkate alıyor. # ----------------------------------------------------------------------------- # expect # ----------------------------------------------------------------------------- TCL komutları kullanılmakta. #!/usr/bin/expect -f # argument set arg1 [lindex $argv 0] # spawn spawn komut # exit exit # expect expect "beklenen ifade" expect -re "beklenen regex ifade" expect -re "\\\[Y/n]:" expect eof expect "\n" { set raw $expect_out(buffer) set response [string trimright "$raw" "\n"] } expect { "beklenen1" { send "komut1\n" } "beklenen2" { send "komut2\n" } default { exit } } # expect_out expect -re "tarih (.*) olunca" if {$expect_out(1,string)!="2017"} { send "2017 bulundu" } set raw $expect_out(buffer) # send send "text" send "\n" # send_tty send_tty "prompt: " # send_user if {$argc<2} { send_user "usage: $argv0 user message\n" exit } # set set key1 "value1" set timeout 10 # ----------------------------------------------------------------------------- # Pexpect # ----------------------------------------------------------------------------- Python3 expect paketi # kurulum apt-get install python3-pexpect # ornek import pexpect child = pexpect.spawn('ftp ftp.openbsd.org') child.expect('Name .*: ') child.sendline('anonymous') # after & before child.after : beklenen regex'e uyan kısım child.before : beklenen kısımdan once gelenler # close child.close() # expect child.expect('^Login.*:') child.expect('^Login.*:', timeout=3) match_index = child.expect(['^Login.*:', pexpect.EOF], timeout=3) # send & sendline child.send('mypassword\n') child.sendline('mypassword') # exceptions pexpect.TIMEOUT # ----------------------------------------------------------------------------- # KAYNAKLAR # ----------------------------------------------------------------------------- http://www.linux-mag.com/id/699/ https://pexpect.readthedocs.io/en/stable/index.html