Use shell to generate session template for MobaXterm

1 Introduction

  1. Actually this is a fancy thing
  2. I usually use a virtual machine to run some linux tests. My daily habit is to clone a DHCP network machine, so every time the IP is new
  3. Therefore, every time you use MobaXterm to connect to a new machine, you need to create a new session (sometimes there are many machines, and it is really annoying to set up one by one)
  4. So there is the following flashy operation

2. Export the session template of MobaXterm

Insert picture description here

  1. In User sessionshere, right
  2. Select Export all sessions to fileand export the session template
  3. The exported template name is: MobaXterm Sessions.mxtsessions(As long as the suffix is .mxtsessions, you can import MobaXterm)
  4. Let's take a look at the exported template content
[Bookmarks]
SubRep=
ImgNum=42
192.168.10.2 (root)=#109#0%192.168.10.2%22%root%%-1%-1%%%22%%0%0%0%%%-1%0%0%0%%1080%%0%0%1#MobaFont%10%0%0%0%15%236,236,236%30,30,30%180,180,192%0%-1%0%%xterm%-1%-1%_Std_Colors_0_%80%24%0%1%-1%<none>%%0#0# #-1

3. Use shell script to generate .mxtsessions file

#!/usr/bin/env bash
set -e
PWD=$(cd `dirname $0`; pwd)
iphead="192.168"
ipbody="10"
user='root'
xterm='%%-1%-1%%%22%%0%0%0%%%-1%0%0%0%%1080%%0%0%1#Consolas%14%0%0%0%15%236,236,236%0,0,0%180,180,192%0%-1%0%%xterm%-1%-1%30,30,30%54,54,54%255,96,96%255,128,128%14,177,108%24,237,147%190,190,18%222,220,18%18,150,190%27,186,233%255,77,255%255,125,255%84,204,239%142,221,244%204,204,204%204,204,204%80%24%0%1%-1%<none>%%0#0# #-1'

function deploytxt (){
    
    
  seq 2 254 > ${PWD}/${iphead}.${ipbody}.0-remote.txt
  seq -w 2 254 > ${PWD}/${iphead}.${ipbody}.0-sessionName.txt
  sed -i "s/^/${iphead}.${ipbody}./g" ${PWD}/${iphead}.${ipbody}.0-remote.txt
  sed -i "s/^/${iphead}.${ipbody}./g" ${PWD}/${iphead}.${ipbody}.0-sessionName.txt
  paste ${PWD}/${iphead}.${ipbody}.0-sessionName.txt ${PWD}/${iphead}.${ipbody}.0-remote.txt > xterm.txt
  printf "\e[1;32m ${iphead}.${ipbody}.0.txt is created in ${PWD}/\e[0m\n"
}

function deploymxtsessions () {
    
    
cat > ${PWD}/${iphead}.${ipbody}.0.mxtsessions <<EOF
[Bookmarks]
SubRep=
ImgNum=42
EOF
echo "##sessionName## (##user##)=#109#0%##remote##%22%##user##${xterm}" > ${PWD}/xterm.example.txt
printf "\e[1;32m ${iphead}.${ipbody}.0.mxtsessions example is created in ${PWD}/\e[0m\n"
}

function createmxtsessions () {
    
    
while read xterm_line
do
    sessionName=$(echo $xterm_line | awk '{print $1}')
    remote=$(echo $xterm_line | awk '{print $2}')
    while read xterm_example_line
    do
      echo ${xterm_example_line} | sed -e "s/##sessionName##/${sessionName}/" -e "s/##remote##/${remote}/" >> ${PWD}/${iphead}.${ipbody}.0.mxtsessions
    done < ${PWD}/xterm.example.txt
done < ${PWD}/xterm.txt
sed -i "s/##user##/${user}/g" ${PWD}/${iphead}.${ipbody}.0.mxtsessions
rm -f ${PWD}/*.txt
printf "\e[1;32m ${iphead}.${ipbody}.0.mxtsessions is created in ${PWD}/\e[0m\n"
}


function main () {
    
    
deploytxt
deploymxtsessions
createmxtsessions
}

main
linux-oz6w:~ # sh mxtsessions.sh
 192.168.10.0.txt is created in /root/
 192.168.10.0.mxtsessions example is created in /root/
 192.168.10.0.mxtsessions is created in /root/
linux-oz6w:~ # ll | grep mxtsessions
-rw-r--r-- 1 root root 54172 Jan  8 06:27 192.168.10.0.mxtsessions
-rw-r--r-- 1 root root  1122 Jan  8 06:08 mxtsessions.sh
`执行脚本后,就会产生192.168.10.0.mxtsessions,从虚拟机导出到宿主机后,就可以导入到MobaXterm`
`脚本里面会删除掉脚本所在路径下所有的.txt文件,一定要注意脚本的执行路径,不要误删了自己的.txt文件`

4. Import to MobaXterm

Insert picture description here

  1. In User sessionshere, right
  2. Click first New folder(this step is to create a directory, put all the sessions of the 192.168.10.0 network segment together, only for obsessive-compulsive disorder, you don’t need to do it)
  3. In 192.168.10.0here, right
  4. Click Import sessions into this folder, select the exported one 192.168.10.0.mxtsessions, click it (if no directory is created, click Import sessions from file)

5. Effect picture

Insert picture description here

  1. Too long, just intercept a part
  2. The script has been updated and optimized, in order to look a little more comfortable, and to make the operation more comfortable

Guess you like

Origin blog.csdn.net/u010383467/article/details/112341605