Search code examples
bashwhile-loopptystty

SSH while-loop in bash. Pseudo-terminal will not be allocated because stdin is not a terminal


I am trying to loop a file to ssh to a list of servers, and perform a find command on those servers for certain logfiles. I know that ssh will swallow the input file as a whole. So i am using the -n parameter to ssh. This works fine but oncertain servers i encounter a new error.

The input file is build like: servername:location:mtime:logfileexention

The code in Bash I use is:

sshCmd="ssh -n -o ConnectTimeout=5 -o Batchmode=yes -o StrictHostKeyChecking=no -o CheckHostIP=no -o PasswordAuthentication=no -q"

while IFS=: read -r f1 f2 f3 f4 ; do        
$sshCmd "$f1"
find "$f2" -type f -name "$f4" -mtime +"$f3"

On certain servers i receive the following error:

Pseudo-terminal will not be allocated because stdin is not a terminal. stty: standard input: Inappropriate ioctl for device

I have tried multiple options to resolve this. I have used the -t, -tt, -T options but when using these either the same error persists or the terminal becomes unresponsive.

Anyone has a solution for this?


Solution

  • You aren't running find on the remote host; you are trying to run a login shell on the remote host, and only after that exits would find run. Further, the remote shell fails because its standard input was redirected from /dev/null due to the -n option.

    sshCmd="ssh -n -o ConnectTimeout=5 -o Batchmode=yes -o StrictHostKeyChecking=no -o CheckHostIP=no -o PasswordAuthentication=no -q"
    
    while IFS=: read -r f1 f2 f3 f4 ; do   
      # Beware of values for f2, f3, and f4 containing double quotes themselves.     
      $sshCmd "$f1" "find \"$f2\" -type f -name \"$f4\" -mtime +\"$f3\""
    done
    

    Unrelated, but sshCmd should be a function, not a variable to expand.

    sshCmd () {
      ssh -n -o ConnectTimeout=5 -o Batchmode=yes -o StrictHostKeyChecking=no -o CheckHostIP=no -q "$@"
    }
    
    while IFS=: read -r f1 f2 f3 f4; do
       sshCmd "$f1" "find \"$f2\" -type f -name \"$f4\" -mtime +\"$f3\""
    done