Ubuntu: Shell script to check if something is plugged in the wifi USB port



Question:

I have a wifi access point that allows to plug a USB hard drive accessible from any computer sharing the same wifi. I would like to know if there was a way a shell script could test if something is plugged there or not.

If there is a way to check which USB key or hard drive is plugged would be a delight, but I'm not asking that much.


edit

The goal is to check if the drive is on before I can work with the files. I want to access the drive using samba. The IP is 192.168.0.1. I can get the name of the drive, but the IP is fix, whichever drive I plug.

NOTE: my final goal is to upload files on that drive with a shell script. I don't mind if you give me another way to do it: SSH, FTP, HTTP, morse code, …


Solution:1

You're probably not going to be able to figure out which drive it is without some fiddling around. More on that later.

Firstly, try to mount the device. If the mount fails, nothing is plugged in. Change the paths and options, obviously. (must be run as root, or with sudo)

mount -t cifs -o <options here> //192.168.0.1/share_name /mnt/whatever  if [[ $? = 1 ]]; then     echo "Drive not available."     exit 1;  fi  

If it succeeds, continue on with your copies to /mnt/whatever.

If you want to be able to tell which drive is inserted, one simple thing you could do is create a file in the root of the USB key or drive that identifies it. I have a Corsair USB key, for example, create an empty file called "CORSAIR" in the top directory of the key. Obviously, you could call it anything you want. Then, after mounting, test for the existence of the file. You could even unmount it at that point and mount it on another directory for further use.

if [[ -f /mnt/whatever/CORSAIR ]]; then      echo "CORSAIR USB key detected.."      # do some stuff  fi  if [[ -f /mnt/whatever/PERSONAL ]]; then      echo "Personal USB key detected..."      umount /mnt/whatever      mount -t cifs -o <options> //192.168.0.1/share_name /mnt/PERSONAL  fi  

Since you're mounting and unmounting, you'll have to run as root or use "sudo mount", which will prompt for passwords.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »