USB 插入后,记录唯一标识符字符串,将驱动器格式化为 FAT32 并复制文件。 Bash 或 Python

发布于 2024-09-04 05:16:34 字数 893 浏览 2 评论 0原文

这就是我想做的,

  • 插入USB闪存驱动器。
  • 安装它。
  • 将唯一标识符字符串记录到文件中。
  • 将驱动器格式化为 FAT32。
  • 将文本文件复制到驱动器。
  • 卸载它。
  • 卸下驱动器。

30次

情况是这样的,我买了30个U盘。我需要格式化每个设备以确保它们干净,我需要每个设备的唯一字符串。我需要在每个文件上放置相同的txt文件。

我不擅长编写脚本,但可以阅读并遵循 bash 和 python。

任何指示将不胜感激。

编辑

感谢您的回复。

这是我到目前为止在 Windows 中所得到的。

我使用 nirsoft.net 的 USBDeview 选项>高级选项> “插入 USB 设备时执行以下命令”并使用以下命令“python getserial.py %serial_number%”,

getserial.py 脚本将从 USBDeview 传递的 %serial_number% 放入文本文件中,然后将文件复制到USB 设备。

import sys
import shutil

sourceFile = "C:\\^READ ME.txt"
destinationFile = "E:\\^READ ME.txt"

f = open('serials.txt', 'a')
f.write(sys.argv[1] + '\n')
f.close()

from time import sleep

sleep(3)

shutil.copyfile(sourceFile, destinationFile)

仍然对可以做到这一点的完整脚本感兴趣,但我认为目前这超出了我的能力。

This is what I want to do,

  • insert USB flash drive.
  • mount it.
  • record uniquie identifer string to a file.
  • format the drive to FAT32.
  • copy a text file to the drive.
  • unmount it.
  • remove the drive.

30 times

The situation is this, I have bought 30 usb drives. I need to format each one to ensure they are clean, I need the unique string from each device. I need to put the same txt file on each one.

I am not great at writing scripts but can read and follow bash and python.

Any pointers would be appreciated.

edit

Thank you for your resposes.

Here is what I have got so far, in windows.

I used USBDeview from nirsoft.net
options > advanced options > "execute the following command when you insert a USB device" and used the following command "python getserial.py %serial_number%"

the getserial.py script puts the %serial_number% passed from USBDeview into a text file, then copies a file to the USB device.

import sys
import shutil

sourceFile = "C:\\^READ ME.txt"
destinationFile = "E:\\^READ ME.txt"

f = open('serials.txt', 'a')
f.write(sys.argv[1] + '\n')
f.close()

from time import sleep

sleep(3)

shutil.copyfile(sourceFile, destinationFile)

Would still be interested in a full script that could do this but I think it is beyond my capabilities at the moment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁憨憨 2024-09-11 05:16:34

为了自动检测插入的 USB 闪存驱动器,您可以使用 autofs。不幸的是,插入设备时它无法运行脚本,否则其他步骤可以很容易地执行。

因此,您需要检测 autofs 是否安装了新的闪存驱动器。 crontab 可能是定期检查磁盘是否已安装的解决方案如果是这样,您的步骤可以执行。唯一的事情是检测您是否已经处理了安装的磁盘(即磁盘是否是新的)

为了找到 UUID,您可以查看 ls /dev/disk/by-uuid 或 blkid 并使用它们的输出来实际获取 UUID。可以使用诸如 mkfs -t vfat /dev/ 之类的方法来格式化您的驱动器。

希望这些提示可以帮助您解决问题。

In order to automatically detect an inserted USB flash drive, you could use autofs. Unfortunately it is not able to run a script when a device is inserted, otherwise the other steps could be performed quite easily.

So, you need to detect that autofs mounted a new flash drive. crontab might be a solution to periodically check whether a disk is mounted and if so your steps could be performed. Only thing is to detect whether you already processed the mounted disk or not (ie the disk is new or not)

In order to find the UUID you could take a look at ls /dev/disk/by-uuid or blkid and using their output to actually grab the UUID. Formatting your drive could be done using something like mkfs -t vfat /dev/<your usb drive here>.

Hopefully these pointers help you solving your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文