1

I followed this tutorial for running several commands through ssh in bash script. But now I want to get the output of one of the commands that I am running through the ssh, save it in a variable and use in the next command.

 #!/bin/bash        
 ssh -t $VM_IP_ADDRESS bash -c "'

 export NET_ID=nova network-list | egrep $SUBNET_NAME | get_field 1

 nova boot $INSTANCE1_NAME --flavor m1.small --image $UBUNTU_IMAGE_NAME --key-name $KEY_PAIR_NAME --security-groups default --nic net-id=$NET_ID
'"

I want to set the NET_ID in my local environment and then use it in the next command. Now when I run this I get this error:

bash: line 17: export: `network-list': not a valid identifier

But I already checked and this command gives me the correct answer in the target machine.

nova network-list | egrep test_net | get_field 1

UPDATE:

With this command the NET_ID variable is setting in the target machine environment, but I wanted to set it in my local machin.

export NET_ID=\$(nova network-list | egrep $SUBNET_NAME | get_field 1)

1 Answer 1

1

You probably want this (a script run on your local machine):

#!/bin/bash

user=<remote-username>
ip=<remote-ip>
SUBNET_NAME=<you-know-the-value>

export NET_ID=$(ssh $user@$ip "nova network-list | egrep $SUBNET_NAME | get_field 1")
# Now, do anything with the $NET_ID on your local machine.

WARNING: the NET_ID variable will only be visible in your script and in any child process started by your script. You cannot export it to the parent environment (if that was the question).


Update: The solution that eventually worked for OP:

NET_ID=$(ssh -t $VM_IP_ADDRESS bash -c "' \
<commands>
nova network-list | egrep $SUBNET_NAME | get_field 1; \
<commands> '")

echo $NET_ID
Sign up to request clarification or add additional context in comments.

7 Comments

But if I do this it is going to try run these commands in my local machine. and I will get these errors: ./bashtest.sh: line 2: nova: command not found ./bashtest.sh: line 2: get_field: command not found
Try escaping the $ sign: export NET_ID=\$(nova network-list | egrep $SUBNET_NAME | get_field 1)
So now this is exporting it in the target machine. Is it possible to export this variable in my local machine?
There is $SUBNET_NAME in that command that should be resolve in local machine so I should use it like this ssh -t $VM_IP_ADDRESS bash -c "' NET_ID=$(ssh $VM_IP_ADDRESS -c "'nova network-list | egrep $SUBNET_NAME | get_field 1'") '" which is not possible. I got this error: Unknown cipher type ''nova network-list | egrep test_net | get_field 1''
@D3GAN: If double quotes are used instead of single quotes in the command passed to ssh, then SUBNET_NAME is resolved locally, not on your remote machine.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.