繁体   English   中英

Bash脚本继续,即使先前的命令未结束

[英]Bash script continue even previous command not ended

我有ssh tony@mark命令,并希望通过echo password输入echo password 我不想使用sshpassssh tony@mark &作为后台进程ssh tony@mark &它不起作用。 即使前面的命令没有结束,还有什么方法可以在bash脚本中继续吗? 因此,最终脚本应如下所示:

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

您需要使用ssh-key。 生成RSA密钥对: 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM