即时修改数据流 [英] modify a datastream on the fly

查看:124
本文介绍了即时修改数据流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要劫持并修改数据流.流由固定宽度的命令组成.每个命令都是新行,文档说明每个命令以STX/ETX对(文本的开始和结束)开始和结束

I need to hijack and modify a datastream. The stream consists of fixed-width commands. Each command is a new line, and the documentation says that each command starts and ends with an STX/ETX pair (start and end of text)

发送系统正在使用串行,但遭到了iPocket设备的攻击,该设备通过IP与我们的PBX通信.据我所知,这只是将串行转换为telnet,这有望使事情变得很简单.

The sending system is using serial, but is attacked to an iPocket device that communicates over IP to our PBX. From what I can tell it's just converting the serial to telnet, which should keep things pretty simple hopefully.

我需要查找的字符串以NAM开头.结构是这样的:

The string I need to look for starts with NAM. The structure is something like this:

STX NAM EXT# LASTNAME,FIRSTNAME ETX

我需要用空格替换,FIRSTNAME,以免改变命令的长度.

I need to replace ,FIRSTNAME with whitespace so it doesn't change the length of the command.

我一直在尝试以下操作,尽管它可以按要求在两个方向上将数据从头到尾传递,并删除所需的数据,但这并不能保持命令的长度.

I've been attempting the following, and while it passes the data end to end in both directions as required, and removes the needed data, it isn't maintaining the length of the command.

ipocket <-> nc -kl 1100 | sed 's/,[^,]*/ /g' | nc target_ip target_port <-> PBX

我将使用linux系统完成此任务.我可以肯定地说,可以很简单地在perl或python中完成此操作,但是我不知道从哪里开始.任何帮助将不胜感激!

I'll be using a linux box for this task. I'm reasonably certain that this could be done quite simply in perl or python but I haven't got a clue where to start. Any assistance would be greatly appreciated!

推荐答案

这应该做到这一点,它只是一个衬里

This should do it, its a one liner

perl -pe '/^STX\s\w+\s\d+#\s\w+,(\w+)\sETX$/;$len=length($1);s/$1/" " x $len/e'

我使用进行过测试.

echo "STX NAM 100# LASTNAME,FIRSTNAME ETX" | perl -pe '/^STX\s\w+\s\d+#\s\w+,(\w+)\sETX$/;$len=length($1);s/$1/" " x $len/e'

它返回

STX NAM 100# LASTNAME,          ETX

为确保字符串长度与我测试的长度相同.

To ensure the strings are the same length i tested.

echo "STX NAM 100# LASTNAME,FIRSTNAME ETX" | perl -pe '/^STX\s\w+\s\d+#\s\w+,(\w+)\sETX$/;$len=length($1);s/$1/" " x $len/e'| perl -pe 'print length($_);'

给36岁.

echo "STX NAM 100# LASTNAME,FIRSTNAME ETX"|perl -pe 'print length($_);'

给36岁.

这篇关于即时修改数据流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆