SSH exited without connecting to server in shell script

I am trying to connect to the servers and cd into each directory. But when I tried to SSH into the first server which is server1 , the error it gave me was

Pseudo-terminal will not be allocated because stdin is not a terminal.

This is the config file and bash script below:

getPatches.cfg

SERVERLIST="server1;server2"
USERNAME=bob
DIRECTORY=/net/bob

bash script

#!/bin/sh

CFGFILE=getPatches.cfg

if ! [ -f $CFGFILE ] ; then
   echo "ERROR: $CFGFILE configuration file is missing"
   exit 1
fi
# READ $CFGFILE
. $CFGFILE
servers=$SERVERLIST
user=$USERNAME
directory=$DIRECTORY

#Tokenize ServerList
while IFS=';' read -ra ADDR; do
        for i in "${ADDR[@]}"; do
                ssh $user@$i
                AccessIntoDirectory="cd $directory"
                export AccessIntoDirectory
                ssh -q $i $AccessIntoDirectory
        done
done <<< "$servers"

I've generated a SSH public key and what this script should do is to SSH into the server and cd into the directory that I want, but it exited instead and did not SSH into the server. Any help would be great!


不使用控制台(tty)而使用ssh命令的解决方案是添加-t -t选项。

ssh -t -t $user@$i

At first check the line

ssh $user@$i

It does not supply a command, so your script tries to start an interactive shell on the remote host. That's where you get the error from.

Secondly I'd like to check if you accidently used backticks `` instead of ticks '' or double quotes "" in the line with

`AccessIntoDirectory=`cd $directory`

which would falsely run the 'cd' command and assign it's output to the variable.

链接地址: http://www.djcxy.com/p/97136.html

上一篇: 将远程终端输出存储在一个变量中

下一篇: SSH在没有连接到shell脚本中的服务器的情况下退出