Tag Archives: ssh

useful functions for .bashrc/.zshrc

ssh domain.name will open tmux terminal on the remote side or reconnect to the old one.
Also it reconnects when get issue with connection.

1
2
3
4
5
6
7
8
9
10
11
12
function ssh () {
    if ! command -v autossh &> /dev/null; then echo "Install autossh"; fi
    AUTOSSH_POLL=20
    export AUTOSSH_POLL
    if [ $# -eq 1 ]; then
        autossh -M 0 $@ -t "tmux attach -t alter || tmux new -s alter";
    elif [ $# -gt 1 ]; then
        /usr/bin/ssh -A $@
    else
        /usr/bin/ssh -h
    fi
}

Insert to pastebin service your message:
$ echo 'test1' | pb
https://pb.idone.su/view/8f60b2b5
$ curl https://pb.idone.su/view/raw/8f60b2b5
test1

$ pb «test2»
https://pb.idone.su/view/32974896
$ curl https://pb.idone.su/view/raw/32974896
test2

1
2
3
4
5
6
7
8
function pb(){
    if [ -n "$1" ]; then
        echo "$1" | curl -d expire=1440 -d private=1 --data-urlencode text@- https://pb.idone.su/index.php/api/create
    else
        stdin="$(cat)"
        echo "$stdin" | curl -d expire=1440 -d private=1 --data-urlencode text@- https://pb.idone.su/index.php/api/create
    fi
}

Ubuntu: mosh

mosh (Mobile shell)

Ubuntu 14.04 LTS

1
2
3
4
5
6
7
8
9
10
11
12
echo >> /etc/apt/sources.list.d/mosh.list << EOF
deb http://ppa.launchpad.net/keithw/mosh/ubuntu trusty main
deb-src http://ppa.launchpad.net/keithw/mosh/ubuntu trusty main
EOF


apt-get update
get error about key(for example: 7BF6DFCD) copy it and insert
gpg --keyserver hkp://keys.gnupg.net --recv-keys 7BF6DFCD
gpg -a --export 7BF6DFCD | sudo apt-key add -

apt-get update
apt-get install mosh

OS X

1
brew install mosh

Usage

1
2
mosh example.com # it connects with current user on default ssh port(22)
mosh --ssh="ssh -p33" alter@example.com # it connects with user "alter" to port 33

autossh

Autossh is a great script which made for people who don't want to make «own wheel».

example script (from sources of autossh):

#!/bin/sh
#
# Example script to start up tunnel with autossh.
#
# This script will tunnel 2200 from the remote host
# to 22 on the local host. On remote host do:
#     ssh -p 2200 localhost
#
# $Id: autossh.host,v 1.6 2004/01/24 05:53:09 harding Exp $
#

ID=username
HOST=hostname.your.net

if [ "X$SSH_AUTH_SOCK" = "X" ]; then
eval `ssh-agent -s`
ssh-add $HOME/.ssh/id_rsa
fi

AUTOSSH_POLL=600
AUTOSSH_PORT=20000
AUTOSSH_GATETIME=30
AUTOSSH_LOGFILE=$HOST.log
AUTOSSH_DEBUG=yes
AUTOSSH_PATH=/usr/local/bin/ssh
export AUTOSSH_POLL AUTOSSH_LOGFILE AUTOSSH_DEBUG AUTOSSH_PATH AUTOSSH_GATETIME AUTOSSH_PORT

autossh -2 -fN -M 20000 -R 2200:localhost:22 ${ID}@${HOST}

ssh: reverse socks proxy

I have schema:

LocalNetwork (192.168.198.0/24) < — > myPC < — > GATE < — > RemoteNetwork (10.0.0.0/24)

In the LocalNetwork i have web-server (192.168.198.68)

myPC# ssh -D 1080 -R127.1:1080:127.1:1080 myPC ssh -R127.1:1080:127.1:1080 GATE ssh -R127.1:1080:127.1:1080 root@10.0.0.16

10.0.0.16# curl --socks4 127.0.0.1:1080 http://192.168.198.68

<html>

...

</html>

rsync via ssh

How to synchronize two folders via network:

test mode:

rsync -avrn --delete-after -e «ssh» source_folder/ user@destination_ip:/destination/folders/

work mode:

rsync -avr --delete-after -e «ssh» source_folder/ user@destination_ip:/destination/folders/

FreeRadius + pam + sshd

For example we will authorized on 192.168.0.233 with pam on ssh from radius server on 192.168.0.1.

192.168.0.1:
For different nix system installation of freeradius and pam_radius_auth.so is not so difficult.
for ubuntu
sudo apt-get install freeradius
fo gentoo
emerge freeradius

 

192.168.0.233:
For the next you will download «PAM Authentication and Accounting module» from:
http://freeradius.org/pam_radius_auth/

Or (in ubuntu):

apt-get install libpam-radius-auth


To install it doing
1. make
2. Copy 'pam_radius_auth.so' to /lib/security/pam_radius_auth.so

192.168.0.1:
Next we will tune settings of freeradius:
All of files are store in /etc/raddb in gentoo or /etc/freeradius in ubuntu.

vim clients.conf
...
client 192.168.0.233 {
secret = passwordko
}
...
EOF
Hear we will allow to auth 192.168.0.233 on radius with pass passwordko.


192.168.0.233:
vim /etc/pam_radius_auth.conf and /etc/radiusclient/servers

192.168.0.1    passwordko 1
EOF

vim /etc/pam.d/sshd ( comment all lines like auth )

auth       required     pam_radius_auth.so debug
EOF

Thats all. Now you can login with passes like on 192.168.0.1 machine by ssh on 192.168.0.233.

Note that logins will be equal on 192.168.0.233 and 192.168.0.1. If you have no login on 192.168.0.233 like equal login on 192.168.0.1 — you cant login.

Anybody know how?? Please tell me on g.link0ln@gmail.com. (languages: Russian/Einglish)