文本处理
程序员文章站
2022-05-16 20:25:03
...
文章目录
行首行末添加字符串
实际应用场景:
我们现在有一个只有cmd功能的system权限的windows shell
主机为xp 32位,3389处于打开状态,我们想使用ew将其3389端口代理出来,但是xp既没有bitsadmin有没有certutil,我们可以选择使用vbs脚本进行下载,这时我们就需要通过echo命令来将vbs脚本的内容写入到目标主机的一个文件中,如果手动一行一行地使用echo命令写入,实在是太麻烦了,我们可以借助sed强大的文本处理功能,在原来的vbs的每一行的行首和行尾加上“echo ”和“>>%temp\update.vbs%”,首先将vbs内容写到test中,然后执行下面两条命令,复制test中的内容,直接粘贴到我们的cmd shell上,一次性执行完所有的写入命令,最后再执行cscript.exe %temp%\update.vbs即可
sed -i 's/^/&echo /g' test
sed -i 's/$/& >>%temp%\\update.vbs/g' test
第二条命令中多出来的反斜杠为转义符
搜索当前目录中的所有文件,查找指定字符串
应用场景,调试PHP时,寻找某一字符串是由哪一个文件输出的
grep -nr "print" *
r选项代表递归(查询子目录)
*代表所有文件
截取字符串
应用场景:
现在我要从一份泄露的邮箱账户中截取出密码,形式如下:
aaa@qq.com:test
我需要截取出:
后面的字符串
只需要使用下面这条命令即可:
var="aaa@qq.com:test"
echo ${var#*:}
获取子串在母串中的偏移量
echo $a | grep -bo ":" | sed 's/:.*$//'
grep -bo ":"
打印样式匹配所位于的字符或字节偏移,如果不使用sed进行处理,会把我们要匹配的字符也跟在后面,并使用:
分割,因此我们需要使用sed的正则匹配,将:
以及:
之后的所有字符替换成空
反转字符串
echo str | rev
剔除含有某一字符串的行
sed -i '/keywords/d' filename
提取出包含某一字符串的行
sed -n '/keywords/p' filename
JS提取域中的主机
我们使用dsquery导出的计算机结果基本上是对下面这个样子:
"CN=CR-50,OU=Coumputers_CR,DC=domain,DC=com"
"CN=CR-55,OU=Coumputers_CR,DC=domain,DC=com"
"CN=CR-54,OU=Coumputers_CR,DC=domain,DC=com"
我们可以使用下面这个脚本,将主机名提取出来:
var str = `
"CN=CR-50,OU=Coumputers_CR,DC=domain,DC=com"
"CN=CR-55,OU=Coumputers_CR,DC=domain,DC=com"
"CN=CR-54,OU=Coumputers_CR,DC=domain,DC=com"
`
str.trim().split('\n').forEach(function(v, i) {
var result = /.*?"CN=(.*?),.*?/.exec(v);
document.write(result[1]);
document.write("</br>");
})
这样就可以在浏览器中输出主机名了
然后用该脚本测试那些主机可以联通:
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f %%i in (dn.txt) do (
echo %%i
net use \\%%i\ipc$ "password" /user:cotoso.com\administrator >>temp.tmp 2>&1
for /f %%l in ('type temp.tmp ^| find /i "1326" /c') do (
if %%l equ 1 (
echo %%i>>res.txt )
)
del temp.tmp
)
使用JS从任意字符串中提取出IP地址
正则表达式:
(\d{1,3}\.){3}\d{1,3}
\d{1,3}\.
表示匹配一个1~3个数字组成的字符串和一个.
d
和.
元字符需要转义所有前面的那个\
,这个是正则的基础语法,不需要我多解释
将它们用()
括起来,作为一个匹配模式,后面的{3}
表示该匹配模式要重复3次,最后再跟上一个\d{1,3}
,由1~3个字符组成的数字
这样就能匹配到所有符合IP地址特征的字符串了
使用JS实现,代码如下:
var str = `
172.96.244.114 testsetsetset testsetsetset testsetsetset testsetsetset104.224.181.204 asdasdasd testsetsetset testsetsetset testsetsetset67.230.185.196 testsetsetset testsetsetset testsetsetset testsetsetset104.224.144.160 testsetsetset testsetsetset asdasdasd testsetsetset104.224.182.250 testsetsetset testsetsetset testsetsetset testsetsetset104.224.190.79 testsetsetset testsetsetset 2oixMlMlzAPPkUxt testsetsetset testsetsetset95.163.207.244 testsetsetset testsetsetset syi71eRZiehv testsetsetset172.96.241.42 testsetsetset testsetsetset testsetsetset testsetsetset198.181.40.163 testsetsetset testsetsetset testsetsetset testsetsetset104.224.178.144 testsetsetset testsetsetset testsetsetset testsetsetset104.224.177.150 testsetsetset testsetsetset testsetsetset testsetsetsetasdasdas
`
var result = str.match(/(\d{1,3}\.){3}\d{1,3}/g);
for ( var i = 0; i <result.length; i++){
document.write(result[i]);
document.write("<br />");
}
IP 排序
sort -t . -k 3,3n -k 4,4n /path/to/file >> /path/to/newfile
JAvascript正则提取出符合条件的字符串
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
</head>
<body>
function PreviewText() {
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("uploadText").files[0]);
oFReader.onload = function (oFREvent) {
document.getElementById("uploadTextValue").value = oFREvent.target.result;
//oFREvent.target.result);
var decodedString = atob(oFREvent.target.result.split("base64,")[1]);
//data:text/html;base64,
//data:text/plain;base64,
//document.getElementsByTagName('xmp')[0].innerHTML = decodedString;//oFREvent.target.result;//;
var lines = decodedString.split('\n');
var fruits = [];
for(var i = 0;i < lines.length;i++){
var result = /dn: DC=(.*?),DC=corp.contoso.com,/.exec(lines[i])
if(result != null)
fruits.push(result[1])
}
//排序
fruits.sort();
//去重
var uniqueNames = [];
$.each(fruits, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});
//输出
for(var i = 0;i < uniqueNames.length;i++)
document.write(uniqueNames[i] + "<br>");
};
};
jQuery(document).ready(function(){
$('#viewSource').click(function ()
{
var text = $('#uploadTextValue').val();
alert(text);
//here ajax
});
});
</script>
<xmp></xmp>
<div>
<input type="hidden" id="uploadTextValue" name="uploadTextValue" value="" />
<input id="uploadText" style="width:120px" type="file" size="10" onchange="PreviewText();" />
</div>
<a href="#" id="viewSource">Source file</a>
</body>
</html>
javascript删除vbs源文件中的注释行
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
</head>
<body>
<script>
function PreviewText() {
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("uploadText").files[0]);
oFReader.onload = function (oFREvent) {
document.getElementById("uploadTextValue").value = oFREvent.target.result;
//oFREvent.target.result);
var decodedString = atob(oFREvent.target.result.split("base64,")[1]);
var string = "";
var lines = decodedString.split('\n');
var fruits = [];
for(var i = 0;i < lines.length;i++){
var temp = lines[i];
//去掉所有的空格(中文空格、英文空格都会被替换)
temp = temp.replace(/\s/g,"");
if(temp.indexOf("'")!=0)
//输出转换后的字符串
string = string+ lines[i];
}
document.getElementsByTagName('xmp')[0].innerHTML=string;
};
}
jQuery(document).ready(function(){
$('#viewSource').click(function ()
{
var text = $('#uploadTextValue').val();
alert(text);
//here ajax
});
});
</script>
<xmp></xmp>
<div>
<input type="hidden" id="uploadTextValue" name="uploadTextValue" value="" />
<input id="uploadText" style="width:120px" type="file" size="10" onchange="PreviewText();" />
</div>
<a href="#" id="viewSource">Source file</a>
</body>
</html>
对nmap结果进行处理
<script type="text/javascript">
var str = `
Nmap scan report for 10.50.195.1
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.50.210.35
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.50.210.40
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.50.211.25
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.50.245.2
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.52.54.239
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.52.240.134
Host is up (0.44s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.52.240.22
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.52.240.6
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.52.240.7
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.54.123.6
Host is up (0.46s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.54.123.7
Host is up (0.48s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.54.123.70
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.54.123.76
Host is up (0.46s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.54.124.7
Host is up (0.48s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.54.124.9
Host is up (0.46s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.54.163.155
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.54.163.157
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.55.167.85
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.59.5.208
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.61.2.128
Host is up (0.46s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.61.2.147
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.61.9.196
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.61.9.197
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.61.9.203
Host is up (0.47s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.63.158.6
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.63.158.7
Host is up (0.42s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.66.40.132
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.66.40.134
Host is up (0.44s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.66.40.144
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.66.40.250
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.75.67.12
Host is up (0.52s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.75.67.13
Host is up (0.51s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.78.122.90
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.60.59
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.100.200
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.111.120
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.121
Host is up (0.44s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.122
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.111.13
Host is up (0.46s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.131
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.141
Host is up (0.47s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.15
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.151
Host is up (0.47s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.161
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.111.172
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.111.173
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.111.19
Host is up (0.44s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.2
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.3
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.33
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.111.7
Host is up (0.44s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.112.103
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.112.104
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.112.105
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.112.106
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.112.107
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.112.97
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.112.98
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.113.7
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.113.9
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.120.10
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.189.54
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.189.56
Host is up (0.43s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.189.8
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.189.9
Host is up (0.45s latency).
PORT STATE SERVICE
445/tcp open microsoft-ds
Nmap scan report for 10.99.194.134
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.194.18
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.202.100
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.241.20
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.241.21
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.99.241.22
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.10
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.2
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.2
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.3
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.4
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.43
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.47
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.49
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.51
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.59
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.6
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.26.7
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.34.12
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.34.154
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.34.16
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.34.169
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.34.171
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.34.196
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.34.198
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.34.227
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.34.238
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.39.179
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.39.186
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.39.189
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.39.26
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.39.37
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.39.46
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
Nmap scan report for 10.100.39.50
Host is up (15s latency).
PORT STATE SERVICE
445/tcp closed microsoft-ds
`
//var oFReader = new FileReader();
//oFReader.readAsDataURL(document.getElementById("uploadText").files[0]);
//oFReader.onload = function (oFREvent) {
//document.getElementById("uploadTextValue").value = oFREvent.target.result;
//oFREvent.target.result);
//var decodedString = atob(oFREvent.target.result.split("base64,")[1]);
//data:text/html;base64,
//data:text/plain;base64,
//document.getElementsByTagName('xmp')[0].innerHTML = decodedString;//oFREvent.target.result;//;
var lines = str.split('\n');
var fruits = [];
for(var i = 0;i < lines.length;i++){
if(i%5 == 1) {
var result = lines[i].match(/(\d{1,3}\.){3}\d{1,3}/g);
var tttttttt =lines[i+3];
var tempp = tttttttt.indexOf("open");
if( tempp >= 0)
document.write(result[0] + "<br>");
}
}
</script>