expect + tmux

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

 

  1 SESSION=autobbu
  2 
  3 RED='\e[0;31m'
  4 GREEN='\033[0;32m'
  5 NC='\033[0m'
  6 green="\033\[0;32m"
  7 red="\033\[0;31m"
  8 reset="\033\[0;0m"
  9 dt_cmd="\01d"
 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\r"
 27 # expect eof
 28 spawn tmux attach -t $1
 29 set cu_id \$spawn_id
 30 sleep 2
 31 send -i \$cu_id "$3\r"
 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}

 

上一篇:linux shell脚本经典案例8---批量主机执行命令


下一篇:shell脚本批量部署