Ringkasan
Admin sistem Linux biasanya login ke server Linux dengan memberikan kata sandi atau menggunakan otentikasi berbasis kunci. sshpass adalah alat yang memungkinkan kita untuk memberikan kata sandi secara otomatis ke command prompt sehingga skrip otomatis dapat dijalankan sesuai keinginan pengguna. sshpass memasok kata sandi ke ssh Prompt menggunakan tty khusus, membodohi ssh untuk percaya bahwa pengguna interaktif memberikan kata sandi.
Beberapa penggunaan umum sshpass
1. mengambil cadangan ke server jauh
2. mengeksekusi perintah pada sistem pada waktu tertentu.
Instalasi sshpass
1. Distribusi Berbasis Centos:
Siapkan repositori EPEL dari https://fedoraproject.org/wiki/EPEL dan kemudian sebagai root run:
# yum -y install sshpass
2. Distribusi berbasis Ubuntu/Debain:
Sebagai root run:
# apt-get install sshpass
3. Kompilasi &instal dari sumbernya:
# wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz # tar -zxvf sshpass.tar.gz # cd sshpass-1.05/ # ./configure # make # make install # which sshpass /usr/local/bin/sshpass
Mendapatkan Bantuan
# sshpass -h Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters -f filename Take password to use from file -d number Use number as file descriptor for getting password -p password Provide password as argument (security unwise) -e Password is passed as env-var "SSHPASS" With no parameters – password will be taken from stdin -h Show help (this screen) -V Print version information
Maksimal salah satu dari -f, -d, -p atau -e harus digunakan
Contoh 1:berikan kata sandi dengan ssh
# sshpass -p 'password' ssh ldap.thegeekdiary.com -l root -o StrictHostKeyChecking=no
Dimana:
sandi adalah kata sandi server Anda (ldap.thegeekdiary.com). ‘StrictHostKeyChecking=no ' digunakan untuk mengontrol login ke mesin yang kunci hostnya tidak diketahui atau telah diubah.
CONTOH:2 UNTUK MENJALANKAN BEBERAPA COMMAND PADA SERVER REMOTE VIZ MEMERIKSA UPTIME DAN UNAME
Contoh 2:Untuk menjalankan beberapa perintah di server jauh
Mari kita coba menjalankan 2 perintah “uptime” dan “uname” di server jauh menggunakan perintah sshpass:
# sshpass -p 'password' ssh ldap.thegeekdiary.com -l root -o StrictHostKeyChecking=no "uptime;uname -a"
Contoh Keluaran:
18:49:34 up 21 days, 18:49, 3 users, load average: 0.01, 0.00, 0.00 Linux ldap.thegeekdiary.com 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux
Contoh 3:Menyalin file menggunakan rsync ke server
Dalam kasus kami, kami menyalin file sshpass.tar.gz ke server jauh ldap.thegeekdiary.com
# sshpass -p 'password' rsync -av --progress sshpass.tar.gz [email protected]:/tmp/
Output dari Perintah di atas:
sending incremental file list sshpass.tar.gz 98362 100% 62.56MB/s 0:00:00 (xfer#1, to-check=0/1) sent 98472 bytes received 31 bytes 197006.00 bytes/sec total size is 98362 speedup is 1.00 root@server1:/home/thegeekdiary#
Contoh 4:for loop untuk menyalin ke server jauh
Buat file sebagai berikut:
# touch /tmp/scr
File harus berisi nama host:
server2.thegeekdiary.com server3.thegeekdiary.com server4.thegeekdiary.com server5.thegeekdiary.com
# for i in `cat /tmp/scr`; do echo " ";echo "###$i####"; sshpass -p 'password' rsync -av --progress sshpass.tar.gz root@$i:/tmp/; done
Output dari Perintah Di Atas:
###server2.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 132.00 bytes/sec total size is 98362 speedup is 1490.33 ###server3.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 44.00 bytes/sec total size is 98362 speedup is 1490.33 ###server4.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 132.00 bytes/sec total size is 98362 speedup is 1490.33 ###server5.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 132.00 bytes/sec total size is 98362 speedup is 1490.33