xshell session转moba模板

已实现子目录查询 但是只支持2层目录 使用前需要创建一个默认的文件夹New folder 在 New folder中导入
源博客https://blog.csdn.net/weixin_45896283/article/details/130566415?spm=1001.2014.3001.5502


# -*- coding:utf-8 -*-
import codecs
import os
import sys
from configparser import ConfigParser
# 将代码放在Session目录执行以下命令即可 完成转换
# python xshelltomoba.py ./Session > xshell.mxtsessions

# 只扫描特定文件
suffix_name = '.xsh'

# 这是密钥路径 请填对应的Session目录
# keyLocation = "E:\\Python_py\\test\\Sessions"

def joinString(infolist, keyLocation, iconId):
    fullString = infolist[0] + ' (' + infolist[3] + ')' + "=#" + str(iconId) + "#0%" + infolist[1] + "%" + infolist[2] +  "%" + infolist[
        3] + "%%-1%-1%%%%%0%0%0%%%-1%0%0%0%%1080%%0%0%1%%0%%%%0%-1%-1%0#MobaFont%10%0%0%-1%15%236,236,236%30,30,30%180,180,192%0%-1%0%%xterm%-1%0%_Std_Colors_0_%80%24%0%1%-1%<none>%%0%0%-1%0#0# #-1"
    # print(fullString)
    return fullString


def convertFile(filePath, fileName):
    config = ConfigParser()
    config.read_file(codecs.open(filePath, "r", "utf-16"))
    name = fileName.replace(".xsh", "")
    ipAddr = config.get("CONNECTION", "Host")
    port = config.get("CONNECTION", "Port")
    username = config.get("CONNECTION:AUTHENTICATION", "UserName")
    infoList = [name, ipAddr, port, username]
    return infoList



def scanDirs(basePath):
    # 新建文件并以GBK的格式写入
    with open('xshell.mxtsessions', 'w', encoding='GBK') as f:
        i = 1
        iconId = 41
        a = "[Bookmarks]\n"
        b = "SubRep=" + '\n'
        c = "ImgNum=42" + '\n'
        f.write(a)
        f.write(b)
        f.write(c)
        f.write('\n')
        for dirRoot, dirs, dirFiles in os.walk(basePath):
            # 图标ID
            if dirRoot == basePath:
                SubRep = 'New folder'
                a = "[Bookmarks_" + str(i) + "]\n"
                b = "SubRep=" + SubRep + '\n'
                c = "ImgNum=" + str(iconId) + '\n'
                f.write(a)
                f.write(b)
                f.write(c)
                d = add(dirRoot, dirFiles, iconId)
                for m in d:
                    f.write(m)
                f.write('\n')
                i = i + 1
            else:
                SubRep = dirRoot.split(basePath)[1]
                SubRep = 'New folder' + SubRep
                a = "[Bookmarks_" + str(i) + "]\n"
                b = "SubRep=" + SubRep + '\n'
                c = "ImgNum=" + str(iconId) + '\n'
                f.write(a)
                f.write(b)
                f.write(c)
                add(dirRoot, dirFiles, iconId)
                d = add(dirRoot, dirFiles, iconId)
                for m in d:
                    f.write(m)
                f.write('\n')
                i = i + 1
    f.close()


def add(dirRoot,dirFiles,iconId):
    a = []
    for fileName in dirFiles:
        if fileName.endswith(suffix_name):
            fileFullName = dirRoot + os.sep + fileName
            # print(fileFullName)
            infoList = convertFile(fileFullName, fileName)
            d = joinString(infoList, keyLocation, iconId) + '\n'
            a.append(d)
    return a


if __name__ == '__main__':
    # 会自动删除前面的创建的文件
    # os.popen('rm -rf xshell.mxtsessions')
    xshellpath = keyLocation
    scanDirs(xshellpath)
    # os.popen('python exchange.py ./Session > xshell.mxtsessions')


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值