VMware Cloud Community
MattGagliardi
Enthusiast
Enthusiast
Jump to solution

Insert variable/attribute into SSH command?

Does anyone know how to insert a variable/attribute into an SSH command for execution on a remote host?

Reply
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Not directly with the stock 'Run SSH command' workflow. If you take a look at the scripting code of this workflow, you'll see that the input command string entered by the user is passed directly to the execution command.

What you can try is to duplicate this workflow and add some code to 'tweak' the input command string before passing it for execution. For example, you can think of some nice placeholder syntax for attribute names, like %attr%, which you can substitute with actual attribute name, and then pass the resulted string to eval().

So if the user enters the following command string:

  some sample %attr1% command

You need to convert it to something like the following string concatenation expression (note the apostrophes)

  'some sample ' + attr1 + ' command'

When you pass this expression to eval(), the actual value of the string attribute attr1 will be evaluated and you'll get he actual string command to run as SSH command.

View solution in original post

Reply
0 Kudos
4 Replies
suvrobhattachar
Enthusiast
Enthusiast
Jump to solution

A few ways to execute commands remotely using SSH :

In this article I describe a few ways to execute commands on a remote host using SSH. If you want to follow along, first set HOST variable to your testing server, optimaly configured with publickey authentication.

Single-line command

Executing a single command:

ssh $HOST ls 

Executing several commands, inlined, separated with ;

ssh $HOST ls; pwd; cat /path/to/remote/file 

Executing a command with sudo

ssh $HOST sudo ls /root sudo: no tty present and no askpass program specified 

sudo requires interactive shell, it can be enabled with -t parameter.

ssh -t $HOST sudo ls /root [sudo] password for zaiste: 

Simple multi-line command

VAR1="Variable 1" ssh $HOST ' ls pwd if true; then echo "True" echo $VAR1 # <-- it won't work else echo "False" fi ' 

Shell variables won't be expanded with this method.

Multi-line command with variables expansion

In order to make variables expansion work, use bash -c.

VAR1="Variable 1" ssh $HOST bash -c "' ls pwd if true; then echo $VAR1 else echo "False" fi '" 

Multi-line command from local script

A local script can be executed against remote machine with a simple stdin redirection.

cat script.sh ls pwd hostname 
ssh $HOST < script.sh 

Multi-line command using Heredoc

Using heredoc is probably the most convenient way to execute multi-line commands on a remote machine. Also, variables expansion works out-of-the-box.

VAR1="boo" ssh -T $HOST << EOSSH ls pwd if true; then echo $VAR1 else echo "False" fi EOSSH 

If you need to assign variables within the heredoc block, put the opening heredoc in single quotes.

ssh -T $HOST <<'EOSSH' VAR1=`pwd` echo $VAR1 VAR2=$(uname -a) echo $VAR2 EOSSH 

The following warning message

Pseudo-terminal will not be allocated because stdin is not a terminal. 

can be disabled by adding -T parameter to ssh execution.

MattGagliardi
Enthusiast
Enthusiast
Jump to solution

I appreciate the input (very much) but I should have been more specific.  Is there a way to take an Orchestrator attribute and use it inline in the execute SSH command workflow?  Let's say you have attribute "attr1" with some value you need to get into the SSH command you want to execute.  Is there a way to pull that off?

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Not directly with the stock 'Run SSH command' workflow. If you take a look at the scripting code of this workflow, you'll see that the input command string entered by the user is passed directly to the execution command.

What you can try is to duplicate this workflow and add some code to 'tweak' the input command string before passing it for execution. For example, you can think of some nice placeholder syntax for attribute names, like %attr%, which you can substitute with actual attribute name, and then pass the resulted string to eval().

So if the user enters the following command string:

  some sample %attr1% command

You need to convert it to something like the following string concatenation expression (note the apostrophes)

  'some sample ' + attr1 + ' command'

When you pass this expression to eval(), the actual value of the string attribute attr1 will be evaluated and you'll get he actual string command to run as SSH command.

Reply
0 Kudos
MattGagliardi
Enthusiast
Enthusiast
Jump to solution

Thanks! Got it done Smiley Happy

Reply
0 Kudos