5 Linux Commands: timeout, cpulimit,awk,tar and youtube-dl

I know how much you love random linux commands so here I’ve compiled some cool random linux commands to copy, convert, limit,kill and redirect things.

Start COMMAND, and kill it if still running after 5 seconds

timeout 5s COMMAND

Convert Youtube videos to MP3

youtube-dl -t --extract-audio --audio-format mp3 YOUTUBE_URL_HERE
youtube-dl has this functionality built in. If you’re running an older version of youtube-dl, you can update it using `youtube-dl -U` (although if you have an older version, it probably doesn’t download youtube videos anyway.)

youtube-dl –help will show you other options that may come in useful.

Limit the cpu usage of a process

sudo cpulimit -p pid -l 50
This will limit the average amount of CPU it consumes.

Target a specific column for pattern substitution

awk '{gsub("foo","bar",$5)}1' file
Awk replaces every instance of foo with bar in the 5th column only.

Redirect tar extract to another directory

tar xfz filename.tar.gz -C PathToDirectory
The command extracting the tar contents into particular directory …

Linux Command Line and MySQL: Awesome

MySQL is the world’s most popular open source database. Whether you are a fast growing web property, technology ISV or large enterprise, MySQL can cost-effectively help you deliver high performance, scalable database applications. Check out this site MySQL Commands for a nice MySQL cheat sheet.
UrFix.com  however has created a list of commands I use almost daily when monitoring and maintaining my LAMP server. I hope you find these useful…

Monitor the queries being run by MySQL


watch -n 1 mysqladmin --user= --password= processlist

Watch is a very useful command for periodically running another command – in this using mysqladmin to display the processlist. This is useful for monitoring which queries are causing your server to clog up.
More info here: http://codeinthehole.com/archives/2-Monitoring-MySQL-processes.html

Backup all MySQL Databases to individual files


for I in $(mysql -e 'show databases' -s --skip-column-names);
do mysqldump $I | gzip > "$I.sql.gz"; done

I put this in a cron job to run @ midnight – “lazy back up”

Copy a MySQL Database to a new Server via SSH with one command


mysqldump --add-drop-table --extended-insert --force --log-error=error.log
 -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost "mysql -uUSER -pPASS NEW_DB_NAME"

Dumps a MySQL database over a compressed SSH tunnel and uses it as input to mysql – i think that is the fastest and best way to migrate a DB to a new server!

Convert all MySQL tables and fields to UTF8


mysql --database=dbname -B -N -e "SHOW TABLES"  | awk '{print "ALTER TABLE", $1,
 "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;"}' | mysql --database=dbname

Backup a remote database to your local filesystem


ssh user@host 'mysqldump dbname | gzip' >
/path/to/backups/db-backup-`date +%Y-%m-%d`.sql.gz

I have this on a daily cronjob to backup the urfix.com database from NearlyFreeSpeech.net (awesome hosts by the way) to my local drive. Note that (on my Ubuntu system at least) you need to escape the % signs on the crontab.

Export MySQL query as .csv file


echo "SELECT * FROM table; " | mysql -u root -p${MYSQLROOTPW}
databasename | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > outfile.csv

This command converts a MySQL query directly into a .csv (Comma Seperated Value)-file.

Create an SSH tunnel for accessing your remote MySQL database with a local port


ssh -CNL 3306:localhost:3306 user@urfix.com

Count the number of queries to a MySQL server


echo "SHOW PROCESSLIST\G" | mysql -u root -p | grep "Info:" |
awk -F":" '{count[$NF]++}END{for(i in count){printf("%d: %s\n",
count[i], i)}}' | sort -n

dump a single table of a database to file



And there you have it, a nice list of hopefully useful commands that you can inspect and learn from.

8 Cool Ways To Use SCP

The SCP protocol is a network protocol, based on the BSD RCP protocol, which supports file transfers between hosts on a network. SCP uses Secure Shell (SSH) for data transfer and utilizes the same mechanisms for authentication, thereby ensuring the authenticity and confidentiality of the data in transit. A client can send (upload) files to a server, optionally including their basic attributes (permissions, timestamps). Clients can also request files or directories from a server (download). SCP runs over TCP port 22 by default. Like RCP, there is no RFC that defines the specifics of the protocol.

SCP is an awesome tool. Learn it, Love it, Use it….

Edit a file on a remote host using vim

vim scp://username@host//path/to/somefile

Colored diff ( via vim ) on 2 remotes files on your local computer.

vimdiff scp://root@server-foo.com//etc/snmp/snmpd.conf scp://root@server-bar.com//etc/snmp/snmpd.conf

Restrict the bandwidth for the SCP command

scp -l10 user@urfix.com:/home/urfix/* .

the command is obvious, I know, but maybe not everyone knows that using the parameter “-l” you can limit the use of bandwidth command scp.
In this example fetch all files from the directory zutaniddu and I copy them locally using only 10 Kbs

Compare a remote file with a local file

vimdiff  scp://[@]/

Easily scp a file back to the host you’re connecting from

mecp () { scp "$@" ${SSH_CLIENT%% *}:Desktop/; }

Place in .bashrc and invoke like this: “mecp /path/to/file”, and it will copy the specified file(s) back to the desktop of the host you’re ssh’ing in from. To easily upload a file from the host you’re ssh’ing in from use this:

ucp (){ scp ${SSH_CLIENT%% *}:Desktop/upload/* .; }

scp file from hostb to hostc while logged into hosta

scp user@hostb:file user@hostc:

While at the command line of of hosta, scp a file from remote hostb to remote hostc. This saves the step of logging into hostb and then issuing the scp command to hostc.

Copy something to multiple SSH hosts with a Bash loop

for h in host1 host2 host3 host4 ; { scp file user@$h:/destination_path/ ; }

Just a quick and simple one to demonstrate Bash For loop. Copies ‘file’ to multiple ssh hosts.

scp with compression.

scp -C /path/to/backup.sql

-C is for compression.

7 Awesome and Free Network Apps for Rooted Androids

Did you root your Android-based smartphone or tablet, thinking about it? Well I present you seven free network apps that take advantage of the superuser permissions. In the list I’ve composed , you’ll find apps that help with the network configuration and security of your device, apps to do sharing and sniffing on networks, and apps that demonstrate network hacking and security risks.

DroidSheep by Andreas Koch

What is this about?
If you know Firesheep or Faceniff, you probably know what this is about – one-click session hijacking using your android smartphone or tablet computer.
DroidsheepYou can use DroidSheep to demonstrate to yourself or others of how easy it is to take-over other user’s unsecured web sessions. You just open the app, agree to their disclaimer, and tap Start. You’ll start seeing the list populate once users on the network login to unsecured sites, displaying their URLs and session IDs. Then you can tap on an entry to open the site in full or mobile view, save the cookies, or export the cookies via email.





Samba File Sharing by funkyFresh

Samba file sharing

Access your Android device over wifi as a Windows shared folder.* Samba filesharing server for Android.
* Your Android device becomes visible to other Windows (& SMB compatible) computers on your network.
* The ‘external’ storage in your Android device is accessible as a network shared folder.
* Copy files and folders to and from your Android device using drag and drop.
* Customizable username, password, Windows workgroup name, and device network (NETBIOS) name.

* Your Android device must already be rooted.
* Superuser or later installed (available on Market).

Set DNS by Mytechie

set dns

This app lets you easily define the name servers used by your Wi-Fi and mobile network interfaces. You can speed up browsing, enable content filtering, or bypass DNS-based filters by using a third-party DNS service. Android lets you natively change the DNS server addresses for just the Wi-Fi interface and is only available if you define static IP information. However, Set DNS lets you change both interfaces and can work when using DHCP.






Shark for Root by Elviss Kuštans

shark for root Traffic sniffer, works on 3G and WiFi (works on FroYo tethered mode too).his network sniffer is based on tcpdump and is basically a simple version of the popular WireShark application. It captures packet information from the Wi-Fi or 3G interfaces and automatically saves them to a .pcap file on the SD card. You can input tcpdump parameters to customize the sniffing sessions. You can view the dumps on your Android if you install another app (Shark Reader) or transfer the .pcap file to another computer to open in an application such as WireShark.



SSH Tunnel by Max Lv

sshtunnel for android This app helps you surf the web privately and securely, for instance, to encrypt your traffic from local eavesdroppers at Wi-Fi hotspots or to bypass filters and geographic restrictions. It’s a SSH client that can proxy the Internet connections for the entire Android system or individual apps through a SSH server. Its DNS Proxy feature can also solve the DNS pollution problem in places such as China by using the pre-defined HOSTS information from the developer’s site to counteract any DNS tampering by the authorities or ISPs.




WiFi Key Recovery by alt236

Android lets you modify the settings for Wi-Fi networks you’ve saved, but doesn’t show you the existing passwords, similar to Windows XP. However, this simple app reveals the login credentials for Wi-Fi networks stored on your device, useful if you forget the password to a network. It also demonstrates one of the security risks if you’re Android gets lost or stolen; someone can potentially get the credentials to log onto your home or work network.




Wireless Tether by Müller, Lemons, Buxton

wireless tether for androidThis lets you share your Android’s Internet connection with other computers or devices equipped with Wi-Fi or Bluetooth. It’s great if your Android version or mobile service provider doesn’t support tethering. For most Android devices, this app’s Wi-Fi sharing creates an ad-hoc (peer-to-peer) network. Some devices support the regular infrastructure-mode, which appears to look like a real Wi-Fi router or access point to the other devices. For either mode, it takes care of handing out IP addresses to clients via DHCP.