I was always a restless student, always wanting to take advantage of opportunities like… for example, copying the semester exams from the teacher's USB stick or something like that. When I was in high school (computer major), I wanted to program "something" that would do the following:
- Detect when a USB device is connected to the classroom computer
- Copy all the content from that device to the computer
This would allow me to have the exam much earlier, it would only be necessary for the teacher to connect his USB memory (pendrive) to the computer and that's it.
Unfortunately in my institute the computers had Windows and… I never and never learned to program for this OS.
However now I use Linux (obviously not? LOL!), And with the knowledge that throughout these years I have been able to acquire, now YES! I can already achieve this with Linux 😀
In other words, I have programmed a simple script that does the following:
1. Create the folder /home/.USBDRIVES/
2. It is checking every 5 seconds if there is any USB device (or CD / DVD) connected to the computer.
3. In case one is connected, it will create a folder inside /home/.USBDRIVES/ with the name of the USB (for example: memory-2gb) and also, it will copy all the files .doc, .pdf, etc (here the list) to this folder you just created.
4. In case there is no USB connected, it will simply wait 5 seconds to start the check that I explained above again 😀
Here is the script and the file that contains the formats to be copied:
So that the script can work without any problems, it is best that it be started with administrative privileges (root), as it is obvious that it needs to be started together with the computer, in this way we make sure that if the computer is restarted or off, when I start the script again it will be there working, ready to extract from any USB what we want 😉
Let's open a terminal ... once open ...
1. Suppose we have both files in / Opt / (/opt/usb-spy.sh y /opt/usb-spy.files), we must give it execution privileges:
sudo chmod +x /opt/usb-spy.sh
2. We open the file /etc/rc.local :
sudo nano /etc/rc.local
3. In it we write above the final line (exit 0) the next:
/opt/usb-spy.sh &
4. Now we press [Ctrl] + [X] to save and exit the file, we press [S] or [Y] (depending on the system language) and then [Enter]. This will be enough for the changes we made to be saved.
And voila, this will be enough for the script to start as root when we turn on the computer.
It is important to verify that the file usb-spy.files is in the same directory as usb-spy.sh 😉
Now ... I'll explain a bit the inner workings of the script, as I know there are many Bash lovers around here por
Q: How does the script know that a USB device is connected? A: In the / etc / mtab file of our system are the devices or partitions that are mounted on our system. Through line 23 of the script it is known if there is a USB connected or not (making cat to mtab and grep media)
Q: Yes, but How does the script know if the cat and grep return any data to us or not? A: Through a if, then, else loop which starts on line 24.
Q: How to make it copy only files with desired extensions? (.doc, .pdf, etc)
A: Using rsync with various parameters, this is on line 34. Simply explained, with rsync I copy only the files that match the usb-spy.files filter, each line is a filter so to speak. I also pass the parameter --prune-empty-dirs
to rsync so it doesn't create empty directories for me.
There isn't much to explain actually, the script is really simple to understand 🙂
If anyone has any doubt or question, complaint, idea or suggestion please tell them and I promise to help them as much as I can.
Well, I was thinking of putting something like: «this script is for educational purposes only, do not use with harmful files»… But… what the hell !, use it for whatever you want, it's time that students have some kind of advantage against the teachers who oppress us 😀
regards
Cheater ¬¬
XDDDD
Nah at all ... if you sure thought at some point to do something like that ... HAHAHA.
You saved me from a pop quiz 😀… Now your «KZKG ^ Gaara» You are my god… Praise you!
Hello, very good script, but in mega the file no longer exists, you can send it to my email, please
Linux is 1% and you say that you never learned to program for windows so the tutoring is for a linux system, my question is: what is the possibility of finding a teacher that uses linux to carry out the tutoring?
This script or trick is "implemented" in a computer that does not necessarily have to be the personal or the professor's own, it is enough that it be any computer of a University or College, then it would only be possible to achieve by X or Y reason that the professor connects your USB on that computer.
Yes, it is for Linux, but many universities have Linux as a system in computer labs, as well as booting with a LiveCD and not necessarily installing Linux on the PC 🙂
About the 1%, I recommend you read this post: https://blog.desdelinux.net/debunking-the-1-percent-myth-traducido-al-espanol/
The possibility of a giant meteorite falling on the Atlantic in approximately 30 min
It looks interesting, it will have to be tried.
Thank you
The utility that it has is not a great thing, but the script as such is interesting ... because you can learn several tips from it, for example how to know the connected USBs, size of partitions, etc 🙂
An original and very well explained article KZKG ^ Gaara. Cheers!
Thank you
What audacity !!!, and you dare to write it on the blog OO, I don't know what to think, could it be that someone who doesn't like him well hacked Gaara?
impudence? why?
Although the purpose or motive of the script may not be as serious as the rest of my contributions, the script, its lines and programming logic does have a lot to contribute, I think.
You can take advantage of several things from this script:
1. How to know the size of a partition and make this a variable.
2. How to check if there is a USB connected and extract its path and name.
3. if-then-else and while loops.
Anyway, I don't think this is entirely useless or something.
I think you misinterpret my comment, I mean at the beginning of your article,
"I was always a restless student, always wanting to take advantage of opportunities like ... for example, copying the semester exams from the teacher's pendrive or something like that." If what I say bothers you, I hope you will excuse me, it was not my intention.
oO what an excellent blog, good articles, good readers, users apologize ... it is paradise lol free of trolls.
Thank you (I.e.
We are proud of what we have achieved so far, this community is truly proud ... it's great to be part of all this 😀
No no not at all, I didn't really bother at all xD
And yes hehe it was my mistake for misinterpreting your comment hehehe, sorry for that 😉
Nothing that the apologies come from me now, I will have to wash my face to see if I finish waking up that ... from what I see, I'm still not 100% HAHAHA.
Greetings friend 🙂
Very fine person xD
I would like my college computers to have Linux muhahahjaja.
Good article!
But whoever wants to imitate it in Windows will be easier, because where you don't have root access on the machine you want to use it, you can't leave it "forever". With Windows that does not happen 😉
Very good hehe.
The idea is good, it only has the weakness of having to use Root privileges
Well, you don't really need to be root as such ... if you declare to the script that the folder will NOT be /home/.USBDRIVES as I put it, and it says for example: /home/usuario/.USBDRIVES ... then executing the script with "user" will suffice 😀
It's a good idea, but I think you would save yourself modifying the rc.local and running it every minute using crontab, and thus you avoid the problem of running it as root and the rest, on the other hand, even if it does not affect much, you consume memory of the pc uselessly every 5 seconds to check if the usb is, even when it is not. Well that's my point of view
I mean, do a check every 1 minute, right? I didn't do it like that because… what if a USB is connected and disconnected in less than 1 minute? 😀
I preferred to do it every 5 seconds to make sure to really "capture" all USBs 😉
If I misunderstood you, please correct me 🙂
Greetings and welcome.
Yes, do the check every minute through cron, but well each one has a different way of seeing the problem, nothing more than looking at the code, it would not be better to check if the /home/.USBDRIVES folder already exists before creating it , is a suggestion nothing more
regards
Yes also, it is a detail that I noticed but ... I was lazy to solve it LOL!
Likewise, I could put a log where the files that have been copied are shown (individual log for each device), maybe even send this log by email (using some method of sending email by terminal of the ones I have put here on the site) ... but as I told you, I am a bit lazy ^ - ^ U
Hahahaha great.
AHA! So your software can be used for evil. LOL!!! 🙂
The trick is fine, but ... what if you want to unmount the USB before the script finishes copying its content? In that case it will complain that it is in use and it will be noticed that there is something wrong. 🙂
Actually, the system will not allow the USB to be unmounted because "something" will be using it 😉
Exact! And that's when Oscar's professor thinks he's found a linux virus. LOL!! 🙂
You made me remember once that I did something similar (on my Uni if we had linux / windows), but I made a C program that generated thousands of folders. I upholstered the USB to a guy who plugged it in. The computer center manager was amazed! He thought he found a virus in linuxx muajajajaja ... ahhh .. what times were those =)
and what is the difference with a virus?
That I remotely got into his USB and ran the program 😛
hahahaha, so it was a hack XD
😀
hehehehehe so you found a virus in Linux right? … LOL!!
It just doesn't work for me xD, if I try to run it from the terminal it tells me this: ./usb-spy.sh: line 31: [: -lt: unary operator expected
There seems to be something wrong with this line: if [$ USBSIZE -lt 15664800]; then
But I don't know what it is, because it doesn't create the .USB directory as well.
If it can be solved I can find a useful script.
Try replacing df with / bin / df and make sure to launch it as root.
Ok, now if it creates the .USB folder inside the home directory, but it keeps telling me through the terminal that: "./usb-spy.sh: line 31: [: -lt: unary operator expected" and it doesn't copy nothing, in that line there must be some conflict, I appreciate the help if someone sees it.
Thank you very much.
Can you give us the content of your / etc / mtab file when the USB is mounted?
Sure, here it goes:
/ dev / sda12 / ext4 rw, errors = remount-ro 0 0
proc / proc proc rw, noexec, nosuid, nodev 0 0
sysfs / sys sysfs rw, noexec, nosuid, nodev 0 0
none / sys / fs / fuse / connections fusectl rw 0 0
none / sys / kernel / debug debugfs rw 0 0
none / sys / kernel / security securityfs rw 0 0
udev / dev devtmpfs rw, mode = 0755 0 0
devpts / dev / pts devpts rw, noexec, nosuid, gid = 5, mode = 0620 0 0
tmpfs / run tmpfs rw, noexec, nosuid, size = 10%, mode = 0755 0 0
none / run / lock tmpfs rw, noexec, nosuid, nodev, size = 5242880 0 0
none / run / shm tmpfs rw, nosuid, nodev 0 0
binfmt_misc / proc / sys / fs / binfmt_misc binfmt_misc rw, noexec, nosuid, nodev 0 0
/ dev / sdb1 / media / DOCU403 vfat rw, nosuid, nodev, uid = 1000, gid = 1000, shortname = m $
The last lines are those of the mounted USB / dev / sdb1
Let's see if we got it. 🙂
Give us the output of the df command. And try adding the line:
echo $ USBSIZE
Right in front of the if that gives the problem and tell us what comes out when launching the script. It should look like this:
USBSIZE = `/ bin / df | grep $ USBDEV | awk {'print $ 2'} `
echo $ USBSIZE
if [$ USBSIZE -lt 15664800]; then
And just in case, also tell us what the output of the which df command is.
Ok, in parts, the df command returns this to me:
Filesystem 1K-blocks Used Available Use% Mounted on
/ dev / sda12 54082300 45246956 6125892 89% /
udev 2004028 4 2004024 1% / dev
tmpfs 805768 1180 804588 1% / run
none 5120 0 5120 0% / run / lock
none 2014420 92 2014328 1 XNUMX XNUMX% / run / shm
/ dev / sdb1 1023200 322256 700944 32% / media / DOCU 3
The command which df returns me: / bin / df
Then, I have put the echo $ USBSIZE, and the result is the same, it does not print anything new, but the error from before, the funny thing is that this happens with the USB that I am testing, if I put the 500GB external hard drive Yes, it returns the size of the disk on the screen, but the 500 exceeds the 16 of the if, therefore with the hard disk it does nothing.
But with USB it does nothing, other than show the error: ./usb-spy.sh: line 34: [: -lt: unary operator expected
It is as if it could not show the size of the USB, but with the df command I get its size.
Thanks for the help, let's see if we can get it!
Just in case the command did not look good here I leave a screenshot: http://i48.tinypic.com/j5dvn5.jpg
I see that in the mtab the path where it appears mounted is "/ media / DOCU403" while in df it appears "/ media / DOCU 3". This makes grep not find it and does not return the size. Try changing the first lines of the for so that they look like this:
for USBD in `cat / etc / mtab | grep media | awk '{print $ 1}' '';
do
USBDEV = `cat / etc / mtab | grep $ USBD | awk '{print $ 2}' ''
USBSIZE = `/ bin / df | grep $ USBD | awk {'print $ 2'} `
The changes consist of changing the name of the for variable to USBD, changing the $ 2 at the end of the for line to $ 1, inserting the line that begins with USBDEV just after the do and changing USBDEV to USBD in the line that begins with USBSIZE… I hope I didn't mess around. 🙂
The idea is to do the for by the name of the device instead of by the mount point.
The problem was that in the mtab when a device has spaces ... it puts some "strange" characters on the line, specifically in the place of the space.
Nothing, as simple to solve as changing the way $ USBDEV is designated, on line 28 change it and put it like this:
for USBDEV in `df | grep media | awk -F / {'print $5'}` ;
😀
Let me see if I can help you ...
The unary operator shows it because it does not locate the size of the disk, this happens because it does not check it, this happens because it is called
Document 3
and takes it as two values, if it were DOCU it would not open a problem
Maybe this will work for you
http://www.itimetux.com/2012/11/manejar-archivos-o-carpetas-con-espacios-en-unix.html
Greetings 🙂
Indeed that is the problem, because I have just tried with other USB that have a name with a word without spaces, example "classes" and the script works without problems, that's why it recognized the hard disk and not the memories, but now the thing is In knowing how I put the quotes in the script code, do I have to put them to "$ USBNAME"?
Many thanks to Atheyus and Hexborg for the help, it's almost done.
Actually semproms it's super simple to fix 😉
Line 28 ... change it to this:
for USBDEV in `df | grep media | awk -F / {'print $5'}` ;
I just did testing with this change and it works fine with devices whose label / name has a space :)
Now I will make the change in the script to download.
Very good. So it is easy to fix. 🙂
Ohhh, very ingenious 😀 I'll take note in case I need to ...
KZKG ^ Gaara, with that arrangement it works perfectly, now if I copy the USB with space in the name, thank you very much everyone for the help, I will try to see if I can make it run with the system boot and that way Check every cycle if there is any USB connected.
In the end we have achieved it xD.
(I.e. ... a pleasure hehe.
The problem was that I used mtab to recognize USB, I could have simply used df ... I used mtab because I thought it would be interesting to explain this file to them, to know it, but I did not foresee this error happening with devices with spaces lol.
Any questions or problems you leave it said, almost everyone here likes to help xD
regards
Of course. We are Linux users. We always get it. XD.
Install a daemon on the teacher's PC that generates a record of all the .doc, .docx, .odt, .pdf and sends them to you by mail. As well as scanning the PC from time to time in search of changes, new files or deletions in said registry, and anet any news will send you the modified or new file by mail as well.
Anyway, I think that more than a catch, running that script (AND AS ROOT !!!!) is suicide. You are making life and things 700% easier for anyone interested in inserting a file on your PC with root permissions and everything. Remember that extensions do not exist in Linux and that a .doc in Linux can be the same as a text, a video or even worse, a script (this time with very bad intentions).
Running it as root is not completely necessary, just define the folder where the content will be put somewhere that the running user has write permissions (for example, their own home) and that's it 🙂
On dangerous files ... well, you could add some lines that would chmod -x to all files, thus losing the execution property.
I have added these lines at the beginning to delete the .USBDRIVES folder if it was already created:
if [-s $ PLACE]
then rm -r $ PLACE
fi
This way, it will not give the error of "directory already existing", that is, if you have to be careful that there is nothing in the .USBDRIVES folder that we do not want to be deleted, because it will delete it and put another in its place.
To add new file formats, just write them to usb-spy? for example .jpg .mp3 etc.
To add new file types you put it in the usb-spy.files
Look at the content of the file and you will see how to add them, it is quite simple 😉
If we are on the same network, and there is a teacher's machine, the interesting thing would be to do a network mapping, we may have ssh on the machines and in that way distribute the code on the laboratory machines or even on the teacher's machine ... . This gives me soooo many ideas: $
Hello, I congratulate you for the post and all your contributions, but I have a question, to see if someone can solve this problem:
I was testing your script on a pc with ubuntu 12.04LTS (with Unity) and it worked perfect, but as I saw that it stopped executing this script after the first time it was run, because the .USBDRIVES folder was already created, I decided to directly use this other script of your authorship that was posted in another part of this forum
here I copy the script for you to locate yourself
#! / Bin / bash
#
# - * - ENCODING: UTF-8 - * -
# This program is free software. You can redistribute it and / or
# modify it under the terms of the General Public License
GNU # as published by the Free Software Foundation,
# either version 2 of said License or (depending on your
# choice) of any later version.
#
# If you make any modifications to this application,
# should always mention the original author of the same.
#
#Copyleft 2012, DesdeLinux.net {Havana City, Cuba}.
# Author: KZKG ^ Gaara
WHILE = 0
while [$ CONTROL = 0]; do
cat / etc / mtab | grep media >> / dev / null
if [$? -ne 0]; then
COUNTER = 0
else
COUNTER = 1
: $ {USBDEV: = `cat / etc / mtab | grep media | awk '{print $ 2}' `` »/»}
cp $ USBDEV / * / home /
fi
Sleep 5
done
exit 0
The topic is the following in ubuntu12.04 lts unity that last script runs fine and works but when I downgrade the same pc for "x" reason, and try to make it work in ubuntu 10.10 no, I can make it work
On the one hand, it throws me an error and does not read the pen drives that have a compound name for example: DATA-G, nor does it copy the files that have a compound name. Apart from this problem I have the problem that the script is only executed when the pc is restarted once.
The question would be: how can I make that script copy me pendrives and files with compound names
and if using crontab or can I schedule it to work at a certain time
Another detail, in Ubuntu 10.10, as it uses Gnome I would like to add some constraints to take into account, when giving execution privileges to a script, it must be located in /etc/init.d not in / etc /
(Take into account that you have to paste it there arriving from terminal with sudo nautilus)
On the other hand, another dimension would be that after pasting it there to see if it can be executed, it must be done in another terminal
sudo su (to be root)
Password
ls
cd / etc / init.d
Ls -l
and there we jump if it is running or not
then in the same terminal or in another terminal at another time being as root and being in etc / init.d (that is, until the step cd / etc / init.d) as in the previous terminal we continue as follows to give it permissions of execution
sudo chmod + x script.sh
update -rc.d script.sh defaults 80
reboot
I repeat that is so that it auto-executes in Ubuntu 10.10 if it is another version like 12.04 you have to do what KZKG ^ Gaara says
And one last question, how do I want to copy the content (up to 3 subfolders of the pendrive) to the home or to a hidden home folder or not, suppose it was .USBDRIVES
in the code I would not have to say
cp -r /media/*/*/*/home/.USBDRIVES/*
In short, to get what I want, how should the script stay?
Because I already tried to modify the line that they modified to usb-spy sh but to that other one that records directly in home and throws an error on the modified line.? Regards . Thank you
I can't download the second script
Very good. 😉
And if I don't do the steps you put in, what happens? Doesn't it run or does it just run but without root? If the computers have a system that we say restores all the internal configuration of the computer when shutting down, will it work? Thank you.
In the event that the system has 'something' that defaults to the / home / folder then you must modify the script, where it says / home / change it to / opt / or some other folder that is not affected.
I think that to improve the script and avoid the intrusion into the pendrive, the optimal thing would be to prioritize type, file size. For example, leave large files of 100 megabytes or more for last. Or first copy files doc, docx, txt, pdf, xml, ... etc etc etc and leave avi, mp4, mkv files for last ...
Hello, the idea is good. I wanted to ask two questions:
- Can it be done in reverse? make a scrip for the penderiver, that it connects to any machine and extracts files.
- works for Windows OS?
Hello,
I'm not sure how it could be done that when connecting the pendrive, the same pendrive self-executes a script that it has inside.
And no, this script does not work for Windows 🙂
Ok, thanks for answering me, I'll investigate how to do it
What a good script. I have been analyzing it, and it seems amazing to me, hopefully one day I could also make such practical scripts.
Excellent contribution, although the utility that I will give it is not exactly what you raised it for, if I use it for file backups of a group of users, misconduct that I have… hehehehe….
when I try to run it gives me this error line 31: [: too many arguments
remove that line to see if it worked. and it turns out that it is the line that restricts the size of the devices.
when removing it copies what I have in my mounted partitions 🙁
Hi, I'm new and I know the pot is old but I liked it a lot, your script is very educational
Thank you for sharing it and explaining it in detail ...
Wave
Friend you saved me, I just had to distract the teacher and introduce the usb to my laptop and then hahahaaj passed exam and notes from all hahaha
Could you pass me the script, the link is down
Can you pass me the script the link is down
kzkggaara, could you re-upload the files? the links are down: /, thank you very much
Can you upload links please?
hears! uncle! you have fallen praises you could raise them again!
OK! : v
well, I do this again because I don't know if what I wanted to say was uploaded….
well it is that you have the links down you can upload them!
The links are down !!!
Resubmit the links please @usemoslinux I am investigating about the subject, thanks!
The script can be downloaded from here. I think it's the same
https://mega.nz/#!yQR1BQTb!FoYoopZ11WSstQaqX1flxhm1t4jCKOI9jj8VIxIBrxk
Well then….
I think from my humble point of view that it is a great article and well if there are people who say that you are a cheater, then I think that it is according to the purpose for which you use the program.
I personally liked more for knowing the programming language for linux since I have only worked in java, thanks mate you have aroused my interest in knowing more about the language for linux.
I thank you.
Hello friends, because I found another way to use this script is by doing backup, I will talk to you a bit I have two devices, a camera and a hdd, what I want is that instead of recording the files, the local files save them in an external hdd from the camera to the hdd but it gives me an error that says "too many arguments" could you help me
It only works with devices with linux operating systems?
Someone raise it, please!
Hello, I do not have linux and in my school they use Windows, I used the app from usb capture and I copy only 5 files from the teacher's memory, I suspect that usb has protection, because the same thing happened twice, there will be some other way to subtract the complete information from that usb?
Well, if you say the code will be simple, it seems very interesting to see the methods work to see if a USB is connected or the size of the partition. Too bad they have removed the mega.nz script, could you upload it again?
Greetings and great work, simple but functional even to automate copies from USB !!!
Good!
I just entered your blog post, and I am really interested. What happens is that when you click on the .zip file that redirects you to mega, it means that the file is no longer available.
I was wondering if you could send me those files that are supposed to be downloaded.
Thank you very much and greetings!
Technology has a habit of shrinking in size as the years go by. And that has happened to an incredible degree with solid state drives (SSDs). Now you can get them in the same size as the fastest USB flash drives.
https://clongeek.com/las-unidades-usb-3-0-mas-rapidas/
Can you send me the file or upload it again? It comes out that cannot be downloaded