Archive for the ‘Linux tips’ Category

Using PSSH for executing parallel SSH commands

Tuesday, June 12th, 2012

I recently needed to check the date across all of our servers, so instead of SSHing into all of them and executing a command manually, I learned about the tool pssh.

Installing pssh on Mac OS X

Installing pssh isn’t available through brew, because it is available through PyPi and it has dependencies on a homebrew built Python. Luckily it is easy to get PyPi running with easy_install.

[~]=> sudo easy_install pip ... (lots of output) [~]=> sudo pip install pssh

You now have installed pssh!

Using pssh!

First I create a file containing a list of servers:

[~]=> cat servers
backend@server1.dk
backend@server2.dk
backend@server3.dk
backend@server4.dk
backend@server5.dk
backend@server6.dk
backend@server7.dk

And for running the command on the servers:

[~]=> pssh -h servers "date"
[1] 13:33:00 [SUCCESS] backend@server1.dk
[2] 13:33:00 [SUCCESS] backend@server2.dk
[3] 13:33:00 [SUCCESS] backend@server3.dk
[4] 13:33:00 [SUCCESS] backend@server4.dk 
[5] 13:33:00 [SUCCESS] backend@server5.dk
[6] 13:33:00 [SUCCESS] backend@server6.dk
[7] 13:33:00 [SUCCESS] backend@server7.dk

But to see the output of the server you need to use the -i argument:

[~]=> pssh -h benjamin-servers -i "date"
[1] 13:34:11 [SUCCESS] backend@server1.dk
Tue Jun 12 13:34:11 CEST 2012
[2] 13:34:11 [SUCCESS] backend@server2.dk
Tue Jun 12 13:34:11 CEST 2012
[3] 13:34:11 [SUCCESS] backend@server3.dk
Tue Jun 12 13:34:11 CEST 2012
[4] 13:34:11 [SUCCESS] backend@server4.dk
Tue Jun 12 13:34:11 CEST 2012
[5] 13:34:11 [SUCCESS] backend@server5.dk
Tue Jun 12 13:34:11 CEST 2012
[6] 13:34:11 [SUCCESS] backend@server6.dk
Tue Jun 12 13:34:11 CEST 2012
[7] 13:34:11 [SUCCESS] backend@server7.dk
Tue Jun 12 13:34:11 CEST 2012

If you are unfortunate enough to run a couple of servers, without having a centralized keystore, or manage the server with puppet, you can add a public key to multiple servers with pssh as well:

[~]=> pssh -h benjamin-servers -i “echo ‘ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGcYH4Cg+PWnYFMUMFXDOAiC+hoN/N3frBstJC5+lMQoq5gJQ4nEb0zzS+5SyLaqwbD5SqByOAY/KynRYtrYLpyJwzRUKD7VoyL5wkahkeMeJvKs7b5tmom970mDMtRgyEuePUaVj0y4basdphmEbRjLwvuQSgNwQdf3g5pVoLXntk0VY4xtE9nkSftaB5EJkoSmzOJT/PwivGwMDNq31/yP4nkYZIUjOpOe2hF3aeFJCL+3xvpkI3tYVRCzqJZHLssiwQM5EK3HiOi4wWuvmOc1LJA5FATe04hyMCUopkwdZO2dGQVpI38IVrD1yKg6okwiq2C+B4HuKsbZ8seAeG5D+i9wh this@is.a.key’ >> /usr/home/backend/.ssh/authorized_keys”

Enable .bashrc on OSX Snow Leopard (and Lion)

Wednesday, October 19th, 2011

Unfortunately OSX doesn’t use .bashrc per default, but it is easy to enable it. To do it, open up the file /etc/profile with root privileges like this:

sudo nano /etc/profile

And add the following line:

[ -r $HOME/.bashrc ] && source $HOME/.bashrc

[ -r $HOME/.bashrc ] && source $HOME/.bashrc

Save, and now your .bashrc file should work like you expect.

Downloading files from SSH over an unstable connection

Sunday, October 9th, 2011

As mentioned earlier I am doing a lot of traveling at the moment, which means that I am mostly on a mobile connection.

I needed to download a 100Mb file from my Linux server, and with scp the download would fail.

Instead I am now using this rsync command that would resume if the connection drops:
rsync –partial –progress –rsh=ssh root@rauw.dk:filename.exe filename.exe

Also, if you are using SSH on a slow connection, typing things make lag, you can use this command:

rlwrap -a ssh user@address