【Linux】ssh自动输入密码

博客介绍了expect命令的使用,包括配置免密登录,若用户无ssh key,可通过ssh-keygen生成,再将本地id_rsa.pub追加到远程主机的authorized_keys文件。还给出了scp和ssh通用的expect交互脚本,介绍了参数说明及执行注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 使用expect命令

[root@localhost /tmp/sdl/test]#expect ssh.exp 192.168.37.128
spawn ssh root@192.168.37.128 ls /home/
root@192.168.37.128's password:
guest
sdl
[root@localhost /tmp/sdl/test]#cat ssh.exp
#!/usr/bin/expect  
set timeout 30

set ip [lindex $argv 0]
set username root
set password 111111
spawn ssh $username@$ip "ls /home/"
expect {
        "(yes/no)" {send "yes\r"; exp_continue}
        "password:" {send "$password\r"}
}
interact

2 配置免密登录

若用户没有ssh key则先通过执行ssh-keygen生成ssh key
在这里插入图片描述
将本地的id_rsa.pub追加到远程主机的authorized_keys文件集中
scp id_rsa.pub 192.168.37.128:~/.ssh/tmp #注意不要覆盖远程机器的id_rsa.pub
ssh 192.168.37.128 -t “cd ~/.ssh/;cat tmp >> authorized_keys;rm -rf tmp”
验证登录
ssh 192.168.37.128
在这里插入图片描述

3 scp和ssh通用的expect交互脚本

#!/usr/bin/expect
set command_1 [lindex $argv 0]
set command_2 [lindex $argv 1]
set command_3 [lindex $argv 2]
set check_key [lindex $argv 3]
set passwords [lindex $argv 4]

set timeout 30
if { $command_1 == "ssh" } {
        spawn ssh $command_2 $command_3
        expect {
                "(yes/no)" {send "yes\r";exp_continue}
                "assword" {send "$passwords\n";exp_continue}
                "$check_key" {send ""}
        }
} else {
        spawn bash -c "scp -o StripHostKeyChecking=no $command_2 $command_3"
        expect {
                "(yes/no)" {send "yes\r";exp_continue}
                "assword" {send "$passwords\n";exp_continue}
                "$check_key" {send ""}
        }
}

expect eof
catch wait result
exit [lindex $result 3]
interact

执行演示:

[root@localhost /home/Sudley]#./exp "ssh" "root@192.168.37.13" "ls /home/Sudley" "." "111111"
spawn ssh root@192.168.37.13 ls /home/Sudley
ssh: connect to host 192.168.37.13 port 22: No route to host
[root@localhost /home/Sudley]#echo $?
255
[root@localhost /home/Sudley]#./exp "ssh" "root@192.168.37.132" "ls /home/Sudley|head" "123.in" "111111"
spawn ssh root@192.168.37.132 ls /home/Sudley|head
root@192.168.37.132's password:
123_1.txt
123.in
123.out
123.txt
a.out
array
array.c
array.h
array.o
exp

[root@localhost /home/Sudley]#echo $?
0
[root@localhost /home/Sudley]#

参数说明:
command_1 - command_3是ssh或者scp命令的组成部分
set check_key [lindex $argv 3]是检查命令是否执行成功的参数,用于ssh或者scp命令不需要输入password的场景(当不确定是否需要输入password时很有用)
if { $command_1 == “ssh” } {最后的"} {“中间需要空格隔开,要不可能会报错"extra characters after close-brace”

catch wait result
exit [lindex $result 3]
expect脚本执行退出后退出状态码为ssh/scp命令的退出状态码

注意到scp前面有个bash -c,这是是scp命令支持通配符*
如果不带bash -c,则scp命令$command_2 $command_3部分的*就是*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值