Skip to content

Instantly share code, notes, and snippets.

@jupihes
Last active October 31, 2024 16:49
Show Gist options
  • Save jupihes/a9e0413b0ae3b81c9a3e91b51bd7edb5 to your computer and use it in GitHub Desktop.
Save jupihes/a9e0413b0ae3b81c9a3e91b51bd7edb5 to your computer and use it in GitHub Desktop.

MobaXterm session export data extraction

Below regex pattern help to extract needful information from Portable export of SSH sessions. Although not complete, can help data extraction from export file.

SSH_pattern = r'(?<Servername>[\w. ()]+)=#(?<Type>\d+)#0%(?<IP>[\w.]+)%(?<port>\d+)%%%([\w.-]+)%([\d-]+)%%%([\w.-]+)%%\d%([\w.-]+)%([\w

Take a look at .mxtsessions file format

Pandas data extraction

import pandas as pd
import re
from os import chdir
chdir(r'/Mobaxterm/')

moba_ssh_text = r'MobaXterm Sessions.mxtsessions'
text = open(moba_ssh_text).readlines()

regex_pat = r"((?P<Servername>[\w. ()]+)=#(?P<Type>\d+)#0%(?P<IP>[\w.]+)%(?P<port>\d+)%%%([\w.-]+)%([\d-]+)%%%([\w.-]+)%%\d%([\w.-]+)%([\w.-]+)%%%([\w.-]+)%([\w.-]+)%([\w.-]+)%([\w.-]+)%%\d+%%\d%\d%\d#)"

df = pd.DataFrame(text)
df = df[0].str.extract(pat=regex_pat, expand=True)

df = df.dropna()
df.drop(columns=[0], inplace=True)

Text play for data extraction

import re

regex = r"(?<Servername>[\w. ()]+)=#(?<Type>\d+)#0%(?<IP>[\w.]+)%(?<port>\d+)%%%([\w.-]+)%([\d-]+)%%%([\w.-]+)%%\d%([\w.-]+)%([\w.-]+)%%%([\w.-]+)%([\w.-]+)%([\w.-]+)%([\w.-]+)%%\d+%%\d%\d%\d#"

test_str = ("1.2.3.47=#109#0%1.2.3.47%22%%%-1%-1%%%22%%0%-1%0%%%-1%0%0%0%%1080%%0%0%1#MobaFont%10%0%0%0%15%236,236,236%0,0,0%180,180,192%0%-1%0%%xterm%-1%0%0,0,0%54,54,54%255,96,96%255,128,128%96,255,96%128,255,128%255,255,54%255,255,128%96,96,255%128,128,255%255,54,255%255,128,255%54,255,255%128,255,255%236,236,236%255,255,255%80%24%0%1%-1%<none>%%0#0# #-1\n"
	"10.11.12.13=#109#0%10.11.12.13%6379%%%-1%-1%%%22%%0%-1%0%%%-1%0%0%0%%1080%%0%0%1#MobaFont%10%0%0%0%15%236,236,236%0,0,0%180,180,192%0%-1%0%%xterm%-1%0%0,0,0%54,54,54%255,96,96%255,128,128%96,255,96%128,255,128%255,255,54%255,255,128%96,96,255%128,128,255%255,54,255%255,128,255%54,255,255%128,255,255%236,236,236%255,255,255%80%24%0%1%-1%<none>%%0#0# #-1\n")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    
    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1
        
        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
        
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment