gpt4 book ai didi

linux - 即使前一个命令未结束,Bash 脚本也会继续

转载 作者:太空宇宙 更新时间:2023-11-04 04:51:59 26 4
gpt4 key购买 nike

我有命令 ssh tony@mark 并希望通过 echo password 输入密码。我不想使用 sshpass ,并且作为后台进程 ssh tony@mark & ,它不起作用。即使上一个命令没有结束,有什么方法可以在 bash 脚本中继续吗?所以最终的脚本应该像这样工作:

#!/bin/bash
ssh tony@mark
echo -e 'MYPASSWORD\n'

最佳答案

您需要使用过的 ssh key 。生成 rsa key 对:ssh-keygen

然后使用一个简单的命令将其复制到服务器上:ssh-copy-id userid@hostname

您现在无需密码即可登录:ssh userid@hostname

<小时/>

但是如果您想使用密码安装 sshpass 实用程序(存在于 apt/yum 中),请尝试一下:

sshpass -p your_password ssh user@hostname
<小时/>

预期:

#!/usr/bin/expect -f
# ./ssh.exp password 192.168.1.11 id
set pass [lrange $argv 0 0]
set server [lrange $argv 1 1]
set name [lrange $argv 2 2]

spawn ssh $name@$server
match_max 100000
expect "*?assword:*"
send -- "$pass\r"
send -- "\r"
interact

例如:

# ./ssh.exp.ex password mark tony
spawn ssh tony@mark
tony@mark's password:
Linux ubuntu-1010-server-01 2.6.35-25-generic-pae #44-Ubuntu SMP Fri Jan 21 19:01:46 UTC 2011 i686 GNU/Linux
Ubuntu 10.10

Welcome to Ubuntu!
* Documentation: https://help.ubuntu.com/
Last login: Tue Mar 1 12:41:12 2011 from localhost

关于linux - 即使前一个命令未结束,Bash 脚本也会继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55647613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com