Drongo_III Posted August 8, 2017 Share Posted August 8, 2017 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 [email protected] << EOF # Get server Status to variable EOF ssh [email protected] << EOF # Get Server Status to variable EOF echo $SOME_VARIABLE_OF_STATUS_FROM_EACH_SERVER Quote Link to comment https://forums.phpfreaks.com/topic/304556-bash-and-heredoc/ Share on other sites More sharing options...
requinix Posted August 8, 2017 Share Posted August 8, 2017 What is the command you want to execute on the remote servers? Quote Link to comment https://forums.phpfreaks.com/topic/304556-bash-and-heredoc/#findComment-1549480 Share on other sites More sharing options...
kicken Posted August 8, 2017 Share Posted August 8, 2017 Generally speaking you just capture the output of the command and store that into a variable. That would look something like this: STATUS=$(ssh [email protected] whatever-command) So if you wanted to get the disk usage for various servers you might do: SERVER1=$(ssh [email protected] df -h) SERVER2=$(ssh [email protected] df -h) SERVER3=$(ssh [email protected] df -h) echo Usage for server 1: echo $SERVER1; echo Usage for server 2: echo $SERVER2; echo Usage for server 3: echo $SERVER3; Quote Link to comment https://forums.phpfreaks.com/topic/304556-bash-and-heredoc/#findComment-1549487 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.