我正在尝试编写一个 shell 脚本,它在远程服务器上创建一些目录,然后使用 scp 将文件从我的本地计算机复制到远程服务器上。到目前为止,这是我所拥有的:
ssh -t user@server<<EOT
DEP_ROOT='/home/matthewr/releases'
datestamp=$(date +%Y%m%d%H%M%S)
REL_DIR=$DEP_ROOT"/"$datestamp
if [ ! -d "$DEP_ROOT" ]; then
echo "creating the root directory"
mkdir $DEP_ROOT
fi
mkdir $REL_DIR
exit
EOT
scp ./dir1 user@server:$REL_DIR
scp ./dir2 user@server:$REL_DIR
每当我运行它时,我都会收到以下消息:
Pseudo-terminal will not be allocated because stdin is not a terminal.
脚本永远挂起。
我的公钥在服务器上是受信任的,我可以很好地运行脚本之外的所有命令。有什么想法吗?
最佳答案
尝试 ssh -t -t
(或简称 ssh -tt
)强制伪 tty 分配,即使 stdin 不是终端。
另请参阅:Terminating SSH session executed by bash script
来自 ssh 手册页:
-T Disable pseudo-tty allocation.
-t Force pseudo-tty allocation. This can be used to execute arbitrary
screen-based programs on a remote machine, which can be very useful,
e.g. when implementing menu services. Multiple -t options force tty
allocation, even if ssh has no local tty.
https://stackoverflow.com/questions/7114990/