1

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?

Kevin
  • 143
  • 1
  • 8
  • Consider using Ansible or Puppet Bolt for tasks like this. – brunorey Mar 22 '19 at 12:45
  • @brunorey that is no option atm – Kevin Mar 22 '19 at 12:46
  • 1
    `sshCmd` should be a function, not a variable. – chepner Mar 22 '19 at 13:00
  • Is the `find` command supposed to run on the remote host? As shown, you are trying to start an interactive shell (the default command). – chepner Mar 22 '19 at 13:00
  • @chepner that sshCmd is not a function will not resolve the issue. I want to perform the find on the remote machine. Ssh'ing into a remote machine would be useless if the find would need to perform locally. – Kevin Mar 22 '19 at 13:05
  • 1
    I never said it would resolve the issue; never the less, it *should* be a function. See [Bash FAQ 050](https://mywiki.wooledge.org/BashFAQ/050). And since `find` has to be run on the remote machine, you need to specify it as an actual argument *to* `ssh`, not written as a separate command on the following line. – chepner Mar 22 '19 at 13:10

2 Answers2

1

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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • This work with 1 exception. When i ssh to an SunOS machine i still get the following error: Pseudo-terminal will not be allocated because stdin is not a terminal. Oracle Corporation SunOS 5.10 Generic Patch January 2005 When i leave the -n option i can ssh but it will break the function for the while loop. Any suggestion? @chepner – Kevin Mar 29 '19 at 17:05
  • What happens if you use `-T` is explicitly disable pseudo-terminal allocation? – chepner Mar 29 '19 at 17:17
  • Seems like the input file was not correct for that specific server. So the original solutions still applies and works. My bad. – Kevin Mar 29 '19 at 17:55
0

Minor addition: save yourself some backslash grief:

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

Everything inside the double quotes will be expanded prior to command processing, so the values will be expanded, and sitting inside single quotes, for the remote host to handle; i.e. the remote host will see the values, and not the variable names.

(Standard disclaimer applies regarding the possibility of f1, f2, f3, f4 containing single quotes...)

Greywolf
  • 11
  • 3