You are not logged in.

#1 2012-05-10 19:08:22

thelastknowngod
Member
Registered: 2008-01-11
Posts: 110

script for starting screen

I need to create a script that works like this:

./script_name IP.ADDRESS

When running that, the script needs to open a screen session with two active window regions. The top region would run a script against that address. The bottom region would open an SSH connection to the server. The bottom region would need to have the focus by default.

Here is the script I have so far but it is not working.

    #!/bin/bash
     
    function startscreen () {
            # Start the screen session in detached mode.
            screen -d -m -S $1;
            # Start SSH in window 1.
            screen -S $1 -X screen -t $1 1 ssh -o StrictHostKeyChecking=no $1;
            # Start dex.sh in window 2.
            screen -S $1 -X screen -t dex 2 /export/home/tlkg/scripts/dex.sh $1;
    }
     
    # Format the layout. This is to be applied after resuming the session.
    function formatscreen () {
            # Remove window that was created by default.
            screen -S $1 -X select 0;
            screen -S $1 -X kill;
            # Create two regions in the terminal.
            screen -S $1 -X split;
            # Place window 2 (dex.sh) in the top region.
            screen -S $1 -X focus top;
            screen -S $1 -X select 2;
            # Place window 1 (SSH) in the bottom region.
            screen -S $1 -X focus down;
            screen -S $1 -X select 1;
    }
     
    # Check to see if there is already a screen session. Prompt the user if there is, create one if there isn't.
    if [ -a /tmp/uscreens/S-"$USER"/*."$1" ]; then
    while :
     do
        echo
        echo "There is already a session open for that server. How would you like to proceed?"
        echo
        echo "[1] Connect to the existing session."
        echo "[2] Start additional session."
        echo
        echo "[q] Quit"
        echo "======================="
        echo -n "Enter your menu choice [1-2]: "
        read yourch
        case $yourch in
          # Disconnect from the active session and reconnect. This ensures you connect/disconnect properly.
          1) screen -d -r $1 && exit 0 ;;
          # Start the session in detatched mode, create appropriate windows and launch applications, attach to the screen session
          2) startscreen && screen -r $1 && formatscreen && exit 0 ;;
          q) exit 0 ;;
          *) echo "That is not a valid option."; read ;;
        esac
     done
    else
            startscreen && screen -r $1 && formatscreen && exit 0
    fi

You guys have any suggestions?

Last edited by thelastknowngod (2012-05-10 19:08:54)

Offline

#2 2012-05-10 19:22:50

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: script for starting screen

Have you considered using a custom screenrc rather than a script?  Basically, I have two special rcs that I use with aliases:

alias screen2='screen -c ~/.screen2'
alias screen4='screen -c ~/.screen4'

My only needs are getting a particular layout (two or four subscreens), but I'm pretty sure the method would suit your needs too.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#3 2012-05-10 19:30:16

thelastknowngod
Member
Registered: 2008-01-11
Posts: 110

Re: script for starting screen

I would love to be able to do that but this script will be run from a shared jump box. There are about a hundred different people using this machine and it would be a mess to tell them each to place those options in .screenrc (or modify their existing file). I know it would be infinitely easier but, unfortunately, it needs to be contained in this script.

Offline

#4 2012-05-10 19:37:40

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: script for starting screen

If the script is located on shared storage, couldn't the custom rc be there instead?  Or in addition: you could just have your script run "screen -c /path/to/customrc-on-shared-storage" to make things a complete no brainer for everyone.

Or maybe I just don't understand what's going on at all. tongue


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#5 2012-05-10 21:01:52

thelastknowngod
Member
Registered: 2008-01-11
Posts: 110

Re: script for starting screen

How would you do using the .screenrc file?

Offline

#6 2012-05-11 14:03:23

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: script for starting screen

For example, here's the relevant bit from my ".screen2" rc file:

split -v
screen
focus down
screen
focus down

This results in two vertically split 'subscreens', with focus on the right one.  Much simpler than what you need to do, I know, but I'm underusing the capability.  From what I understand, the main thing you would need is to augment the calls for "screen" with your arguments.  You'd still need your script to get the session check functionality though (I didn't see that before).


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

Board footer

Powered by FluxBB