在 Linux 里,strings 本质就是一个命令行工具,所谓“脚本调用”通常是指在 Shell 脚本 / Python 脚本 / 其他程序 里执行 strings 命令并处理输出。下面按常见场景给你示例。
#!/bin/bash
strings /bin/ls
strings /bin/ls > strings.txt
strings -n 8 /bin/ls
for f in /bin/*; do
echo "=== $f ==="
strings "$f"
done
subprocessimport subprocess
result = subprocess.run(
["strings", "-n", "6", "/bin/ls"],
capture_output=True,
text=True
)
print(result.stdout)
import subprocess
with subprocess.Popen(
["strings", "/bin/ls"],
stdout=subprocess.PIPE,
text=True
) as proc:
for line in proc.stdout:
if "version" in line:
print(line.strip())
strings:
strings -n 8 mybinary > strings.txt
#include <stdio.h>
#include <stdlib.h>
int main() {
system("strings /bin/ls > out.txt");
return 0;
}
或用 popen 读取输出:
FILE *fp = popen("strings /bin/ls", "r");
char buf[256];
while (fgets(buf, sizeof(buf), fp)) {
printf("%s", buf);
}
pclose(fp);
| 参数 | 作用 |
|---|---|
-n N |
只显示长度 ≥ N 的字符串 |
-a |
扫描整个文件(默认已如此) |
-e |
指定字符编码(如 s, l, b) |
-t x |
显示偏移(十六进制) |
示例:
strings -n 10 -t x binary_file
如果你有具体场景(比如:
可以直接说,我可以给你更针对性的脚本。