我可以用一个定界符输入在bash密码? [英] Can I use a heredoc to enter a password in bash?

查看:140
本文介绍了我可以用一个定界符输入在bash密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道RSA身份验证,但我的目的,我想用一个定界符指定密码。我想要的东西,像下面这样,但我不能得到它的工作。这甚至可能?

 #!/斌/庆典
呼应-n输入密码
阅读-s密码
SSH为myhost<< EOL
$密码
回声我登录到为myhost
EOL
做呼应

这是我所得到的,当我尝试:

  $ ./testssh
输入密码:
伪终端将不被分配,因为标准输入不是终端。
用户@为myhost的密码:
警告:无xauth的数据;使用假身份验证数据X11转发。
警告:对tty的访问(错误的文件描述符)。
因此,在这个shell没有作业控制。
输入mypassword:命令未找到。
我登录到为myhost
DONE

编辑:

根据bmargulies的回答,我返工我的脚本,并具有以下想出了:

 #!/斌/庆典
呼应-n输入主机
阅读HOST
呼应-n输入用户名:
读取用户
呼应-n输入密码
阅读-s PASS
VAR = $(预计-c
产卵的ssh $ USER @ $ HOST
预计\\密码:\\
发送\\$ PASS \\ r \\
预计\\> \\
发送\\LS \\ r \\
发送\\呼应我\\'米的$ HOST'\\ r \\
预计-re \\东西\\
发送\\退出\\

回声-e\\ n \\ n \\ n ========
回声VAR =$ VAR
做呼应


解决方案

程序读取密码经常专门打开/ dev / tty的击败重定向。在这种情况下,你需要的工具是希望,这将运行一个伪终端后面。

I know about RSA authentication, but for my purposes I want to use a heredoc to specify the password. I want something like the following, but I can't get it to work. Is this even possible?

#!/bin/bash
echo -n "Enter Password: "
read -s password
ssh myhost << EOL
$password
echo "I'm logged onto myhost"
EOL
echo done

This is what I get when I try it:

$ ./testssh 
Enter Password: 
Pseudo-terminal will not be allocated because stdin is not a terminal.
user@myhost's password: 
Warning: No xauth data; using fake authentication data for X11 forwarding.
Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell.
mypassword: Command not found.
I'm logged onto myhost
done

EDIT:

Based on bmargulies' answer, I reworked my script and came up with the following:

#!/bin/bash
echo -n "Enter the Host: "
read HOST
echo -n "Enter Username: "
read USER
echo -n "Enter Password: "
read -s PASS
VAR=$(expect -c "
spawn ssh $USER@$HOST
expect \"password:\"
send \"$PASS\r\"
expect \">\"
send \"ls\r\"
send \"echo 'I\'m on $HOST'\r\"
expect -re \"stuff\"
send \"logout\"
")
echo -e "\n\n\n========"
echo VAR = "$VAR"
echo done

解决方案

Programs that read passwords often specifically open /dev/tty to defeat redirection. In which case, the tool you need is 'expect', which will run one behind a pseudo-tty.

这篇关于我可以用一个定界符输入在bash密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆