在 Debian(或其他 Linux)系统里,strings 通常来自 binutils 包。所谓“脚本调用”,一般指 shell 脚本 或 其他语言脚本(Python 等) 去执行 strings 命令。
下面分几种常见方式说明。
which strings
如果没有:
sudo apt update
sudo apt install binutils
#!/bin/bash
strings /bin/ls
保存为 run_strings.sh:
chmod +x run_strings.sh
./run_strings.sh
#!/bin/bash
FILE="$1"
MIN_LEN="$2"
strings -n "${MIN_LEN:-4}" "$FILE"
使用:
./run_strings.sh /bin/ls 6
常用参数:
-n:最小字符串长度-a:扫描整个文件-e:指定编码(如 s=7-bit, l=16-bit little endian)strings -n 6 /bin/ls > strings.txt
grep "usage" strings.txt
import subprocess
result = subprocess.run(
["strings", "-n", "6", "/bin/ls"],
capture_output=True,
text=True
)
print(result.stdout)
如果你只是想要“提取可打印字符串”,也可以纯 Python:
import re
with open("/bin/ls", "rb") as f:
data = f.read()
strings = re.findall(rb"[ -~]{6,}", data)
for s in strings:
print(s.decode())
⚠️ 但功能不如 strings 全面(不支持编码、段分析等)
strings:
strings -n 6 ./mybinary > strings.txt
make strings
如果你有具体使用场景(比如:分析 ELF?提取某类字符串?在 CI 里用?),我可以给你更针对性的脚本。