SoFunction
Updated on 2024-10-29

Tool for bulk migration of xshell sessions to mobaxterm (python widget)

Wrote my own Python widget: xshell2mobaxterm, which can convert an xshell session into a mobaxterm data file for import into mobaxterm.

The exe version is compiled, the blog park does not support attachments, so I can not upload, if you need to comment to leave an e-mail, I will send it to you.

Python code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# xshell session to mobaxterm session file
# Support: TELNET, serial, SSH
# Usage:  Xshell_Session_Direcotry
# Example: python3  D:\Xshell\Session >
# Linux platform enviroment: LANG=zh_CN.utf8/en_US.utf8
# Windows: python3.6 cmd run it.

import os, sys


def convert_x2m(xshell_session_file, num):
    #print(xshell_session_file)
    ssh_format = (
        '[{BookMarks}]\n'
        'SubRep={Directory}\n'
        'ImgNum=41\n'
        '{SessionName} ({UserName})=#109#0%{Host}%{Port}%{UserName}%%-1%-1%%%%%0%0%0%%%-1%0%0%0%%1080%%0%0%1#DejaVu Sans Mono%{FontSize}%0%0%-1%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%1%-1#0# #-1\n'
    )
    
    telnet_format = (
        '[{BookMarks}]\n'
        'SubRep={Directory}\n'
        'ImgNum=41\n'
        '{SessionName} ()= #98#1%{Host}%{Port}%%%2%%%%%0%0%%1080%#DejaVu Sans Mono%{FontSize}%0%0%-1%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%1%-1#0# #-1\n'
    )
    
    serial_format = (
        '[{BookMarks}]\n'
        'SubRep={Directory}\n'
        'ImgNum=42\n'
        '{SessionName}= #131#8%-2%100{Speed}%3%0%0%1%2%{Port}#DejaVu Sans Mono%{FontSize}%0%0%-1%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%1%-1#0# #-1\n'
    )

    session_file = open(xshell_session_file, 'r')
    session_lines = session_file.readlines()
    xshell_attr = {'num': num}
    xshell_attr['Directory'] = (xshell_session_file.replace([1], '').strip('\\'))
    xshell_attr['SessionName'] = (xshell_session_file).rsplit('.',1)[0]

    if num == 0:
        xshell_attr['BookMarks'] = 'Bookmarks'
    else:
        xshell_attr['BookMarks'] = 'Bookmarks_{num}'.format(num=num)

    for line in session_lines:
        if ('UserName='):
            xshell_attr['UserName'] = ('=')[1].strip('\n')
        elif ('Port='):
            xshell_attr['Port'] = ('=')[1].strip('\n')
        elif ('FontSize='):
            xshell_attr['FontSize'] = ('=')[1].strip('\n')
        elif ('Host='):
            xshell_attr['Host'] = ('=')[1].strip('\n')
        elif ('Protocol='):
            xshell_attr['Protocol'] = ('=')[1].strip('\n')
        elif ('BaudRate='):
            xshell_attr['Speed'] = ('=')[1].strip('\n')

    #print(xshell_attr)
    x = 1
    if xshell_attr.get('Protocol').lower().startswith('ssh'):
        x = ssh_format.format(
            Directory = xshell_attr.get('Directory') or '',
            SessionName = xshell_attr.get('SessionName') or 'default',
            UserName = xshell_attr.get('UserName') or '',
            Port = xshell_attr.get('Port') or '22',
            FontSize = xshell_attr.get('FontSize') or '12',
            num=xshell_attr['num'],
            Host=xshell_attr.get('Host') or '',
            BookMarks=xshell_attr['BookMarks']
        )
    elif xshell_attr.get('Protocol').lower() == 'telnet':
        x = telnet_format.format(
            Directory = xshell_attr.get('Directory') or '',
            SessionName = xshell_attr.get('SessionName') or 'default',
            UserName = xshell_attr.get('UserName') or '',
            FontSize = xshell_attr.get('FontSize') or '12',
            Port = xshell_attr.get('Port') or '23',
            num=xshell_attr['num'],
            Host=xshell_attr.get('Host') or '',
            BookMarks=xshell_attr['BookMarks']
        )
    elif xshell_attr.get('Protocol').lower() == 'serial':
        x = serial_format.format(
            Directory = xshell_attr.get('Directory') or '',
            SessionName = xshell_attr.get('SessionName') or 'default',
            FontSize = xshell_attr.get('FontSize') or '12',
            Port = xshell_attr.get('Port') or '',
            Speed = xshell_attr.get('Speed') or '9600',
            num = xshell_attr['num'],
            BookMarks = xshell_attr['BookMarks']
        )
    #print(('/', '\\'))
    return ('/', '\\')


def generate_mobaxterm_sessions(xshell_session_path, count):
    #print(session_path)
    base_dir = (xshell_session_path)
    for file in base_dir:
        file = (xshell_session_path, file)
        try:
            (file)
            #print(file)
            generate_mobaxterm_sessions(file, count)
        except:
            if not ('.xsh'): continue
            print(convert_x2m(file, count[0]))
            count[0] += 1

if __name__ == '__main__':
    count = [0]
    generate_mobaxterm_sessions([1], count)

Usage:

(1) cmd> python3 Python code file "Path where Xshell Session is located" >

For example, C:\Users\user>python3 "C:\Users\user\Documents\NetSarang\Xshell\Sessions" >

A file will be generated in the current directory of cmd (C:\Users\user\)

Note: If you don't know the path to the Xshell Session, you can check it by typing the pwd command in the xshell local command line:

(32) Just import the generated file into mobaxterm.

To this article on the xshell session batch migration to mobaxterm tool is introduced to this article, more related xshell session migration mobaxterm tool content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!