ブロックデバイスで検索/変更しますか?

ブロックデバイスで検索/変更しますか?

私が持っていると仮定しましょうインストールされていません/dev/sdaに置き換えるブロックデバイスのすべてのMyPassWordインスタンスXXXXXXXXXX。 (私の目標が明確であることを願っています。)

これを行う最も簡単な方法は何ですか?

ベストアンサー1

次のことができます。

#! /usr/bin/env python

device = '/dev/sdi'
old_pattern = "MyPassWord"
new_pattern = "XXXXXXXXXX"

assert len (old_pattern) == len(new_pattern)

BS = 1024 ** 2  # 1 Mb buffer
# read a few bytes more to account for occurences of the pattern on the edge
READSIZE = BS + len(old_pattern)

offset = 0
with open(device, 'r+b') as fp:
    assert isinstance(fp, file)
    while True:
        try:
            fp.seek(offset)
        except IOError:
            #print 'offset', offset
            #raise
            break
        buf = fp.read(READSIZE)
        occurences = buf.count(old_pattern)
        if occurences:
            print offset, occurences
            fp.seek(offset)
            fp.write(buf.replace(old_pattern, new_pattern))
            fp.flush()
        offset += BS

上部で適切なデバイス名を変更します。

rootファイル内容のシステムバッファに変更が通知されないため、スクリプトを実行して完了したらデバイスを再マウントする必要があります。

おすすめ記事