Is it possible to write a shell script that takes input then will ssh into a server and run scripts on my local machine?

NTWorthy :

I have several scripts on my local machine. These scripts run install and configuration commands to setup my Elasticsearch nodes. I have 15 nodes coming and we definitely do not want to do that by hand.

For now, let's call them Script_A, Script_B, Script_C and Script_D.

Script_A will be the one to initiate the procces, it currently contains:

#!/bin/bash

read -p "Enter the hostname of the remote machine: " hostname
echo "Now connecting to $hostname!"

ssh root@$hostname

This works fine obviously and I can get into any server I need to. My confusion is running the other scripts remotely. I have read few other articles/SO questions but I'm just not understanding the methodology.

I will have a directory on my machine as follows:

Elasticsearch_Installation
|
|=> Scripts
    |
    |=> Script_A, Script_B, etc..

Can I run the Script_A, which remotes into the server, then come back to my local and run Script_B and so on within the remote server without moving the files over?

Please let me know if any of this needs to be clarified, I'm fairly new to the Linux environment in general.. much less running remote installs from scripts over the network.

John Bollinger :

You have a larger problem than just setting up many nodes: you have to be concerned with ongoing maintenance and administration of all those nodes, too. This is the space in which configuration management systems such as Puppet, Ansible, and others operate. But these have a learning curve to overcome, and they require some infrastructure of their own. You would probably benefit from one of them in the medium-to-long term, but if your new nodes are coming next week(ish) then you probably want a solution that you can use immediately to get up and going.

Certainly you can ssh into the server to run commands there, including non-interactively.

My confusion is running the other scripts remotely.

Of course, if you want to run your own scripts on the remote machine then they have to be present there, first. But this is not a major problem, for if you have ssh then you also have scp, the secure copy program. It can copy files to the remote machine, something like this:

#!/bin/bash

read -p "Enter the hostname of the remote machine: " hostname
echo "Now connecting to $hostname!"

scp Script_[ABCD] root@${hostname}:./
ssh root@hostname ./Script_A

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33010&siteId=1