Archive

Archive for the ‘bash’ Category

increase sudo timeout

February 12, 2017 Leave a comment

Problem
You want to increase the sudo password remember timeout.

Solution
Run “sudo visudo“, then add this line to the end:

Defaults    timestamp_timeout=60

Where 60 means 60 minutes. If you specify -1, then the password is never forgotten (which is not a good idea IMO).

Tip from here.

Categories: bash, linux Tags: ,

Don’t paste blindly to your terminal

January 27, 2017 2 comments

Most of the time when we see a code snippet online to do something, we often blindly copy paste it to the terminal.

Well, the copied text may contain some malicious code. Here is a post about that with a demo.

Categories: bash Tags: , ,

VPN list

January 23, 2017 Leave a comment

Autovpn is a nice and simple project to easily connect to a VPN in a country of your choice.

If you need a VPN list, you can find the necessary URL in its source.

Here is a Python implementation too (link).

Categories: bash Tags: ,

grab a Twitch video in mp3

January 17, 2017 Leave a comment

Problem
You want to grab a Twitch video in mp3. For instance, you want to listen to it offline.

Solution
You need two programs for it: youtube-dl and ffmpeg. Let’s take a concrete example:

$ youtube-dl -g "https://www.twitch.tv/wearethevr/v/115335579"
https://vod067-ttvnw.akamaized.net/v1/AUTH_system/vods_c631/wearethevr_24261824064_585034506/chunked/index-dvr.m3u8
$ ffmpeg -i "https://vod067-ttvnw.akamaized.net/v1/AUTH_system/vods_c631/wearethevr_24261824064_585034506/chunked/index-dvr.m3u8" -f mp3 out.mp3

Where https://www.twitch.tv/wearethevr/v/115335579 is the URL of this particular Twitch video.

I wrote a script for it to automate the whole process: twitch2mp3.

Categories: bash, python Tags: , ,

git: revert “git add .”

September 7, 2016 Leave a comment

Problem
You run “git add .” and you manage to add 100+ files :( How to “unadd” them with one command?

Solution

git reset HEAD --

I found it here. Click on it for a detailed explanation.

Categories: bash Tags: ,

MongoDB: connect remotely

August 8, 2016 Leave a comment

Problem
I have a Digital Ocean VPS running MongoDB. There is a web application on this machine that is on port 80. MongoDB is hidden from the outside world and can only be accessed internally. There is also an SSH port where I can log in.

How to connect to my MongoDB server from home? Say I want to use a graphical client, e.g. MongoChef. The client runs on my home machine and I want to connect to MongoDB with it on my DO VPS. How to do that?

Solution
I found the solution here. In short: we connect securely to our database through an SSH tunnel.

Make sure that:

  • you can SSH into your Mongo droplet
  • your MongoDB is bound to localhost

For connecting, I use this script:

REMOTE_SSH_PORT=1234
LOCAL_PORT=2345
REMOTE_MONGO_PORT=27017
cmd="ssh -p ${REMOTE_SSH_PORT} -L ${LOCAL_PORT}:localhost:${REMOTE_MONGO_PORT} user@your.remote.ip"
echo "#" $cmd
echo "# connect on your home machine to port ${LOCAL_PORT}"
echo "# example:    mongo --port ${LOCAL_PORT}"
$cmd

The default SSH port is 22, but it’s a good idea to change it. With the command “ssh -p ${REMOTE_SSH_PORT} user@your.remote.ip” I could log in to my VPS. However, MongoDB was not accessible from outside, thus executing “mongo --host your.remote.ip --port ${REMOTE_MONGO_PORT}” failed.

The SSH tunneling above works as follows. On your home machine you open the port ${LOCAL_PORT} that is connected to your remote machine via the SSH port ${REMOTE_SSH_PORT}, and the connection is tunneled to localhost:${REMOTE_MONGO_PORT}, where localhost means the remote machine where we logged in with SSH.

So, when you execute the script above, you’ll have to log in to your remote machine via SSH. Then open a new terminal and type “mongo --port 2345” and voilá, you are connected to MongoDB on your remote machine!

If you use a Mongo client (e.g. MongoChef), then simply create a new connection and specify localhost with port 2345. Connect, and you are in.

It works as long as you are logged in in a terminal via SSH. When you log out, the local port closes that is tunneled to your remote machine.