Wednesday, 27 July 2016

Using screen to run commands on multiple servers.

In a previous tutorial I talked about how screen can be used to share a terminal display remotely.
In this tutorial we'll be seeing how screen can be used to run commands across multiple servers.

First make sure screen is installed on the system. If not install it via yum.

Next create the first screen by running screen command.


You'll see the word "screen 0" above the terminal window.

Now create a second screen by typing the key sequence ctrl+a c.


You'll see the word "screen 1" above the terminal window.

Now, while in screen 1 ssh to another server.


I had already set up passwordless ssh so there was no password prompt.

So, we have two screens now. To move between the screens use the key combinations ctrl+a n & ctrl+a p to go the next & previous screens respectively.

If we have multiple screens open we can return to the original screen by typing the key sequence ctrl+a ".
This will give us a list of open screens. From the list select 0 which is our original screen.


From our original screen i.e. screen 0 we'll now launch a command which we want to be run on all screens.
To do this first type the key combination ctrl+a :.
This will give us a prompt. On the prompt type the following sequence:

at "#" stuff "uname -a^M"



Here's a breakdown of what we just did:

at means on the screen
# specifies that we want to run the command on all screens. If we want to run a command on a specific screen we can just type the screen number.
stuff means to stuff the screen buffer with the command or sequence of commands that follow.
^M is equivalent to the user pressing enter key after typing the command.

To close the screens type exit on the command prompt on the screen you want to close.
You can verify if any screens are open with screen -ls command.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...