Useful little things in the work of a web developer or “How could I live without it”

An evil Trojan stole my account for Habr, after which some stupid cartoons began to be published under my account. Unfortunately, I only found out about this when the UFO turned me into read-only, and the rating went to a negative value. It doesn’t matter: the reason to finally write a post that has been going for a long time.

A web developer needs a console, but not so much as to drop everything and start reading thick Linux books. That is why I learned console tricks from case to case and, judging by my employees, many do the same. I will reveal a couple of convenient secrets, without which I can no longer live.

1) Use ssh keys, Luke!


I discovered the keys for a long time, although people regularly meet for whom they are new. The ssh keys allow you to configure the connection once and no longer store passwords for all sites in the “ notepad”. We agree on the standard key location: /home/user/data/.ssh/id_dsa, Enter (or do not enter) passphrase. It’s better to enter it: the system will remember your password from login to logout, that is, you do not need to enter this password constantly. But security will increase by an order of magnitude. After that, we get two files: ~ / .ssh / id_dsa and ~ / .ssh / id_dsa.pub. The first is a personal (private) key - it is better to copy it to a USB flash drive and hide it in reserve. The second is the public key, and we will be reporting it to all our servers.
$ ssh-keygen -t dsa






The easiest way to transfer the key to the server is to execute this command in your console: and for the last time enter the password for the SSH of the remote computer
$ ssh user@hostname "umask 077; cat >> .ssh/authorized_keys" < ~/.ssh/id_dsa.pub



2) Use ssh configs, Luke!


Everything works fine, but you need to enter long logins and hostnames each time. It is necessary to optimize!
We edit the file ~ / .ssh / config, add: We check the rights to the file ~ / .ssh / config, if they allow someone other than us to write, we change it to others: Let's say the computer to which we want to connect is behind the nat. We need to log in via SSH to one server, then go from it to the desired computer. If you need to do this many times a day, it will very, very quickly get bored. We write the new rule in the config: That's all! Now we can write ssh computer.hostname, the user will be set up automatically, the connection will be established directly with the desired computer. The main thing is not to forget him, too, put his public key.
Host host
User user
Hostname hostname


$ chmod 644 ~/.ssh/config



Host computer.hostname
Hostname 192.168.1.10
User user
ProxyCommand ssh hostname nc %h %p


In addition, I will describe two useful directives.
LocalForward localhost:8080 192.168.10.10:80 #Пробрасывать удаленный порт к себе каждый раз, когда происходит подключение по SSH.
Port 8022 #указать порт SSH сервера, удобно когда он живет не на стандартном порту.

3) Power in auto-completion


Enter four host letters each time? This is tiring! As a rule, autocompletion parses ssh-config files, it is enough to start writing a hostname and clicking on a tab so that the host name is added automatically. If this does not happen, you need to teach this bash.
Add a line to the ~ / .bash_profile file There you can add the following code: You can also enter mysqldump in the same way. After we open a new bash console, our console will supplement the name of the remote computer and the name of the local database! If you have a password to connect to the database, you need to take the next step.
complete -W "$(echo `cat ~/.ssh/config | grep -iE '^(Host|HostName) ' | awk '{print $2}'`)" ssh




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





4) Do not enter the password for the console muscle


Each time you launch the mysql or mysqldump console client, you must remember to pass it the username and password. To avoid this, it is enough to once and for all create a ~ / .my.cnf file with the following contents: Add the mysqld section as desired. It allows you not to worry about the 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 automatically be sent to the less command. On which you can conveniently navigate vertically and horizontally and even do a search!
[client]
user = 'root'
password = 'password'

[mysql]
pager = less -iMSx4 -FX



5) Results:


To get a database dump from a remote server, you had to run a series of commands before. In the worst case (the example is based on real events): Now, instead of all this horror, it is enough to execute one command: In reality, it’s even less, because before each command you can press tab: ssh com [tab] mysqldu [tab] lon [tab ] | mys [tab] lon [tab] No desire to send the file unpacked? It does not matter, we will pack on the fly from the other side into a zip, and from this side - unpack. As additional buns, they got the opportunity to download files directly to themselves from a remote computer behind the nat, without re-saving them in the dorg. If the topic seems interesting to the community, I will continue.
localhost $ ssh -P 8022 user@hostname #идем на сервер
hostname $ ssh user2@computer #идем на удаленный комп
computer $ mysqldump -u root -p password long_database_name > ~/filename.sql
computer $ exit
hostname $ scp user2@computer:~/filename.sql ~/filename.sql
hostname $ ssh user2@computer
computer $ rm ~/filename.sql
computer $ exit
hostname $ exit
localhost $ scp -P 8022 user@hostname:~/filename.sql ~/filename.sql
localhost $ ssh -P 8022 user@hostname
hostname $ rm ~/filename.sql
hostname $ exit
localhost $ cat ~/filename.sql | mysql -u root -p password long_database_name
localhost $ rm ~/filename.sql #залили наконец, удаляем


$ ssh computer mysqldump long_database_name | mysql long_database_name





$ ssh computer 'mysqldump long_database_name | gzip' | gunzip | mysql longdatabase_name


$ scp computer:~/test.txt ~/



I can talk about how to configure iTerm under a poppy, that it would be extremely convenient to work with ssh.
About how bash scripting can save a lot of time when working with the command line.
About the pros of the screen command and how to configure it conveniently
. Also about forgotten grandfather z- modem and how it can help a modern developer in everyday life.

Also popular now: