1

I'm writing a simple bash server health check that runs on a local machine and asks the user which server they are interested in looking at. When provided with the name, the script runs a set of health check commands on that server and returns the output to the user. Currently, the script will just log the user into the server but won't run the health check until the user exits that ssh session, then it runs those checks locally, not on the remote server as intended. I don't want the user to actually log on to the server, I just need the output from that server forwarded to the users local console. Any clue what I'm doing wrong here? Thanks in advance!

#!/bin/bash
echo -n "Hello "$USER""
echo "which server would you like to review?"
read var1
ssh -tt $var1
echo ">>> SYSTEM PERFORMANCE <<<"
top -b -n1 | head -n 10
echo ">>> STORAGE STATISTICS <<<"
df -h
echo ">>> USERS CURRENTLY LOGGED IN <<<"
w
echo ">>> USERS PREVIOUSLY LOGGED IN <<<"
lastlog -b 0 -t 100
AsherV
  • 37
  • 1
  • 1
  • 8

2 Answers2

2

Using a here-doc :

#!/bin/bash
echo -n "Hello "$USER""
echo "which server would you like to review?"
read var1
ssh -t $var1<<'EOF'
echo ">>> SYSTEM PERFORMANCE <<<"
top -b -n1 | head -n 10
echo ">>> STORAGE STATISTICS <<<"
df -h
echo ">>> USERS CURRENTLY LOGGED IN <<<"
w
echo ">>> USERS PREVIOUSLY LOGGED IN <<<"
lastlog -b 0 -t 100
EOF
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • I think we're definitely on the right track here. However, now, the output is also showing the raw commands. When I run the script locally without EOF, it's displaying clean with no commands shown. Any ideas for a work around? Much appreciated. – AsherV Apr 02 '18 at 21:18
  • Removed a `t` from `-tt` (play around this is not sufficient) – Gilles Quénot Apr 02 '18 at 21:21
  • 1
    That did the trick! Thank you Gilles! – AsherV Apr 02 '18 at 21:47
0

There is a tool that is used extensively for all classes of remote access (via ssh and/or telnet, http, etc) that is called expect(1) It allows you to send commands and wait for the responses, allowing even use of interactive commands (like vi(1) in screen mode) or even to supply passwords over the line. Do some googling on it and you'll see how useful it is.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31