rsync 退出并显示消息“stdin is not a tty”
我想使用 rsync 到我有 SSH 访问权限的远程服务器。我使用以下命令:
rsync -e 'ssh -p 22222' -rtz --delete content_dir/ [电子邮件受保护]:/home/user/public_html
输入命令后,它会要求输入远程位置的密码。当我输入它时,它会退出并显示消息,
stdin:不是 tty
如何向 rsync 提供密码?当我在 shell 脚本中使用建议的方法时,它也应该有效。
I want to use rsync to my remote server for which I have SSH access. I use the following command:
rsync -e 'ssh -p 22222' -rtz --delete content_dir/ [email protected]:/home/user/public_html
After entering the command, it asks for the password for the remote location. When I type it, it exits with the message,
stdin: is not a tty
How do I supply the password to rsync? The method suggested should also work when I use it in a shell script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要添加:
到位于您的主目录中的 .bashrc 的开头。
You need to add:
to the beginig of .bashrc that is located in your home dir.
正如您自己所说,操作确实发生了,密码正在被接受。
错误消息“stdin:不是 tty”是由于服务器上的启动脚本中的某些内容试图处理仅应在交互式登录时发生的操作(当您使用 ssh 直接连接到服务器时等)。
The password is being accepted here, as you've stated yourself the operation does happen.
The error message "stdin: is not a tty" is due to something in the startup script on your server attempting to process an action that should only happen for interactive logins (when you connect with ssh direct to the server, etc).
<代码>[ -z "$PS1" ] && return 解决了问题,但它检查提示字符串长度是否为零,如果为零则退出。尽管 $PS1 不会在非交互式 shell 中设置,但 $PS1 的长度为零并不最终意味着该 shell 不是交互式的。
更好的方法是使用
$-
检查 shell 的当前选项。例如[[ $- != *i* ]] &&返回
。[ -z "$PS1" ] && return
solves the problem but it checks whether the prompt string length equals to zero and if it does then exits. Although $PS1 will not be set in a non-interactive shell, $PS1 being of zero length doesn't ultimately mean that the shell is not interactive.Better approach is to check the shell's current options using
$-
. For example[[ $- != *i* ]] && return
.如果简单的
return
无法完成这项工作,这里有另一种方法,取自 这篇博客文章:tty -s
检查是否附加了 TTY(-s
告诉它这样做默默地退出并使用适当的返回代码)。tty
返回附加的 tty(例如“/dev/pts/1”)。这应该比检查某些 shell 变量更安全;)mesg
控制对终端的写入访问(msg n
不允许写入(在我们的例子中不存在的)终端) ,因此需要一个人在场。在某些系统上(在我的例子中是 Debian Jessie,但也有关于 Ubuntu 的报告)
mesg n
1 在中无条件设置~/.bashrc
或~/.profile
。因此,如果它以这种方式存在,那么这可能就是罪魁祸首。与其他示例一样,您当然可以将其设为单行:
[[ $(tty -s ) ]] &&消息n。没有人阻止您将两者结合起来:
顺便说一句:根据链接的文章,此片段应该转到您连接的机器的
.bashrc
(“远程”) ) – 因此,如果是johndoe@somehost
,则应在somehost
上的/home/johndoe/.bashrc
开头应用。就我而言,我只是在“呼叫主机”上应用了此更改后才删除了该消息。PS:还要检查
.profile
是否有独立的msg n
命令(在我的例子中确实如此)。如果有,请将其包裹在那里。1:
mesg n
用于防止机器上的其他用户写入您当前的终端设备,这本身是一件好事 - 但对某些人来说没有帮助rsync
作业;)In case a simple
return
doesn't do the job, here's another approach taken from this blog article:tty -s
checks if there's a TTY attached (the-s
tells it to do so silently and just exit with the appropriate return code).tty
returns the tty attached (e.g. "/dev/pts/1"). This should be safer than checking some shell variable ;)mesg
controls the write access to your terminal (msg n
disallows writing to the (in our case non-existing) terminal), and thus requires one to be present.On some systems (in my case Debian Jessie, but there are also reports on Ubuntu)
mesg n
1 is set unconditionally in either~/.bashrc
or~/.profile
. So if it exists that way, this might be the culprit.As with the other examples, you can of course make that a one-liner:
[[ $(tty -s ) ]] && mesg n
. And nobody keeps you from combining the two:Btw: According to the linked article, this fragment should go to the
.bashrc
of the machine you connect to (the "remote") – so if that'sjohndoe@somehost
, this should be applied at the start of/home/johndoe/.bashrc
onsomehost
. In my case I only got rid of the message after having applied this change on the "calling host" as well.PS: Also check the
.profile
if it has a stand-alonemsg n
command (it did in my case). If it does, wrap it there.1:
mesg n
is used to prevent other users on the machine writing to your current terminal device, which per se is a good thing – but not helpful for somersync
job ;)