Does anyone know how to insert a variable/attribute into an SSH command for execution on a remote host?
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.
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.
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:
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.
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 '"
A local script can be executed against remote machine with a simple stdin
redirection.
cat script.sh ls pwd hostname
ssh $HOST < script.sh
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.
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?
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.
Thanks! Got it done