0

below is my use case , I have to take a input from a user for a specific command which need to run on a remote machine.But my problem is I am not able to access that machine directly so what I am doing

Ssh to othere server and from their I am doing ssh to that server. But at that server we are not able to take any inout from user.

Below is a sample script:-

ssh user@machine1 /bin/bash <<\EOF1

ssh user@machine2 /bin/bash <<\EOF2

pwd
echo " Enter input  :"

read -r input

curl -XPOST -k -H "Content-type: application/json" -d '{ "LOGIN_URL": "https://xyz/login","LOGIN_USER": "user11@gmail.com", "LOGIN_PASSWORD": "xyz", "CID": "'"$input"'" }' 'https://abc/gettoken' > sample

scp sample.txt root@user1:/root

It does not ask for user input and dirctly jump to scp command. Also is this a correct way to pass a variable in a curl command

Any help will be appreciated!

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
Mohan
  • 1

1 Answers1

0

Your shell variable in a JSON document in a curl command in a here document is not easy to work with. Super annoying to get quoting correct. Especially when this needs to be done to multiple hosts.

Consider writing short shell scripts. A wrapper script on localhost, that prompts the user, possibly with stty -echo to hide sensitive values. The wrapper script then sftp's additional scripts and maybe JSON files to the remote servers, and runs them. Consider the use of JSON processors like jq to edit CID, rather than rely on shell variable expansion.

Or, some automation tools can help with running commands on remote hosts. For example, an Ansible play could make use of vars_prompt keyword for the password prompt, uri module to do the POST, and save the response.

John Mahowald
  • 30,009
  • 1
  • 17
  • 32