PROJET AUTOBLOG


Planet-Libre

source: Planet-Libre

⇐ retour index

bazzanella : Ecrire vers un fichier distant

mercredi 8 mai 2013 à 23:01

Vous souhaitez écrire le contenu d’une variable contenant des double-quotes dans un fichier distant.

Vous avez une variable contenant des doubles quotes (« ) :

chaine="Bonjour \\"$LOGNAME\\" et bienvenue"
  1. chaine="Bonjour \\"$LOGNAME\\" et bienvenue"

le résultat d’affichage étant :
Bonjour "martin" et bienvenue

Vous souhaitez donc conserver les doubles quotes entourant votre login martin et écrire ce résultat dans un fichier distant :

ssh -p 22 -i /home/user1/.ssh/id_dsa user1@remoteserver "echo '$chaine' > /home/user1/file1.txt"
  1. ssh -p 22 -i /home/user1/.ssh/id_dsa user1@remoteserver "echo '$chaine' > /home/user1/file1.txt"

ou plus pratiquement en écrivant une fonction :

function write_var_to_remotefile () {
local var_param="${1}"
local remotepathfile_param="${2}"

if [ -z ${var_param} ]; then
  echo "write_var_to_remotefile : Argument 1 non fourni"
  exit 1;
fi

if [ -z ${remotepathfile_param} ]; then
  echo "write_var_to_remotefile : Argument 2 non fourni"
  exit 1;
fi

if [ -z ${PORT} ]; then
  echo "write_var_to_remotefile : Global variable PORT non fourni"
  exit 1;
fi

if [ -z ${PATHPRIVKEY} ]; then
  echo "write_var_to_remotefile : Global variable PATHPRIVKEY non fourni"
  exit 1;
fi

if [ -z ${REMOTESERVER} ]; then
  echo "write_var_to_remotefile : Global variable REMOTESERVER non fourni"
  exit 1;
fi

if [ -z ${REMOTEUSER} ]; then
  echo "write_var_to_remotefile : Global variable REMOTEUSER non fourni"
  exit 1;
fi

/usr/bin/ssh -p ${PORT} -i ${PATHPRIVKEY} ${REMOTEUSER}@${REMOTESERVER} "echo '${var_param}' > ${remotepathfile_param}"
}
  1. function write_var_to_remotefile () {
  2. local var_param="${1}"
  3. local remotepathfile_param="${2}"
  4.  
  5. if [ -z ${var_param} ]; then
  6.   echo "write_var_to_remotefile : Argument 1 non fourni"
  7.   exit 1;
  8. fi
  9.  
  10. if [ -z ${remotepathfile_param} ]; then
  11.   echo "write_var_to_remotefile : Argument 2 non fourni"
  12.   exit 1;
  13. fi
  14.  
  15. if [ -z ${PORT} ]; then
  16.   echo "write_var_to_remotefile : Global variable PORT non fourni"
  17.   exit 1;
  18. fi
  19.  
  20. if [ -z ${PATHPRIVKEY} ]; then
  21.   echo "write_var_to_remotefile : Global variable PATHPRIVKEY non fourni"
  22.   exit 1;
  23. fi
  24.  
  25. if [ -z ${REMOTESERVER} ]; then
  26.   echo "write_var_to_remotefile : Global variable REMOTESERVER non fourni"
  27.   exit 1;
  28. fi
  29.  
  30. if [ -z ${REMOTEUSER} ]; then
  31.   echo "write_var_to_remotefile : Global variable REMOTEUSER non fourni"
  32.   exit 1;
  33. fi
  34.  
  35. /usr/bin/ssh -p ${PORT} -i ${PATHPRIVKEY} ${REMOTEUSER}@${REMOTESERVER} "echo '${var_param}' > ${remotepathfile_param}"
  36. }

Gravatar de bazzanella
Original post of bazzanella.Votez pour ce billet sur Planet Libre.

Articles similaires