[Request] explain temporary files - mktemp
Welcome, Guest.

ANNOUNCEMENT:

Click the CHAT button above for connection details. Show up early, seating is limited, late entries will not be registered.
Make sure you show up for game night! We have prize giveaways just for being there! Anyone can win!

Author Topic: [Request] explain temporary files - mktemp  (Read 1757 times)

Offline JayVii

  • Contributor
  • *******
  • Posts: 1759
  • Country: de
    • JayVii's Page
[Request] explain temporary files - mktemp
« on: June 19, 2016, 08:56:24 AM »
So Ben (a former Administrator on here) posted a script somehwere in the forum to block ads with your /etc/hosts-file. Back then I didn't quite understand what this thing really does, but only few weeks later I got more into bash and scripts and got a rough idea.

Well, today i'm much further into it and understand pretty much most shell-script i look at.
Also, I'm still maintaing and occasionally updating Ben's old script on one of my You are not allowed to view links. Register or Login.

The script makes use of temporary files ("Selfdestruct") to store adblock-lists in, before inserting them into your /etc/hosts using the mktemp command.
So yes, I completely understand what this command does (also read rough explainations on it You are not allowed to view links. Register or Login.

What I would like to know, though is:
Why does he use mktemp:


Code: You are not allowed to view links. Register or Login
Selfdestructinghosts=$(mktemp)
wget -O - https://URL/TO/ADSERVERLIST1.txt >> $Selfdestructinghosts
wget -O - https://URL/TO/ADSERVERLIST2.txt >> $Selfdestructinghosts


instead of simply writing it all to a file manually like so:
Code: You are not allowed to view links. Register or Login
wget -O - https://URL/TO/ADSERVER-LIST1.txt > /tmp/adserverlist
wget -O - https://URL/TO/ADSERVER-LIST2.txt >> /tmp/adserverlist
or maybe:
Code: You are not allowed to view links. Register or Login
touch /tmp/adserverlist
wget -O - https://URL/TO/ADSERVER-LIST1.txt >> /tmp/adserverlist
wget -O - https://URL/TO/ADSERVER-LIST2.txt >> /tmp/adserverlist

Is it just "good practice" or are there any other benefits to it?


EDIT: If you need to see the script, to understand my question, see You are not allowed to view links. Register or Login (Line 18-19, Line 23-45 & the deletion of the temporary files in Line 54)


Thanks for explaining in advance :)
You are not allowed to view links. Register or Login

Offline misko_2083

  • Newbie
  • **
  • Posts: 10
  • Country: cs
Re: [Request] explain temporary files - mktemp
« Reply #1 on: April 25, 2017, 04:14:19 AM »
The script is run as root. If someone reads your script and does

Code: You are not allowed to view links. Register or Login
ln -s /etc/passwd /tmp/adserverlistAnd you write to that temp file, it's going to be "ouch". It's better to have unpredictable file name. The attacker would have to have phisycal access to your computer but it's still a good practice.