Following steps were performed:
^M => denotes "Enter" on keyboard. (ctrl-v Enter => ^M)
screen^M => can also be run as screen -S <session name>^M
ctrl-a c => 2 times to create two screen windows in one screen session.
create a file e.g /tmp/screen_bufferfile => this file will work as a screen buffer file.
Note : screen default buffer register is called "." ( dot )
scenario 1 : Sending Identical commands to everywhere non-interactively.
step 1 = ctrl-a : readbuf /tmp/screen_bufferfile => this will copy the contents of file in "." register
step 2 = ctrl-a : at "#" paste . => this command will paste the contents of "." register on every screen window (even to sshed windows also)
scenario 2 : Sending commands interactively.
ctrl-a : bufferfile /tmp/screen_bufferfile => sets the default buffer file
ctrl-a < => reads the contents of buffer to "." register
ctrl-a ] => paste the contents of buffer to current screen window only ( NOT any other )
even the ssh password and sudo password prompts accepts passwords like this.
scenario 3: Sending commands using screen from outside
Create a screen session and create multiple screen in it using ctrl-a c (multiple times)
Get the screen session id (or name) using screen -list
To send unix shell commands(or any input) to ALL windows in a screen session run following 2 commands:
screen -S 3461.ttys000.MacBook-Pro -X readbuf /tmp/screen_bufferfile
screen -S 3461.ttys000.MacBook-Pro -X at "#" paste "."
/tmp/screen_bufferfile will contains unix commands or (any input) you want to send to screen windows.
To send unix shell command( or any input) to a particular window run following.
screen -S 3461.ttys000.MacBook-Pro -X readbuf /tmp/screen_bufferfile
screen -S 3461.ttys000.MacBook-Pro -p 5 -X paste "."
readbuf and paste are used in scenes when we have to input some quite a good amount of data. This also relieves from
taking care of quotes and control characters that we have anyways to do when we use "stuff" command. e.g.
Above can also be done by using following:
screen -S 3461.ttys000.MacBook-Pro -p 5 -X stuff "hostname^M"
screen -S 3461.ttys000.MacBook-Pro -X at "#" stuff "hostname^M"
Above we have used actual data on command line itself to send to screen windows. Please note that we have to explicitly used ^M characters to have Enter effect. But this is not the case when we create /tmp/screen_bufferfile.
screen -S javaupdate -X screen => will spawn screen windows inside screen session (named javaupdate)
=====================================================
To create multiple windows in one screen session non-interactively
screen -d -m -S TEST => creates a screen session with name TEST and detach it. Does not go into screen session.
screen -S TEST -X screen -t yahoo 03 ssh remotemachine => creates a screen window inside TEST with name yahoo at window no 3. In window no 3 , there will an ssh session to remotemachine.