pass variable in ssh

I'm trying to pass a variable to ssh remote but not works. My code is:

#!/bin/bash set -x conexion="[email protected]" parameter="$1" ssh -T $conexion <<'ENDSSH'  clear echo "$parameter" ENDSSH 

I execute:

./script.sh try 

It says me:

parameter: Undefined variable. 

any help please?

Replay

Passing variables (environment variables) over ssh is possible but generally restricted.

You need to tell the client to send them. For instance with OpenSSH, that's with:

ssh -o SendEnv=parameter host cmd...

But you also need the server the accept it (AcceptEnv configuration directive with OpenSSH). Accepting any variable is a big security risk so is generally not done by default, though some ssh deployments allow some variables under some namespace (like LC_* in some OpenSSH deployments).

You also need to export the variable before calling ssh, like:

LC_parameter="$parameter" ssh -o SendEnv=LC_parameter host csh << 'END'
echo $LC_parameter:q
END

Above, we're passing the content of the $parameter bash shell variable as the LC_parameter environment variable to ssh. ssh sends that over to sshd, which if it accepts it, passes it as an environment variable to the login shell of the user which then passes it to that csh command (which can then expand it).

But as mentioned earlier, that won't work unless the administrator of the host machine has added a AcceptEnv LC_parameter or AcceptEnv LC_* (that one sometimes done by default) to the sshd configuration.

The Undefined variable error message in your example suggests the login shell of the remote user is csh or tcsh. It's better to explicitly invoke the shell to avoid surprises (ssh host csh also means a tty is not requested so you don't need -T). Note the $LC_parameter:q syntax, which is the csh way to pass the content of a variable verbatim, not "$LC_parameter" which doesn't work if the variable contains newline characters.

If using LC_* variables is not an option, then alternatively, you can have the client shell (bash in your case) expand the variable. A naive way would be with

ssh host csh << END
echo "$variable"
END

But that would be dangerous as the content of the variable would be interpreted by the remote shell. If $variable contains `reboot` or "; reboot; : " for instance, that would have bad consequences.

So, you'd need first to make sure the variable is properly quoted in the syntax of the remote shell. Here, I would avoid csh where it's hard to do reliably and use sh/bash/ksh instead.

Use a helper function to do the sh quoting:

shquote() {
  awk -v q=\' -v b='\\' '
    BEGIN{
      for (i=1; i<ARGC; i++) {
        gsub(q, q b q q, ARGV[i])
       printf "%s ", q ARGV[i] q
      }
      print ""
      exit
    }' "[email protected]"
}

And call ssh as:

ssh host sh << END
parameter=$(shquote "$parameter")
echo "\$parameter"
END

See how we escape the third $ so the expansion of $parameter is done by the remote shell, not the local one.

Removing quotes from ENDSSH on the 4th line should help.

Courtesy: @Archemar in comments to the question.

WARNING As it was stated by @St├ęphaneChazelas in the comment to this answer, this sollution would cause the $parameter variable in the here doc to be expanded by the local shell, which means the content of the variable would be interpreted as shell code by the remote shell, which means it introduces a command injection vulnerability. So generally it's discouraged to do so.

Category: ssh Time: 2016-07-29 Views: 0
Tags: ssh

Related post

  • Passing variables into ssh sessions (Session has to stay alive) 2016-06-24

    is it possible to start a ssh remote session and pass a variable to the remote host, which is usable in the opened session. For Example: Client: testvar="this is my text" Open ssh session (ssh [email protected]) and pass $Testvar to the remote Ses

  • Unable to get profile variable using ssh 2013-03-15

    Unable to retrieve the profile variable using ssh. server2 ==> user apptst ==> bash profile has $APPHOME variable. Was trying to retrieve the value from server1 using ssh but failed. Used options below server1> ssh [email protected] 'echo $APPHOME'

  • How to pass variables from one vm to another vm using bash script 2016-06-30

    What is the proper way to pass variable from one machine to another machine via bash script? In the below code I am trying to get the input from user and want to pass the same to remote machine, but the value is not getting accessed in remote machine

  • Passing variables through locate_template 2010-11-23

    While I've typically used include or require on their own to save long term code maintenance I've started to use get_template_part and locate_template as using built in WordPress stuff is always best. My question is are you supposed to be able to pas

  • In bash, what is the safest way to pass variables to another program - $* or [email protected]? 2011-03-18

    This question already has an answer here: What is the difference between $* and [email protected]? 7 answers Should I use $* or [email protected]? [duplicate] Can someone explain to me in a concise way what the difference between these two vars is? What is the safest way to pass

  • Can I pass variables from more specific template/preprocess function up to less specific? 2011-06-29

    I need to display a node as a block, so I went with the most obvious way and created a view with a display block and this very node in it. But now I need to style it and I can't pick up the proper template. block-views.tpl.php looked great, but being

  • Pass variable to hook. Its possible? 2011-09-14

    I have this hook created to retrieve user password when he register or update his pass. I dont want my client to touch wordpress core so i was planning to use a hook in wp-includes/user.php All this come from this other post as i cant find any other

  • How to pass variable from jade template file to a script file? 2012-01-02

    I'm having trouble with a variable (config) declared in a jade template file (index.jade) that isn't passed to a javascript file, which then makes my javascript crash. Here is the file (views/index.jade): h1 #{title} script(src='./socket.io/socket.io

  • How to pass variable to a view after submitting? 2012-06-21

    Why does the below code not work? I am trying to pass the $_POST value to example_page() when I submit the form: function example_menu () { $items['example'] = array( 'title' => 'Example', 'page callback' => 'drupal_get_form', 'page_arguments' =>

  • how to pass variables from one module to another module? 2012-08-06

    I wanted to use apachesolr module along with a field module that is custom written. The field module gives the input to 'apachesolr' module instead of the normal search bar. The output is passed through the field module. The output is printed as the

  • Transferring environment variable through SSH / quoting in bash/sh/csh/tcsh 2012-10-02

    I want to transfer an environment variable over SSH. The "correct" way is using SendEnv/~/.ssh/environment, but that requires the server to support AcceptEnv or PermitUserEnvironment, which it does not in my case. So instead I am thinking to set

  • Cannot pass variable to js file 2012-10-22

    I cannot pass variable to js file, from my common.module $max_length = 10; //variable_get('title_max_length'); drupal_add_js(drupal_get_path('theme', 'myTHEME'). '/js/script.js'); drupal_add_js(array('common' => array('max' => $max_length)), array('

  • How to pass variables between theme functions 2012-12-24

    I often use functions that contain variables in the following files: Template.php Custom Panels Style Plugins node.tpl.php files field.tpl.php files views.tpl.php files I often needed to pass variables between these functions. At the moment, I do thi

  • Passing variables to Joined Reports 2013-02-20

    Is it possible to pass variables to Joined Reports (as in pv0=) and/or is it possible to set the Time Frame selector for each report in a Joined Report? The problem I am trying to solve is having one Joined Report (with 4 blocks) serve to provide mul

  • how to pass variables from a views style-template to the row-template it invokes 2013-04-03

    I created a custom style-plugin for views, and a corresponding style-template (my-style-template.tpl.php). Similarly, I created a custom row-plugin for views and a corresponding row-template. The style-template loops through the views-rows, and invok

  • Passing variable from filter 2013-07-09

    I have this filter: function wpa_filter_nav_menu_objects( $items){ global $count4footer; foreach( $items as $item ){ $count = countPosts($item->ID); if($count!==false){ $item->title = $item->title." ($count)"; $count4footer[$item->ID

  • Pass variable from function to template 2013-07-26

    I want to pass a variable from a mymodule_preprocess_node(&$vars) to my template file. Something as simple as the following: function mymodule_preprocess_node(&$vars) { $vars['node_type'] = 'some_type'; } I trying to pass this variable to a zone--

  • How to pass variable from module to other theme 2013-08-23

    I would like to pass variables from my custom module to some of my custom themes. The most tutorials are concerning using hook_theme function but as I understand, it works only for creating new theme functions (new tpl files) inside module, where the

  • Passing variables through permalink structure in custom post types 2013-10-30

    I'm having a real headache with this one, and not sure what to try. Basically, I'm building a website with a custom post type of 'charities'. There are many charities listed, we'll use RSPB as an example. SO visiting "example.com/charities/rspb"

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.425 (s). 13 q(s)