Web development
Raiting:
6

Useful stuff for a web developer or How could I live without it?


The web developer needs a console, but not that much that he/she should drop everything and start reading thick books about Linux. That's why I have learned occasionally some console tricks, and many of my coworkers do the same. I will reveal a few useful secrets without which I cannot live anymore.

1) Use ssh keys!


I discovered the keys a long time, but regularly there are people who never heard about them. SSH keys allow adapting the connection once, and then passwords do not have to be stored to all sites in a notepad.
$ ssh-keygen -t dsa
We accept a standard key location: /home/user/data/.ssh/id_dsa,
Then we enter (or not enter) passphrase, but it is better to enter it: the system will remember the password for a login and logout, namely there is no need to enter the password constantly, and the security will increase in times.
After that we will get two files: ~/.ssh/id_dsa и ~/.ssh/id_dsa.pub.
The first is a private key – this one is better to copy and hide it as a reserve. The second key is a public, and this one will be given to all our servers.

The easiest way to transfer the key to the server is to run this command from a console:
$ ssh [email protected] "umask 077; cat >> .ssh/authorized_keys" < ~/.ssh/id_dsa.pub
and the last time to enter the password to SSH remote computer.

2) Use ssh configurations!


Everything works fine, but each time we must enter the long logins and hostnames. It is necessary to optimize!
Edit the file ~/.ssh/config, add:
Host host
User user
Hostname hostname

Then we check the permissions for a file ~/.ssh/config, if they allow to write someone else other than us, we change them:
$ chmod 644 ~/.ssh/config
Let’s suppose the machine to which we want to connect is outside NAT. We need to go through SSH to one server, then using it we go to the right computer. If this operation should be done many times a day, it may annoy us very quickly.
We write a new rule in the configuration:
Host computer.hostname
Hostname 192.168.1.10
User user
ProxyCommand ssh hostname nc %h %p

That's it! Now we can write ssh computer.hostname, the user will be replaced automatically, and the connection will be established directly with the right computer. The main thing is to remember to put a public key.
In addition, I will give two useful guidelines:
LocalForward localhost:8080 192.168.10.10:80 #forward a remote port every time when there is the connection through SSH.
Port 8022 #specify the port of SSH server, it is convenient when it does not live on the standard port.

3) Strength is in autocompletion


It's exhausting to enter each time four-letter host! Typically, the autocompletion parses the files of ssh configuration, but it is enough to start writing a hostname and press a tab in order to be completed the hostname automatically. If it does not happen, I will teach how to do that.
Add the line
complete -W "$(echo `cat ~/.ssh/config | grep -iE '^(Host|HostName) ' | awk '{print $2}'`)" ssh
in the file ~/.bash_profile

There also we can add the following code:
function __mysql_list_all_opts {
local i IFS=$'\n'
mysql --help|egrep '^ -'|awk '{print $1 "\n" $2}'|egrep '^-'|sed s/,$//|sort
}

__mysql_all_opts=
function __mysql_compute_all_opts {
: ${__mysql_all_opts:=$(__mysql_list_all_opts)}
}

function _mysql_complete {
local cur prev opts

COMPREPLY=()
cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]}

case $prev in
*)
if [[ "$cur" == -* ]]; then
__mysql_compute_all_opts
opts=${__mysql_all_opts}
else
opts=$(mysql -uroot -s -e 'show databases')
fi
;;
esac

COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
COMPREPLY=( $(compgen -W "$opts" -- $cur) )
}

complete -F _mysql_complete mysql

Similarly, we can write for mysqldump

Once we open a new console bash will add the remote computer name and the name of a local database in our console!
If we have a password to connect to the database, we need to do following step.

4) Do not enter the password for the console muscles


Each time we run the console client mysql or mysqldump, we should remember to give it the username and password. In order to avoid that it is needed to create a file ~/.my.cnf with the following contents:
[client]
user = 'root'
password = 'password'

[mysql]
pager = less -iMSx4 -FX

Section of mysqld could be added as desired. It will allow us not to suffer from an optimal selection of the limit, when working with the database from the command line. If the output is longer than the number of lines on the screen, the output will be automatically sent to less, where we can conveniently move vertically and horizontally, and even do a search!

5) Resume:


In order to get a database dump from a remote server we previously had to perform a number of kompand. In the worst case (this example is based on real events):
localhost $ ssh -P 8022 [email protected] #we go to the server
hostname $ ssh [email protected] #we go to a remote computer
computer $ mysqldump -u root -p password long_database_name > ~/filename.sql
computer $ exit
hostname $ scp [email protected]:~/filename.sql ~/filename.sql
hostname $ ssh [email protected]
computer $ rm ~/filename.sql
computer $ exit
hostname $ exit
localhost $ scp -P 8022 [email protected]:~/filename.sql ~/filename.sql
localhost $ ssh -P 8022 [email protected]
hostname $ rm ~/filename.sql
hostname $ exit
localhost $ cat ~/filename.sql | mysql -u root -p password long_database_name
localhost $ rm ~/filename.sql #finally, we remove it

Now, instead of all this horror it is enough to execute a single command:
$ ssh computer mysqldump long_database_name | mysql long_database_name
In reality, it is even less, because before each command, we can press a tab:
ssh com[tab] mysqldu[tab] lon[tab] | mys[tab] lon[tab]
If there is no desire to send as unpacked file. Do not worry; we will pack it on the fly from the one side in the zip, and from this one to unpack.
$ ssh computer 'mysqldump long_database_name | gzip' | gunzip | mysql longdatabase_name
Additionally, we were able to download files directly from the remote computer outside NAT without resaving them on the way.
$ scp computer:~/test.txt ~/
If this topic seems interesting to the community, I will continue my writing. Just leave your comments on UMumble!
BumBum 15 may 2012, 12:46
Vote for this post
Bring it to the Main Page
 

Comments

Leave a Reply

B
I
U
S
Help
Avaible tags
  • <b>...</b>highlighting important text on the page in bold
  • <i>..</i>highlighting important text on the page in italic
  • <u>...</u>allocated with tag <u> text shownas underlined
  • <s>...</s>allocated with tag <s> text shown as strikethrough
  • <sup>...</sup>, <sub>...</sub>text in the tag <sup> appears as a superscript, <sub> - subscript
  • <blockquote>...</blockquote>For  highlight citation, use the tag <blockquote>
  • <code lang="lang">...</code>highlighting the program code (supported by bash, cpp, cs, css, xml, html, java, javascript, lisp, lua, php, perl, python, ruby, sql, scala, text)
  • <a href="http://...">...</a>link, specify the desired Internet address in the href attribute
  • <img src="http://..." alt="text" />specify the full path of image in the src attribute