Expect Komutu ile SSH Bağlantısı Açmak için ssh.exp diye bir dosya oluşturalım. Aslında bilindiği üzere linux’de dosya çalıştırılacağı zaman uzantısına değil içeriğine bakar.
Bundan dolayı ‘.exp’ uzantısının herhangi bir fonksiyonu yoktur.Fakat alışkanlık olması açısından expect script yazıyorsak bu şekilde ilerlememiz.
Expect Script Oluşturma:
1 |
[root@unitednations ~]$# vim ssh.exp |
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/usr/bin/expect -f # Bu script iel password girmeden ssh bağlantısı kuruyoruz. spawn ssh user@ip_address #set timeout -1 expect "Are you sure you want to continue connecting (yes/no)?" send -- "yes\r" set timeout -1 expect "password: " send -- "mypassword" send -- "\r" interact # expect eof |
[email protected]_address kısmına ssh yapacağımız user ve ip adresimizi yazıyoruz.
send – “mypassword” şifre bölümüne şifremizi yazıyoruz.
1 |
[root@unitednations ~]$expect -f ssh.exp |
otomatik olarak bağlanacaktır.
Şimdi bu dosya ile ilgili alias oluşturalım.
1 2 |
[root@unitednations ~]$alias ssh_connect='expect -f ssh.exp' [root@unitednations ~]$ssh_connect |
dediğimizde alias’da tanımladığımız expect komutu ile ssh.exp dosyasını çalıştıracaktır ve otomatik bağlanabilirsiniz.
Alias ımızın sistemimizde kalıcı hale gelmesini istiyorsak;
1 |
[root@unitednations ~]$vim .bashrc |
dosyasına ” alias ssh_connect=’expect -f ssh.exp’ ” bu satırı ekliyoruz.