xshell会话批量迁移到mobaxterm的工具(python小工具)

 更新时间:2021年12月20日 15:41:37   作者:韩晓萌  
这篇文章主要介绍了xshell会话批量迁移到mobaxterm的工具,使用方法也超级简单,本文通过python代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

自己写了一个Python小工具:xshell2mobaxterm,可以将xshell的session转换成mobaxterm的数据文件以导入到mobaxterm中。

exe版的编译好了,博客园不支持附件,所以没法上传,需要的话可以评论留下邮箱,我发送给你。

Python代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# xshell session to mobaxterm session file
# Support: TELNET, serial, SSH
# Usage: xshell2mobaxterm.py Xshell_Session_Direcotry
# Example: python3 xshell2mobaxterm.py D:\Xshell\Session >mobaxterm.mxtsessions
# 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'] = os.path.dirname(xshell_session_file.replace(sys.argv[1], '').strip('\\'))
    xshell_attr['SessionName'] = os.path.basename(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 line.startswith('UserName='):
            xshell_attr['UserName'] = line.split('=')[1].strip('\n')
        elif line.startswith('Port='):
            xshell_attr['Port'] = line.split('=')[1].strip('\n')
        elif line.startswith('FontSize='):
            xshell_attr['FontSize'] = line.split('=')[1].strip('\n')
        elif line.startswith('Host='):
            xshell_attr['Host'] = line.split('=')[1].strip('\n')
        elif line.startswith('Protocol='):
            xshell_attr['Protocol'] = line.split('=')[1].strip('\n')
        elif line.startswith('BaudRate='):
            xshell_attr['Speed'] = line.split('=')[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(x.replace('/', '\\'))
    return x.replace('/', '\\')


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

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

使用方法:

(1)cmd>python3 Python代码文件 "Xshell Session所在的路径" >mobaxterm.mxtsessions

例如:C:\Users\user>python3 xshell2moba.py "C:\Users\user\Documents\NetSarang\Xshell\Sessions" > mobaxterm.mxtsessions

会在cmd的当前目录(C:\Users\user\)中生成一个mobaxterm.mxtsessions文件

备注:如果不知道Xshell Session的路径,可以在xshell本地命令行中输入pwd命令查看:

(32)将生成的mobaxterm.mxtsessions文件导入mobaxterm即可。

到此这篇关于xshell会话批量迁移到mobaxterm的工具的文章就介绍到这了,更多相关xshell会话迁移mobaxterm工具内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Python实现SSH隧道界面功能

    使用Python实现SSH隧道界面功能

    这篇文章主要介绍了使用Python实现一个SSH隧道界面功能,界面使用tkinter实现,左边是输入隧道的信息,右边为历史列表,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2022-02-02
  • 浅析Python3爬虫登录模拟

    浅析Python3爬虫登录模拟

    本篇文章通过学习python爬虫相关知识点来分析Python3爬虫登录模拟的原理以及相关代码分析,对此有兴趣的朋友参考下。
    2018-02-02
  • python中的实例方法、静态方法、类方法、类变量和实例变量浅析

    python中的实例方法、静态方法、类方法、类变量和实例变量浅析

    这篇文章主要介绍了python中的实例方法、静态方法、类方法、类变量和实例变量浅析,需要的朋友可以参考下
    2014-04-04
  • python skimage图像处理

    python skimage图像处理

    这篇文章主要为大家介绍了python skimage图像处理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Pytorch实现神经网络的分类方式

    Pytorch实现神经网络的分类方式

    今天小编就为大家分享一篇Pytorch实现神经网络的分类方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • 实例详解Python模块decimal

    实例详解Python模块decimal

    这篇文章主要介绍了Python模块decimal ,Python提供了decimal模块用于十进制数学计算,它具有以下特点在文中给大家详细介绍,需要的朋友可以参考下
    2019-06-06
  • 利用python生成一个导出数据库的bat脚本文件的方法

    利用python生成一个导出数据库的bat脚本文件的方法

    下面小编就为大家带来一篇利用python生成一个导出数据库的bat脚本文件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • 30秒学会30个超实用Python代码片段【收藏版】

    30秒学会30个超实用Python代码片段【收藏版】

    许多人在数据科学、机器学习、web开发、脚本编写和自动化等领域中都会使用Python,它是一种十分流行的语言。本文将简要介绍30个简短的、且能在30秒内掌握的代码片段,感兴趣的朋友一起看看吧
    2019-10-10
  • django使用admin站点上传图片的实例

    django使用admin站点上传图片的实例

    今天小编就为大家分享一篇django使用admin站点上传图片的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • tensorflow 获取所有variable或tensor的name示例

    tensorflow 获取所有variable或tensor的name示例

    今天小编就为大家分享一篇tensorflow 获取所有variable或tensor的name示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01

最新评论