LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-21-2012, 12:06 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 285

Rep: Reputation: 16
tmux bash script create new windows


Code:
#!/bin/bash
sn=test_windows_session_2
tmux new-session -s "$sn" -d

for i in {1..10};  do
        tmux new-window -t "$sn:$i"
done

for i in {1..10} ; do
        for k in carcasper caspats cashpats casblob cashblob cascore cashcore casrash cashrash
        do
        tmux send-keys -t "$sn:$i" "ssh -l casper $k"
        done
sleep 1
done
It creates the windows real good

Code:
                                                                        0: bash  [218x132] (1 panes)
  1: bash  [218x132] (1 panes)
  2: bash  [218x132] (1 panes)
  3: bash  [218x132] (1 panes)
  4: bash  [218x132] (1 panes)
  5: bash  [218x132] (1 panes)
  6: bash  [218x132] (1 panes)
  7: bash  [218x132] (1 panes)
  8: bash  [218x132] (1 panes)
  9: bash- [218x132] (1 panes)
  10: bash* [218x132] (1 panes)
but this was in each pane

Code:
casper@casper:~/script_tmux $ ssh -l casper carcasperssh -l casper caspatsssh -l casper cashpatsssh -l casper casblobssh -l casper cashblobssh -l casper cascoressh -l casper cashcoressh -l casper casrashssh -l casper cashrashbjoh cashpatsssh -l casper casblobssh -l casper cashblobssh -l casper carcas

All I need is a loop that will send keys to each window that i create
Code:
tmux send-keys -t "name_of_session:<increment by one>" "ssh -l waljoh <the_host_to_ssh_to>"
next host
 
Old 07-22-2012, 02:50 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 576

Rep: Reputation: 318Reputation: 318Reputation: 318Reputation: 318
Hi.

What are you trying to achieve? It looks like you want to run few interactive ssh sessions in a row in each pane. How is this supposed to work? Anyway, the only way to execute shell command in a specific pane I managed to find is either to use `shell-command' option to `new-window' or set `default-command':
Code:
#!/bin/bash
sn=test_windows_session_2
tmux new-session -s "$sn" -d
tmux set-option -s default-command 'for k in carcasper caspats cashpats casblob; do PS1="$k: " sh; done'

for i in {1..3};  do
        tmux new-window -t "$sn:$i"
done
I use `sh' instead of `ssh' in this example and set prompt to the name of the host we are supposed to connect to. Type Control-D (or `exit') multiple times and see what happens. Is that what you want?

Last edited by firstfire; 07-22-2012 at 02:53 AM.
 
Old 07-22-2012, 09:18 PM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 285

Original Poster
Rep: Reputation: 16
I was thinking of this on the way home - I need an array.
the number of $i is going to corelate to a tmux window and a separate host - so when

I attach to the session, each windows is going to be ssh'd to a separate host 9one
of the hosts in the array)
i will try thi sout tomorrow when I get to work.
Code:
#!/bin/bash
sn=test_windows_session_4
tmux new-session -s "$sn" -d

for i in {1..10};  do
        tmux new-window -t "$sn:$i"
done
declare -a hosts=('casperutil' 'casperpats' 'casperpats' 'casperblob' 'casperblob' 'caspercore' 
'caspercore' 'casperrash' 'casperrash')

for i in {1..10} ; do
        tmux send-keys -t "$sn:$i" "ssh -l casper ${hosts[$i]}"
        sleep 5
done
if you don't use tmux - it is like linux screen, but scriptable

Last edited by casperdaghost; 07-22-2012 at 09:19 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to enter commands in a Tmux window Southpaw94 Linux - Software 2 03-26-2012 04:17 PM
[SOLVED] bash aliases lost within tmux sycamorex Linux - General 3 07-08-2011 07:53 PM
Help me to create a bash script Mr. Alex Linux - Newbie 5 06-14-2011 12:51 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
How to create a bash script to count? Alinuxnoob Programming 4 01-28-2007 10:48 PM


All times are GMT -5. The time now is 02:00 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration