r/dailyprogrammer • • Oct 27 '14
[10/27/2014] Challenge #186 [Easy] Admin Schmadmin
Description
"I'm sorry we had to call you in at such small notice but our last admin royally screwed us over. I don't suppose you can have a scout through the files and see if there's any remnants of that slimeball left in our system can you? Any leftover documents, programs, CV's, ANYTHING you can find about him, I need it so I can finish him."
A few weeks pass
...Congratulations!
You've been hired as a temp to do some administrative duties that involve digging through the records of the filesystem in search for any hints as to where the ex-employee may have fled to. But first, you'll need some training. I've assigned you a few simple tasks that should build your command line skills to that of an adequate admin.
Formal Inputs & Outputs
For this task, you are given a tasklist of tasks to perform. Each task has a bulleted point and a summary. The bulleted point contains the dialogue of what the manager wants you to perform, the summary can be seen as a sort of 'technical overview' of what needs to be done.
Input description
As input, you are expected to execute commands into your terminal that correspond to the given task on the tasklist.
Output description
The program should output the expected output of your command.
Tasklist
"Okay employee, I've hired you now get to work! Listen carefully to what I have to say, I'm not going to say it twice!..."
- "Bring up that list of his most used files, let's see what that scumbag's been up to!"
Summary : Get the 20 last used documents from the system and sort by the date they were modified.
- "Great, can you email that to me?"
Summary : Output the above command to a .txt file.
- "Hmm, still nothing. Maybe the answer is right in front of us? Get the last commands he used on the console!"
Summary : Retrieve the last 10 commands used on the console.
- "AHA, this looks good I'll just email it to my...what the? What's going on!..." 10 minutes later "He crashed our machine! I knew he had some software throttling our machines, find out what's causing it, and fix it!"
Summary : Get the 10 most CPU-heavy processes in descending order.
- "wait, wait, WAIT! Before you go any further. Let's look through the error logs! I won't be able to understand them and you don't have access to most of what's needed but if you could link them to my tech team, I'm sure they could figure it out!
Summary : Retrieve the last 20 error logs/messages and output these as a formatted HTML table
- "Okay, now we're getting somewhere. Let's put the nail in the coffin. Bruteforce it. Search every file, every directory, every nook and cranny for any .txt files, any .pdf and any .exe files"
Summary : Retrieve all txt/pdf/exe files on the machine (You do not need to do the whole machine, just 1 drive is enough, or less if your machine is struggling).
"Thanks kid, you saved our bacon! Now get out."
Notes/Hints
Beginners, consider using a shell environent for this. For windows I recommend Powershell. I'm not a Unix man but I hear the default shell is more than up to this task. Doing this in a programming language will prove to be a lot of work, choose a shell if you want your sanity.
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
Remember to check out our IRC channel. Check the sidebar for a link -->
12
u/Purp3L Oct 27 '14 edited Oct 28 '14
Bash scripting in Linux.
Task 1: List the 20 most recently modified files.
Task 2: Send that output to a text file.
Task 3: Find the last 10 commands used.
Task 4: Find the 10 most CPU intensive tasks.
Task 5: I chose to ignore the HTML, as Linux logs are generally in readable text.
The above command just lists the logs. To actually read the contents of each log file, you simply pipe that output into cat:
Task 6: Retrieve all txt/pdf/exe files.
Edit because grep -i isn't the same as --include.
Edit 2: sort -rk instead of sort -kr., thanks to /u/Simpfally for catching that.
Edit 3: Added cat to the log files task, as I wasn't actually reading them - only listing the files.
Edit 4: Changed head to tail in the ls-ltrR statements. As /u/Dirgon pointed out, with the -r option, the last entries would be the ones we want, not the first.
4
u/Simpfally Oct 27 '14 edited Oct 27 '14
Everything was achieved with bash on linux. (and a lot of google and man pages since I'm terrible with bash)
Task 1 :
(can change / to any directory, since / will return some uninteresting system files)
Task 2 :
I'm not sure what suitable for email mean though!
Task 3 :
(head -n 9 is just there to remove the the command we just entered from the history..)
Task 4 :
And it works..
Task 5 :
I don't really have any idea for this, there's no error in my dmesg, and I can't find a log file with errors in it.
Task 6 :
6
u/YuEnDee14 Oct 27 '14
Powershell on Windows 8! I honestly don't think I've ever really used Powershell before, so this was an entirely new experience for me and I thought it was great! I got to learn a bunch of new stuff, lay the foundation for a new skill for me, and have fun in the process, so that's for putting this challenge out there!
Tasks 1 and 2:
Task 3:
Task 4:
Task 5:
Task 6:
A few comments from me, I ended up borrowing a bit of my Task 5 command from /u/adrian17's solution, particularly the HTML formatting part, so thank you for that!
I also wasn't sure if I did Task 4 correctly, but it seems to be right according to this article:
As always, I'd really appreciate some feedback!
6
u/[deleted] Oct 27 '14
[Easy]
wat...
3
u/Reverse_Skydiver 1 0 Oct 27 '14
Here's my progress so far in Java:
As you can see, parts of this have not yet been completed. Is it possible to do parts 2 & 4 in Java on a Windows computer?
2
u/adrian17 1 4 Oct 27 '14 edited Oct 27 '14
Powershell (used very rarely, I had to look into help a lot):
1,2:
(If you're in a corporate network, I guess you could pass it to
Send-MailMessage.)3.
4. AFAIK PS can only show the cpu usage time (but not current load in %), so that's the best I can get in a one-liner:
5.
6.
4
u/[deleted] Oct 27 '14
Powershell on Windows.
Task one and two:
Task three:
Tragically, history like this is stored per session; if he closed his window, you're out of luck.
Task four:
Task five:
Application is just one of the Windows event logs; there are several others on my machine. You could create a script to go through all of them by piping their names to GetEventLog, like...
...You'd probably want to include some additional code to introduce a header or something in there for each log.
Task six:
I will point out that it is possible to make Powershell keep a persistent command history, but I'm much too lazy to do it. Anything I do often enough that I'd want to go back to it session after session is stored as a function in my profile.
2
u/[deleted] Oct 28 '14 edited Oct 28 '14
Done in Bash. Some are one-off commands, one requires killing a redirect manually, and another is a shell script.
Task 1
Task 2
Task 3
Task 4
Task 5
That will list the contents of the last 20 updated log files, not necessarily errors, though. I don't know where else to look, is stderr by default logged anywhere?
Task 6
I used find here rather than ls -R because it prints the entire path to the file, rather than just the file name.
Edit: Forgot the -t flag in my task 5 script.
2
u/dyslexic_captcha Oct 29 '14
Powershell Script This was a fun one. I wrote it as a script to be all in a single ps1 file with comments for each task.
2
u/lazydancer Oct 27 '14 edited Oct 28 '14
Great challenge, I use the terminal often but not to this depth. Please criticise, I am new to this.
Bash on Unix
1 and 2: Last used documents and print to file
3: Retrieve last commands
4: Most CPU-heavy processes
5: Retrieve the last 20 error log/messages
6: Search for all txt, pdf and exe files
1
u/KevMar Oct 30 '14
Powershell 4.0 on Windows 7
1
u/dmd Nov 10 '14
A lot of the answers here are using 'top' or 'ps' for checking CPU-heavy processes and looking at the %CPU column.
This is good, but note that these are instantaneous views of the system; a better solution would be to use
sa, which provides process-level accounting.