sshpass 我想在.sh 中使用并执行一些命令 Ubuntu 16.04

sshpass 我想在.sh 中使用并执行一些命令 Ubuntu 16.04
  • 我想要从一个 VM-X 登录到不同的 VM-Y,并从 VM-Y 获取输出 dh -H 命令

  • 并找出哪个磁盘使用率超过 50% 并发送电子邮件

来自 VM-X

echo "df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 \" \" \$1 }'" | sshpass -p 'password' ssh user@VM-Y-IpAddress 

并且我能够得到输出

Pseudo-terminal will not be allocated because stdin is not a terminal. Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-62-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

122 packages can be updated. 0 updates are security updates.


*** System restart required ***  udev  /dev/mapper/servername--vg-root  /dev/sda1

但当我将其放入 ganetichexdiskspace.sh 中时却无法获取它

#!/bin/sh
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free avilable) percentage
# of space is >= 90%
# set admin email so that you can get email

ADMIN="[email protected]"


# set alert level 90% is default
ALERT=50
OUTPUT="$(df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 \" \" \$1 }' \" | sshpass -p 'password' ssh user@VM-Y-IpAddress)"

df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 \" \" \$1 }'" | sshpass -p 'password' ssh user@VM-Y-IpAddress | while read output;
do
  #echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
     mail -s "Alert: Almost out of disk space $usep" $ADMIN
  fi
done

有什么帮助吗

相关内容