1
2
|
# mkdir /root/.ssh
# chmod 700 /root/.ssh
|
1
|
# scp /root/.ssh/id_rsa.pub root@服务端IP:/root/.ssh/authorized_keys #id_rsa.pub可以追加多个客户端的公钥
|
1
2
3
4
5
6
|
# vi /etc/ssh/sshd_config
RSAAuthentication
yes
#这三行取消注释,开启密钥对验证
PubkeyAuthentication
yes
AuthorizedKeysFile .
ssh
/authorized_keys
PasswordAuthentication no
#关闭密码验证
# service sshd restart
|
1
|
# ssh root@服务端IP 'df -h'
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/usr/bin/expect
set
ip 192.168.1.156
set
pass 123.com
set
timeout 30
spawn
ssh
root@$ip
expect {
"(yes/no)"
{send
"yes\r"
; exp_continue}
"password:"
{send
"$pass\r"
}
}
expect
"root@*"
{send
"df -h\r"
}
expect
"root@*"
{send
"exit\r"
}
expect eof
# interact
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#!/bin/bash
user=root
pass=
'123'
ip=
'192.168.1.154'
/usr/bin/expect
<< EOF
set
timeout 30
spawn
ssh
$user@$ip
expect {
"(yes/no)"
{send
"yes\r"
; exp_continue}
"password:"
{send
"$pass\r"
}
}
expect
"root@*"
{send
"df -h\r"
}
expect
"root@*"
{send
"exit\r"
}
expect eof
EOF
|
1
2
3
4
5
6
7
8
9
10
11
|
#!/bin/bash
user=root
pass=
'123'
ip=
'192.168.1.154'
expect -c "
spawn
ssh
$user@$ip
expect {
\"(
yes
/no
)\" {send \"
yes
\r\"; exp_continue}
\"password:\" {send \"$pass\r\"; exp_continue}
\"root@*\" {send \"
df
-h\r
exit
\r\"; exp_continue}
}"
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# vi login.exp #免交互登陆脚本
#!/usr/bin/expect
set
ipaddress [lindex $argv 0]
set
username [lindex $argv 1]
set
password [lindex $argv 2]
if
{ $argc != 3 } {
puts
"Usage: expect login.exp ipaddress username password"
exit
1
}
set
timeout 30
spawn
ssh
$username@$ipaddress
expect {
"(yes/no)"
{send
"yes\r"
; exp_continue}
"password:"
{send
"$password\r"
}
}
expect
"$username@*"
{send
"df -h\r"
}
expect
"$username@*"
{send
"exit\r"
}
expect eof
|
1
2
3
4
5
6
7
8
9
10
11
|
# vi user_info #用户信息文件
192.168.1.156 user user
192.168.1.154 root 123.com
# vi expect.sh #读取用户信息并赋值到变量
#!/bin/bash
for
ip
in
`
awk
'{print $1}'
user_info`
do
user=`
awk
-
v
I=
"$ip"
'{if(I==$1)print $2}'
user_info`
pass=`
awk
-
v
I=
"$ip"
'{if(I==$1)print $3}'
user_info`
expect login.exp $ip $user $pass
done
|