Environnement d'éxécution de session ssh non interactive

## Table of contents
[TOC]

##Positionnement du problème
Lors de l'éxécution d'une commande sur un site distant par ssh l'environnement n'est pas positionné par l'éxécution du .bashrc

On le constate en comparant le résultats produit par l'éxécution de la commande env dans les deux contextes interactif vs non-interactif.

### cas de l'éxécution non interactive
> **ssh postgres@vmpgdeb2 env**
SHELL=/bin/bash
XDG_SESSION_COOKIE=d1972cda01f5a935fdc4e1a10000027f-1396515845.558540-1345276070
SSH_CLIENT=192.168.56.11 40585 22
USER=postgres
MAIL=/var/mail/postgres
PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
PWD=/home/postgres
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/postgres
LANGUAGE=en_US:en
LOGNAME=postgres
SSH_CONNECTION=192.168.56.11 40585 192.168.56.12 22
_=/usr/bin/env

### cas de l'exécution interactive
####Connexion
> **ssh postgres@vmpgdeb2**
Linux vmpgdeb2 2.6.32-5-amd64 #1 SMP Mon Feb 25 00:26:11 UTC 2013 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
>
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Apr  3 11:05:49 2014 from vmpgdeb1

#### commande
>postgres@vmpgdeb2:/home/postgres/ [dummy] **env**
PGDB=dummy
TERM=xterm
SHELL=/bin/bash
XDG_SESSION_COOKIE=d1972cda01f5a935fdc4e1a10000027f-1396516683.800437-2094283887
SSH_CLIENT=192.168.56.11 40588 22
SSH_TTY=/dev/pts/2
USER=postgres
PGPORT=54320
LD_LIBRARY_PATH=/u00/app/oracle/instantclient
LS_COLORS=rs=0:d........
PGUSER=postgres
PGSQLPATH=/u00/app/pg/local/dmkpg/sql:/u00/app/pg/admin/dummy/sql
PGDATABASE=postgres
PGBASE=/u00/app/pg
MAIL=/var/mail/postgres
PATH=/u00/app/pg/product/9.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
PWD=/home/postgres
PGLOCALEDIR=/u00/app/pg/product/9.1/share/locale
LANG=en_US.UTF-8
PGADM=/u00/app/pg/admin/dummy
PS1=${LOGNAME}@${HOSTNAME}:${PWD}/ [${DB}]
PGHOME=/u00/app/pg/product/9.1
SHLVL=1
HOME=/home/postgres
LANGUAGE=en_US:en
DMKPGHOME=/u00/app/pg/local/dmkpg
LOGNAME=postgres
SSH_CONNECTION=192.168.56.11 40588 192.168.56.12 22
PGDATA=/u01/pg/data/null
ORACLE_HOME=/u00/app/oracle/instantclient
_=/usr/bin/env
postgres@vmpgdeb2:/home/postgres/ [dummy]

## Problème
Le script .bashrc exécuter lors de la connexion vérifie que la session est interactive.
> \# If not running interactively, don't do anything
[ -z "$PS1" ] && return

Cela oblige à positionner manuellement dans tous les scripts utilisés de cette manière votre environnement d'éxécution.

##Solution
Elle est décrite dans ces deux liens ci-dessous.
[source-profile....on ssh login sans tty](http://apple.stackexchange.com/questions/23633/source-profile-and-bashrc-on-ssh-login-without-tty)
[setting environnemnt on remote on interactive ssh...](http://www.thinkplexx.com/learn/howto/security/ssh/setting-ssh-remote-host-environment-variables-interactive-shell)

###Résumé des actions possibles.

#### Solution 1
+ Positionner coté serveur ssh
    * la paramètre PermitUserEnvironment  à yes dans sshd_conf
    * redémarrer lde serveur ssh - service ssh restart
    * Positionner dans ~/.ssh/environment la variable BASH_ENV
    * ou comme
> dans notre cas
DMKPGHOME=/u00/app/pg/local/dmkpg

Attention le positionnement de BASH_ENV entraine l'éxécution du contenu de BAS_ENV à chaque appel de bash.

##### Résultat
> barman@vmpgdeb1:~/source$ **ssh postgres@vmpgdeb2 env**
SHELL=/bin/bash
XDG_SESSION_COOKIE=d1972cda01f5a935fdc4e1a10000027f-1396520509.980640-683673841
SSH_CLIENT=192.168.56.11 40593 22
USER=postgres
MAIL=/var/mail/postgres
PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
PWD=/home/postgres
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/postgres
LANGUAGE=en_US:en
DMKPGHOME=/u00/app/pg/local/dmkpg
LOGNAME=postgres
SSH_CONNECTION=192.168.56.11 40593 192.168.56.12 22
_=/usr/bin/env


#### Solution 2
+ Modifier .bashrc en ajoutant le code suivant:
> if [[ ! $- == *i* ]]; then
        . ~/profile
  fi

+ Ajouter dans votre fichier profile l'environnement utile.
    >  # dmkpg init
       # ~/.DMKPGHOME
       #t
       if [ -f ~/.DMKPGHOME ]; then
           . ~/.DMKPGHOME
           . $DMKPGHOME/bin/dmkpg.bash dummy
       fi
**il est pas prudent d'appeller dmkpg.bash dans ce script il y a de nombreux effet de bord possible (psql) **

##### Résultat
>**ssh postgres@vmpgdeb2 env**
PGDB=dummy
SHELL=/bin/bash
XDG_SESSION_COOKIE=d1972cda01f5a935fdc4e1a10000027f-1396519672.367280-1795997669
SSH_CLIENT=192.168.56.11 40592 22
USER=postgres
LS_COLORS=
PGPORT=54320
LD_LIBRARY_PATH=/u00/app/oracle/instantclient
PGUSER=postgres
PGSQLPATH=/u00/app/pg/local/dmkpg/sql:/u00/app/pg/admin/dummy/sql
PGDATABASE=postgres
PGBASE=/u00/app/pg
MAIL=/var/mail/postgres
PATH=/u00/app/pg/product/9.1/bin:/u00/app/pg/product/9.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
PWD=/home/postgres
PGLOCALEDIR=/u00/app/pg/product/9.1/share/locale
LANG=en_US.UTF-8
PGADM=/u00/app/pg/admin/dummy
PS1=${LOGNAME}@${HOSTNAME}:${PWD}/ [${DB}]
PGHOME=/u00/app/pg/product/9.1
SHLVL=1
HOME=/home/postgres
LANGUAGE=en_US:en
DMKPGHOME=/u00/app/pg/local/dmkpg
LOGNAME=postgres
SSH_CONNECTION=192.168.56.11 40592 192.168.56.12 22
PGDATA=/u01/pg/data/null
ORACLE_HOME=/u00/app/oracle/instantclient
_=/usr/bin/env
barman@vmpgdeb1:~/source$

##Conclusion
La solution 1 évite la gestion de multiple fichier de gestion de l'environnement.
La solution 2 dans notre cas n'èvite pas le besoin de lancer un environnement spécifique

### Mon choix :
La solution 1 , le script status ci-dessous donnant le résultat attendu.
#### script
>\#!/bin/bash
. \$DMKPGHOME/bin/dmkpg.bash
\$DMKPGHOME/bin/sta.bash
exit

#### résultat

**ssh postgres@vmpgdeb2 bash -c '"~/status.bash appone"'**

    ---------------------------------------------------------------
    appone   |  server is running
    ---------------------------------------------------------------
    psql (PostgreSQL) 9.3.3
    ---------------------------------------------------------------
                                     List of databases
       Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
    -----------+----------+----------+------------+------------+-----------------------
    postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
    template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
               |          |          |            |            | postgres=CTc/postgres
     template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
               |          |          |            |            | postgres=CTc/postgres
    (3 rows)
    ---------------------------------------------------------------



> Written with [StackEdit](https://stackedit.io/).