目標:
運行腳本自動匹配用戶信息,自動登陸ssh。
條件:
ubuntu 10.04
安裝expect 與openssh
bash腳本(bash.sh)內容:
#!/bin/bash
echo "hello ,now you will login ssh!"
echo "show me the detail info-->##user: $1 ##addr: $2 ##passwd:$3"
./login-expect.sh $1 $2 $3
expect腳本(login-expect.sh)內容:
#!/usr/bin/expect
#parameter tran all by command.
#login in ssh
set timeout 30
set user [lindex $argv 0] #設置傳入參數為用戶 IP 密碼
set addr [lindex $argv 1]
set passwd [lindex $argv 2]
spawn ssh $user@$addr -p 22 #發送連接命令
expect "password:" #等待字段出現
send "$passwd\r"
interact #設置為交互式(即登陸后停留,如果是expect eof則是退出-->用於登陸做完事情后退出)