Ssh – bash function with ssh

bashremotessh

I am just getting into bash for the first time.

How would I run a function on the server in this scope? drush status is something only on the server being ssh connected too.

#!/bin/bash

function test {
    drush status
}

function connect {
    ssh user@serveraddress 'test'

}

connect

I understand you need to put the remote code in the ssh user@server 'code here', however complicated things get confused with all the '" etc. For instance this should echo all the db names of the server.

function connect {


    ssh user@serveraddress 
   '

    dbuser=user
    dbpass=pass
    DBS=`mysql -u$dbuser -p$dbpass -Bse 'show databases'| egrep -v 'information_sch$
    for db in $DBS; do
        echo "DB name -  $db"
    done

    '   
}


connect

Any help links appreciated, cheers

Best Answer

You could use here-documents:

ssh user@serveraddress <<"END"
dbuser=user
dbpass=pass
DBS=$(mysql -u$dbuser -p$dbpass -Bse 'show databases'| egrep -v 'information_sch$')
for db in $DBS; do
    echo "DB name -  $db"
done
END

See: http://tldp.org/LDP/abs/html/here-docs.html