step1) $ yum install expect

step2) $ vim ssh.login.exp

------------------------------------------------------
#!/usr/bin/expect -f

set HOST [lindex $argv 0]
set USER [lindex $argv 1]
set PASS [lindex $argv 2]
set CMD [lindex $argv 3]
set timeout -1

spawn ssh -o StrictHostKeyChecking=no $USER@$HOST
match_max 100000

#若出現要輸入password
expect "*?assword:*"
#則輸入$PASS變數的字串,\r表示<Enter>鍵
send -- "$PASS\r"
send -- "\r"

expect "#"
send -- "$CMD\r"
expect eof
------------------------------------------------------

 

step3) $ expect ssh.login.exp 192.168.1.1 root password 'uname -a'

      root是帳號
      password是密碼
      uname -a是指令


dreamtails 發表在 痞客邦 PIXNET 留言(0) 人氣()