sshpass和expect远程登录并执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
我的博客已迁移到xdoujiang.com请去那边和我交流
一、使用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 $local_user@192.168.$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:jimmygong@taomee.com
#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
 
三、批量执行多台机器同一个操作(非交互模式)sshpass
基础环境说明
1、ip
serverA=192.168.1.8
serverB=192.168.1.7
serverC=192.168.1.10
 
2、 ssh 的-t和-o  "StrictHostKeyChecking=no" 参数
-o option Can be used to give options  in  the  format  used  in  the configuration  file .
This is useful  for  specifying options  for  which  there is no separate  command -line flag.
For full details of the options listed below,and their possible values,see ssh_config(5).
   
-t Force pseudo- tty  allocation.This can be used to execute arbitrary  screen -based programs on a remote machine,
which  can be very useful,e.g.when implementing menu services.Multiple -t options force  tty  allocation,even  if  ssh  has no  local  tty .
供一个远程服务器的虚拟 tty 终端
 
3、sshpass的-f参数
-f filename The password is the first line of the  file  filename.
支持从文件里读取密码
 
cat  sshpass.sh
#!/bin/bash
#--------------------------------------------------
#Author:jimmygong
#Email:jimmygong@taomee.com
#FileName:sshpass.sh
#Function: 
#Version:1.0 
#Created:2015-12-16
#--------------------------------------------------
port=22
user= "jimmy"
cmd= "/bin/ps -ef|grep mysql"
#cmd="/bin/df -Th"
allip=(
192.168.1.8
192.168.1.7
192.168.1.10
)
for  ip  in  ${allip[@]}
do 
     echo  $ip
     sshpass -f  /root/ .11pass  ssh  -o  "StrictHostKeyChecking=no"  -t -p$port $user@$ip $cmd
done
 
执行结果
bash  sshpass.sh
192.168.1.8
root       1992      1  0 09:57 ?        00:00:00  /bin/sh  /usr/bin/mysqld_safe
mysql      2331   1992  0 09:57 ?        00:00:00  /usr/sbin/mysqld  --basedir= /usr  --datadir= /var/lib/mysql  --plugin- dir = /usr/lib/mysql/plugin  --user=mysql --pid- file = /var/run/mysqld/mysqld .pid --socket= /var/run/mysqld/mysqld .sock --port=3306
root       2332   1992  0 09:57 ?        00:00:00 logger -t mysqld -p daemon.error
jimmy      2665   2664  0 10:09 pts /1     00:00:00  bash  -c  /bin/ps  -ef| grep  mysql
jimmy      2667   2665  0 10:09 pts /1     00:00:00  grep  mysql
Connection to 192.168.1.8 closed.
192.168.1.7
root       1983      1  0 09:57 ?        00:00:00  /bin/sh  /usr/bin/mysqld_safe
mysql      2310   1983  0 09:57 ?        00:00:00  /usr/sbin/mysqld  --basedir= /usr  --datadir= /var/lib/mysql  --plugin- dir = /usr/lib/mysql/plugin  --user=mysql --pid- file = /var/run/mysqld/mysqld .pid --socket= /var/run/mysqld/mysqld .sock --port=3306
root       2311   1983  0 09:57 ?        00:00:00 logger -t mysqld -p daemon.error
jimmy      2636   2635  0 10:09 pts /1     00:00:00  bash  -c  /bin/ps  -ef| grep  mysql
jimmy      2638   2636  0 10:09 pts /1     00:00:00  grep  mysql
Connection to 192.168.1.7 closed.
192.168.1.10
root       2160      1  0 09:57 ?        00:00:00  /bin/sh  /usr/bin/mysqld_safe
mysql      2505   2160  0 09:57 ?        00:00:04  /usr/sbin/mysqld  --basedir= /usr  --datadir= /var/lib/mysql  --plugin- dir = /usr/lib/mysql/plugin  --user=mysql --pid- file = /var/run/mysqld/mysqld .pid --socket= /var/run/mysqld/mysqld .sock --port=3306
root       2506   2160  0 09:57 ?        00:00:00 logger -t mysqld -p daemon.error
jimmy      2842   2841  0 10:09 pts /1     00:00:00  bash  -c  /bin/ps  -ef| grep  mysql
jimmy      2844   2842  0 10:09 pts /1     00:00:00  grep  mysql
Connection to 192.168.1.10 closed.









本文转自 xdoujiang 51CTO博客,原文链接:http://blog.51cto.com/7938217/1652452,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值