USB 插入后,记录唯一标识符字符串,将驱动器格式化为 FAT32 并复制文件。 Bash 或 Python
这就是我想做的,
- 插入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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了自动检测插入的 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
orblkid
and using their output to actually grab the UUID. Formatting your drive could be done using something likemkfs -t vfat /dev/<your usb drive here>
.Hopefully these pointers help you solving your problem.