最新发现xshell用的有点问题,用moba的话好多会话需要导入,手动比较麻烦,这边参考别人的代码,修改了一下能够正常使用了,本人菜鸟,仅做参考。
参考大佬连接https://github.com/guugle/xshelltoMobaXterm
# -*- coding:utf-8 -*-
import codecs
import os
import sys
from configparser import ConfigParser
# 将代码放在Session目录执行以下命令即可 完成转换 会自动生成对应文件
# python xshelltomoba.py
# 只扫描特定文件
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%-1%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"
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):
global iconId
# print('basePath', basePath)
iconId = 41 # 图标ID
# 新建文件并以GBK的格式写入
with open('xshell.mxtsessions', 'w', encoding='GBK') as f:
for dirRoot, dirs, dirFiles in os.walk(basePath):
if iconId == 41:
SubRep = ''
else:
break
print("[Bookmarks]")
print("SubRep=%s" % SubRep)
print("ImgNum=%d" % iconId)
a = "[Bookmarks]\n"
b = "SubRep=" + SubRep + '\n'
c = "ImgNum=" + str(iconId) + '\n'
f.write(a)
f.write(b)
f.write(c)
iconId += 1
for fileName in dirFiles:
if fileName.endswith(suffix_name):
# print(dirRoot,111,os.sep,2222,fileName,333)
fileFullName = dirRoot + os.sep + fileName
# print(fileFullName)
infoList = convertFile(fileFullName, fileName)
d = joinString(infoList, keyLocation, iconId) + '\n'
f.write(d)
f.close()
if __name__ == '__main__':
# 会自动删除前面的创建的文件
os.popen('rm -rf xshell.mxtsessions')
xshellpath = keyLocation
scanDirs(xshellpath)
# os.popen('python exchange.py ./Session > xshell.mxtsessions')