expect + tmux

调试5G BBU 写了一个shell,提高效率

  1 SESSION=autobbu
  2 
  3 RED='e[0;31m'
  4 GREEN='33[0;32m'
  5 NC='33[0m'
  6 green="33[0;32m"
  7 red="33[0;31m"
  8 reset="33[0;0m"
  9 dt_cmd="1d"
 10 
 11 grep "confirm-before kill-session" ~/.tmux.conf
 12 if [ $? != 0 ]; then
 13   echo "bind k confirm-before kill-session" >> ~/.tmux.conf
 14   echo -e "Configure ${GREEN}kill-seesion${NC} shortkey"
 15 fi
 16 
 17 TIMEOUT=10
 18 
 19 function expect_done(){
 20 #  #!/usr/bin/expect -f
 21 # /usr/bin/expect -d <<-EOF
 22 /usr/bin/expect <<-EOF
 23 set timeout $2
 24 # spawn ssh root@172.16.11.99
 25 # expect "*password:"
 26 # send "rootzhang
"
 27 # expect eof
 28 spawn tmux attach -t $1
 29 set cu_id $spawn_id
 30 sleep 2
 31 send -i $cu_id "$3
"
 32 sleep 2
 33 expect {
 34     -i $cu_id "$4" { puts "* run ${3}: ${green}READY${reset}"}
 35     timeout {
 36         puts "* run ${3}: ${red}timeout${reset}"
 37         # send -i $cu_id "$dt_cmd"
 38         exit 1
 39     }
 40     puts "* Tmux ${1} ${red}Exit.${reset}"
 41 }
 42 # tmux detach -s $1
 43 # interact
 44 EOF
 45 }
 46 
 47 
 48 function run_bbu(){
 49   SESSION=$1
 50   tmux new-session -s ${SESSION} -n PHY -d
 51   tmux send-keys -t ${SESSION} "echo 'cd /home/flexRAN_preBuild/flexran_e2e/flexran_l1/'" C-m
 52   tmux send-keys -t ${SESSION} "echo run_l1.sh" C-m
 53   TIMEOUT=90   # 72s
 54   EXP="PHY>welcome to application console"
 55   CMD="kubectl exec -it flexran-5gnr-bbu-l1du -- bash -c 'cd /home/flexRAN_preBuild/flexran_e2e/flexran_l1/; ./run_l1.sh; bash'"
 56   # tmux send-keys -t ${SESSION} "kubectl exec -it flexran-5gnr-bbu-l1du -- bash -c 'cd /home/flexRAN_preBuild/flexran_e2e/flexran_l1/; bash'" C-m
 57   expect_done ${SESSION} $TIMEOUT "$CMD" "$EXP"
 58   ret=$?
 59   if [ $ret != 0 ]; then
 60      echo -e "${RED}Failed to Setup flexran PHY${NC}"
 61      return $ret
 62   fi
 63   echo -e "${GREEN}Successful to Setup flexran PHY${NC}"
 64   # tmux split-window -v -p 90 -t ${SESSION}
 65   # tmux select-layout -t ${SESSION} main-horizontal
 66   # tmux send-keys -t ${SESSION}:0.1 "echo ptp4l -i $PTPNIC -m -f /home/ptp4l-slave.conf" C-m
 67   tmux new-window -n PHY -t ${SESSION}
 68   tmux send-keys -t ${SESSION}:1 "echo 'cd /home/flexRAN_preBuild/flexran_e2e/rsys_cu'" C-m
 69   tmux send-keys -t ${SESSION}:1 "echo run_cu.sh" C-m
 70   TIMEOUT=90   # 55s
 71   EXP="#############################################################################################################################"
 72   CMD="kubectl exec -it flexran-5gnr-bbu-l2cu -- bash -c 'cd /home/flexRAN_preBuild/flexran_e2e/rsys_cu; ./run_cu.sh; bash'"
 73   # tmux send-keys -t ${SESSION}:1 "kubectl exec -it flexran-5gnr-bbu-l2cu -- bash -c 'cd /home/flexRAN_preBuild/flexran_e2e/rsys_cu; bash'" C-m
 74   expect_done ${SESSION} $TIMEOUT "$CMD" "$EXP"
 75   ret=$?
 76   if [ $ret != 0 ]; then
 77      echo -e "${RED}Failed to Setup CU${NC}"
 78      return $ret
 79   fi
 80   echo -e "${GREEN}Successful to Setup CU${NC}"
 81   tmux new-window -n PHY -t ${SESSION}
 82   tmux send-keys -t ${SESSION}:2 "echo 'cd /home/flexRAN_preBuild/flexran_e2e/rsys_du'" C-m
 83   tmux send-keys -t ${SESSION}:2 "echo run_du_3D7G_DL4_UL2.sh" C-m
 84   TIMEOUT=75   # 42s
 85   EXP="CELL[1] is UP"
 86   CMD="kubectl exec -it flexran-5gnr-bbu-l1du -- bash -c 'cd /home/flexRAN_preBuild/flexran_e2e/rsys_du; ./run_du_3D7G_DL4_UL2.sh; bash'"
 87   # tmux send-keys -t ${SESSION}:2 "kubectl exec -it flexran-5gnr-bbu-l1du -- bash -c 'cd /home/flexRAN_preBuild/flexran_e2e/rsys_du; bash'" C-m
 88   expect_done ${SESSION} $TIMEOUT "$CMD" "$EXP"
 89   if [ $ret != 0 ]; then
 90      echo -e "${RED}Failed to Setup CELL${NC}"
 91   fi
 92   echo -e "${GREEN}Successful to Setup CELL${NC}"
 93   sleep 3
 94   tmux rename-window -t ${SESSION}:0 phy
 95   tmux rename-window -t ${SESSION}:1 cu
 96   tmux rename-window -t ${SESSION}:2 du
 97   tmux select-window -t ${SESSION}:0
 98 }
 99 
100 # https://pityonline.gitbooks.io/tmux-productive-mouse-free-development_zh/content/book-content/Chapter3.html
101 tmux has-session -t ${SESSION}
102 if [ $? != 0 ]
103 then
104   run_bbu ${SESSION}
105 fi
106 tmux rename-window -t ${SESSION}:0 phy
107 tmux rename-window -t ${SESSION}:1 cu
108 tmux rename-window -t ${SESSION}:2 du
109 tmux attach -t ${SESSION}

Session select example

Run or send a command to a tmux pane in a running tmux session

Attach to tmux session and run a command

Automating tcl/expect  

session=whatever
window=${session}:0
pane=${window}.4
tmux send-keys -t "$pane" C-z 'some -new command' Enter
tmux select-pane -t "$pane"
tmux select-window -t "$window"
tmux attach-session -t "$session"

 Tmux get screen output

# https://www.kancloud.cn/kancloud/tmux/62465

PANE=
FILE= 

if [[ $1 =~ ^.*[0-9]+.[0-9]+$ ]] || [[ $1 =~ ^[0-9]+$ ]]; then
  PANE="-t $1"
  FILE=${2:-tmux.log}
fi

if [ -z "$PANE" ]  && [ -n $2 ]; then
  FILE=${1}
  if [[ $2 =~ ^.*[0-9]+.[0-9]+$ ]] || [[ $2 =~ ^[0-9]+$ ]]; then
    PANE="-t $2"
  fi
fi

if [ -n $1 ] && [ -z "$PANE" ]; then
  FILE=${1}
fi

FILE=${FILE:-tmux.log}

START=${3:-100000000}
#  capture-pane (alias: capturep)

# tmux capturep -pS -100000000 -E 0 
# tmux capturep -pS -${START} -E 0  $PANE > $FILE
tmux capturep -S -100000000 -E 0 $PANE && tmux save-buffer $FILE

echo "Capture pane: ${PANE:-default}  to file $FILE"
echo "tmux capturep -pS -${START} -E 0  $PANE |less"
# show-buffer (alias: showb), choose-buffer
echo "tmux  showb"
echo ""

echo "--------------------------------"
tmux ls
echo "--------------------------------"
# list-panes (alias: lsp)
tmux lsp -s

echo ""
echo "--------------------------------"
echo "tmux showb"
echo ""
echo "To list a specify panes of a session, please: append the session to the follow command"
echo "tmux lsp -s -t "

log output

https://mdschreier.com/2018/03/01/tmux-logging-and-preserving-sessions/

pipe-pane

The easiest way to get logging activated is by following command

tmux  pipe-pane -o 'cat >>~/tmux_output.#S:#I-#P'

This would log the output of the current pane to the file mentioned.
While you can use following substitutions to define your filename:

  • #H Hostname of local host
  • #h Hostname of local host without the domain name
  • #F Current window flag
  • #I Current window index
  • #P Current pane index
  • #S Session name
  • #T Current window title
  • #W Current window name
  • ## A literal `#’

The substitution i choose assure unique-ness on any given point in time, so  you will not overwrite your output. It does not assure uniqueness over the time, so maybe you will have output of the todays session added to a complete different session, which had the same ideas before.

hooks

Maybe there is an option to add the “pipe-pane” command as hook when a new pane gets created. Unfortunately i was not able to find a full list of hooks and unfortunately hooks do not exist with Version 1.8.

The configuration in ~/.tmux.conf would maybe be similar to:

set-hook -g -n 'after-new-pane' 'run "pipe-pane -o 'cat >>~/output.#S:#I-#P'"'

… not working. There are no hooks with my version of tmux.

tmux logging 

https://github.com/tmux-plugins/tmux-logging

How to increase scrollback buffer size in tmux?

Tmux has pipe-pane, and it has -I and -O to go both directions.

It does require some gyrations to get around the fact that it logs everything (the command itself, terminal control, etc), and not just what's output by the command. Here's a quick and dirty example:

a) Start a tmux session to a remote host

b) On the local host, run this (assuming session 0):

$ tmux pipe-pane -o -t0 "tail -n +2 | col -b | base64 -d > ~/output"

$ tmux send-keys -t0 "base64 /some/file && read" Enter

$ tmux pipe-pane -t0

$ tmux send-keys -t0 Enter

You should now have ~/output on the local host, which is identical to /some/file on the remote.

 REF

How to increase scrollback buffer size in tmux?

How to increase scrollback buffer size in tmux?

tmux in practice: scrollback buffer

How do I increase my iterm (tmux) window scrollback  

原文地址:https://www.cnblogs.com/shaohef/p/15054356.html