第一种方法(expect): cat expect.sh #!/usr/bin/expect #使用第一个参数 set local_ip [lindex $argv 0] #后面的也可以用参数[lindex $argv n] #set password [lindex $argv 1 ] #set password2 [lindex $argv 2] #set command [lindex $argv 3] set local_port 22 set local_user jackchem set local_11psss 11111111 set local_22psss 22222222 set timeout 30 spawn ssh -q [email protected]$local_ip expect { "192.168.20.* password" { send "$local_11psss\n"; } "192.168.21.* password" { send "$local_22psss\n"; } "yes/no" { send "yes\n"; exp_continue; } } interact 先chmod +x expect.sh 比如登录到192.168.20.99的话./auto.sh 20.99 比如登录到192.168.21.88的话./auto.sh 21.88 第二种方法(sshpass): cat sshpass.sh #!/bin/bash #-------------------------------------------------- #Created:2015-05-18 #Author:jimmygong #Mail:[email protected] #Function: #Version:1.0 #-------------------------------------------------- case $1 in 192.168.22[0-9].*) sshpass -f /root/.11pass ssh -q -p22 -l tomhong $1;; 192.168.23[0-9].*) sshpass -f /root/.22pass ssh -q -p22 -l tomhong $1;; 10.*.*.*) sshpass -f /root/.33pass ssh -q -p22 -l tomhong $1;; esac 先chmod +x sshpass.sh 比如登录到192.168.230.199的话./sshpass.sh 192.168.230.199 比如登录到10.1.1.55的话./sshpass.sh 10.1.1.55
时间: 2024-10-22 14:57:22