Below is my working script, running into one last issue. When I run op usb m yes the usb gets mounted but i get the below error, any thoughts?
### Error ###
root> op usb m yes
### Mounting USB ###
### USB Mounted ###
Traceback (most recent call last):
File "/var/db/scripts/op/automountusb1.py", line 115, in <module>
main()
File "/var/db/scripts/op/automountusb1.py", line 64, in main
mount_msdosfs: /dev/da2s1:
_p01 = Popen(["grep", _dev],stdin=_p0.stdout, stdout=None)
File "../../../../../../../src/dist/python/Lib/subprocess.py", line 702, in __init__
File "../../../../../../../src/dist/python/Lib/subprocess.py", line 1117, in _get_handles
ValueError
:
I/O operation on closed file
error: op script failed: /var/db/scripts/op/automountusb1.py
{master:0}
### Python Script ###
#!/usr/bin/env python2
"""
Mounts or UnMounts USB stick
Script takes a command line argument of m(mount) or u(un mount) and then attempts to
locate the current USB stick and either mount or un mount the USB stick
Args:
(m): mounts the USB stick
(u): unmounts the USB stick
Returns:
(NA)
Exceptions:
(NA):
Created By:
(Jason Mann)
"""
################################################
from subprocess import Popen, PIPE
import re #Regular Expressions
import argparse
################################################
def main():
_arguments = {'m': 'Enter yes to mount the usb stick', 'u': 'Enter yes to unmount the usb stick'}
_parser = argparse.ArgumentParser(description='Script will mount or unmount USB stick.')
#Define the arguments accepted by parser
# which use the key names defined in the arguments dictionary
for _key in _arguments:
_parser.add_argument(('-' + _key), required=False, help=_arguments[_key])
_args = _parser.parse_args()
# Dont want user to enter yes for both arguments since neither is mandatory
if _args.m == "yes" and _args.u == "yes":
print ("")
print ("### Can not have both arguments set to yes ###")
print ("")
# Mount the USB
if _args.m == "yes" and _args.u == "no" or _args.m == "yes" and _args.u is None:
print ("")
print ("")
print ("### Mounting USB ###")
# FreeBSD shell command that will list attached devices
_p0 = Popen(["camcontrol", "devlist"], stdout=PIPE)
# List will be used to find the FreeBSD dev device
_listofnum = ["1","2","3","4","5"]
_found = False
# Loop through the list to find the dev device, usually will be 1 but added up to 5
# just in case
for _num in _listofnum:
# FreeBSD shell command that greps the output from the _po command ran
# have to run them this way to do the pipe command
_dev = "da" + _num
_p01 = Popen(["grep", _dev],stdin=_p0.stdout, stdout=None)
_p01_1 = _p01.communicate()[0]
_p0.stdout.close()
if _p01_1 != "": # device found
# FreeBSD shell command that will retrieve the sn of the device
# Doing this to verify that the dev used was correct
_p1 = Popen(["camcontrol", "inquiry", _dev, "-S"], stdout=None)
_p2 = _p1.communicate()[0]
# FreeBSD shell command that will mount the USB stick
_devmnt = "/dev/da" + _num + "s1"
_p3 = Popen(["mount_msdosfs", _devmnt, "/var/tmp/usb"],stdout=None)
print ("")
print ("")
print ("### USB Mounted ###")
_found = True
if _found == False:
print ("")
print ("")
print ("### USB not Found ###")
print ("")
else:
print ("")
print ("")
print ("### Invalid arguments or argument values ###")
print ("")
print ("")
# UnMount the USB
if _args.u == "yes" and _args.m == "no" or _args.u == "yes" and _args.m is None:
print ("")
print ("")
print ("### Unmounting USB ###")
# We use /var/tmp/usb as the standard mount point for usb sticks
_p2 = Popen(["umount", "/var/tmp/usb"],stdout=None)
print ("")
print ("### USB Unmounted ###")
else:
print ("")
print ("")
print ("### Invalid arguments or argument values ###")
print ("")
print ("")
if __name__ == "__main__":
main()