Jump to content

Bash and heredoc


Drongo_III

Recommended Posts

Hello

 

I'm really green when it comes to bash...

 

Trying to create a script which will SSH into multiple servers and report back a list showing the status of a deployment on each.  The intention is for the script to run from my local machine.

 

I read using heredoc is the way to go but I don't think its possible to store variables which are accessible outside of the sub-session - i.e. you can't store variables inside heredoc and have them accessible outside

 

So my question is does anyone know a good way to get the variables from heredoc into the 'higher level' bash script so I can aggregate the results from each server?

 

Pseudo code to illustrate what I'm aiming for:


ssh me@11.11.11.11 << EOF
# Get server Status to variable
EOF

ssh me@22.22.22.22 << EOF
# Get Server Status to variable
EOF

echo $SOME_VARIABLE_OF_STATUS_FROM_EACH_SERVER
Link to comment
Share on other sites

Generally speaking you just capture the output of the command and store that into a variable. That would look something like this:

STATUS=$(ssh you@example.com whatever-command)
So if you wanted to get the disk usage for various servers you might do:

SERVER1=$(ssh you@server1.example.com df -h)
SERVER2=$(ssh you@server2.example.com df -h)
SERVER3=$(ssh you@server3.example.com df -h)

echo Usage for server 1:
echo $SERVER1;
echo Usage for server 2:
echo $SERVER2;
echo Usage for server 3:
echo $SERVER3;
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.