xshell session导入mobaxsterm

工作要求

不可使用xshell,xftp等工具,当然我也不用,常用mobaxsterm,比较直观看到服务器性能以及拖拽文件方便,再次不做赘述。此时同时同事们面临多个xshell得session导入费时问题,参与研究,找到解决方法。参考于 https://www.cnblogs.com/hanxiaomeng/p/15066986.html

一、准备python环境

Python官网:https://www.python.org/ 安装最新python脚本并选择add path

二、 找到xshell session存储位置

在这里插入图片描述

三、启动py脚本

#!/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=42\n'
        '{SessionName} ({UserName})=#109#0%{Host}%{Port}%{UserName}%%-1%-1%%%{Port}%%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\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')is not None and 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')is not None and 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') is not None and  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('/', '\\'))
    if x==1:
        return None
    else:
        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
            if convert_x2m(file, count[0]) is  None: continue
            print(convert_x2m(file, count[0]))
            count[0] += 1


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

**

注:不通版本得mobaxterm得ssh_format不一致,需要根据所需版本有所修改,导出文件为编码格式为“UCS-2 Little Endian”,不支持导入,对此可以新建一个txt,赋值字符,修改格式为"ANSI",自此功能完成

**

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
更新的内容包含: Version 10.8 (2018-07-07) Bugfix: fixed the error message which occured when running the graphical package manager "MobApt" 破解Securecrt怕中毒,Xshell 用着不爽,Putty太单薄,手头没Mac用不了iterm2。那就试试这个全能开源的终端吧(MobaXterm)!下面就介绍下MobaXterm的主要功能 windows下支持多标签的终端 通过MobaXterm进行远程终端链接,你可以创建 SSH, Telnet, Rlogin, RDP, VNC, XDMCP, FTP, SFTP or 串口等链接。你的每次链接都会自动保存并且出现在左侧链接窗口中。 对于本机,MobaXterm让你可以在windows下运行Unix命令,如:ls, cd, grep, awk, tail, cut, sed, wget, rsync, telnet, ssh, rlogin, rsh…等等Unix基本命令。而且MobaXterm有很多免费的插件可以实现你更多的需求 图形化的SFTP 浏览器 当你链接一个ssh终端的时候,左侧窗口就会出现一个图形化的SFTP 浏览器,它可以让你通过安全的SFTP 链接拖放文件来管理远程服务器。(不喜欢可以关闭) 分屏显示及多任务执行 MobaXterm支持多分屏显示,方便管理多台服务器,并且你可以仅输入一次,让一条命令同时在这些不同的服务器终端执行。(ps:是不是贼拉的方便) 内置文本编辑器 在SFTP浏览器双击文件,既可以用默认的编辑器打开文件进行编辑。 Windows远程桌面(RDP) 通过 RDP 协议远程链接并控制你的windows电脑. 在session 管理面板中你可以找到更多的RDP配置选项。 Unix远程桌面(XDMCP) 有了 MobaXterm ,你可以在Windows 电脑上,通过XDMCP协议,远程控制Solaris 桌面系统。 更多功能见 Embedded servers MobaXterm allows you to start network daemons for remote access. No extra tool or runtime is needed in order to use these daemons. Embedded tools MobaXterm brings some useful tools for sysadmins, developpers, webmasters and all users who need to work efficiently with their computer. SSH gateway In ssh, telnet, RDP, VNC sessions, you can select a “SSH-gateway” (a.k.a. “jump host”) in order to tell MobaXterm to connect first to a SSH server before connecting to the end-server you want to reach in the end. This allow you to reach some servers behind a firewall and to secure your connection. X11 server When you run a SSH, TELNET or RLOGIN/RSH session you will be able to display your remote applications directly on your local Windows PC. In a SSH session, there is no need to set the “DISPLAY” variable as MobaXterm uses X11-forwarding in order to ease and secure your work. Enhanced X extensions The embedded X server based on X.org provides the latest features available in recent X server implementations: extensions such as OpenGL, Composite or Randr are included. Xdmcp protocol is also supported. SSH tunnels (port forwarding) The graphical SSH tunnels manager allows you to create your SSH tunnels using an intuitive graphical tool. MobApt package manager MobaXterm package manager (MobApt / apt-get) allows you to download and use much more Unix tools directly into MobaXterm terminal. Macros support You can record macros in MobaXterm terminal: everything you type in the terminal will be recorded in order to replay it later on other servers. Passwords management MobaXterm is able to save your session passwords and to keep them secure by using a “Master password”. Professional Customizer MobaXterm Professional Edition gives you access to professional support and to the “Customizer” software. This program allows you to generate customized copies of MobaXterm with your own logo and default settings. 引用地址:http://www.open-open.com/lib/view/open1437704662490.html
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值