2

I have a bash script that will execute a script of my choosing against a server over ssh. My problem is that I also want to use an input file with common variables so I don't have to change them in each script. So far my attempts at getting it to source the two files have resulted in it trying to find one of them on the remote machine.

input

JBLGSMR002,IP.IP.IP.IP,root,pers,pers

sourcelist

#!/bin/bash
var1="Some stuff"
var2="Some stuff 2"

Script

#!/bin/bash
#
#set -x
input="/home/jbutryn/Documents/scripts/shell/input/nodelist.csv"
sourcelist="/home/jbutryn/Documents/scripts/shell/Tools/slist"
tools="/home/jbutryn/Documents/scripts/shell/Tools"
#
is.there () {
        if grep -wF $1 $2 > /dev/null 2>&1 ; then
                echo "true"
        else
                echo "false"
        fi
}
#
nodethere=$(is.there $1 $input)
#
if [[ $nodethere = "true" ]]; then
        ipconn=$(awk -F ',' '/'"$1"'/ {print $2}' $input)
        usrconn=$(awk -F ',' '/'"$1"'/ {print $3}' $input)
elif [[ $nodethere = "false" ]]; then
        echo "Couldn't find $1 in database"
        exit 1
fi
#
if [[ -f $tools/$2 ]]; then
        echo "Please enter your password for $1: "
        read -s SSHPASS
        eval "export SSHPASS='""$SSHPASS""'"
        sshpass -e ssh $usrconn@$ipconn <  "$tools/$2"
elif [[ ! -f $tools/$2 ]]; then
        echo "Couldn't find $2 script in the Tools"
        exit 1
fi

I have this test script to see if it's passing the variables to the remote machine:

Test Script

#!/bin/bash
#
touch testlog
echo $var1 >> ./testlog
echo $var2 >> ./testlog

And this is what I've tried so far to get the sourcelist to pass through:

if [[ -f $tools/$2 ]]; then
        echo "Please enter your password for $1: "
        read -s SSHPASS
        eval "export SSHPASS='""$SSHPASS""'"
        sshpass -e ssh $usrconn@$ipconn < "$sourcelist"; "$tools/$2"

This one will create a blank testlog file on the local machine

if [[ -f $tools/$2 ]]; then
    echo "Please enter your password for $1: " 
    read -s SSHPASS
    eval "export SSHPASS='""$SSHPASS""'"
    sshpass -e ssh $usrconn@$ipconn <'EOF'
    source $sourcelist
    bash "$tools/$2"
    logout
    EOF

This one will create a blank "testlog" file on the local machine

I've also tried using source bash . to call the files but I still can't seem to get both local files to pass to the remote machine. Anyone know how this can be done?

  • 1
    It's a bit difficult to understand what you're really trying to do, but perhaps this is what you're looking for: cat "$sourcelist" "$tools/$2" | sshpass -e ssh $usrconn@$ipconn – janos Jul 15 '17 at 10:18
  • That works, make it an answer so I can give you the bounty. Thanks. – Jesse_b Jul 15 '17 at 14:08
  • Done, glad it helped – janos Jul 15 '17 at 14:45
3
+50

It's a bit difficult to understand what you're really trying to do. If you want to concatenate the content of $sourcelist and $tools/$2 and execute it in Bash, you can use cat with those two files and pipe to ssh like this:

cat "$sourcelist" "$tools/$2" | sshpass -e ssh $usrconn@$ipconn 
  • The end goal is to execute some common administrative scripts against servers on the infrastructure as needed. The sourcelist will contain common variables, functions, etc. The second script is whatever script needs to be run, which usually will just be something that gathers information and sends it off via email when an alert is received. – Jesse_b Jul 28 '17 at 0:50
0

Just use scp to copy the needed files over, source them there and delete them when done?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.