Skip to content

Instantly share code, notes, and snippets.

@crazyboycjr
Last active April 17, 2017 11:34
Show Gist options
  • Save crazyboycjr/40e47aca2ba94ecfb866fc07fefa6605 to your computer and use it in GitHub Desktop.
Save crazyboycjr/40e47aca2ba94ecfb866fc07fefa6605 to your computer and use it in GitHub Desktop.
自动连接寝室电脑并连接ss
[Unit]
Description=Network Auto Connect Tool
DefaultDependencies=no
After=sshd.service NetworkManager.service
[Service]
Type=simple
RestartSec=3
Restart=on-failure
ExecStart=/home/cjr/Developing/autoconnect/auto-connect.sh
ExecReload=/home/cjr/Developing/autoconnect/auto-connect.sh
[Install]
WantedBy=default.target
#!/bin/bash
my_inet_ip='0.0.0.0';
path=`dirname $0`;
dorm_ip='10.147.xxx.xx'
function ensure_inet()
{
ip addr show wlp2s0 | grep inet 2>&1 >/dev/null;
while [ $? -ne 0 ]; do
sleep 1
my_inet_ip='0.0.0.0';
ip addr show wlp2s0 | grep inet 2>&1 >/dev/null;
done
if [[ $my_inet_ip == '0.0.0.0' || -z $my_inet_ip ]]; then
echo :$my_inet_ip:
my_inet_ip=`curl tsinghua-server:23333 --retry 3 --retry-delay 1`;
#my_inet_ip=`socat TCP:tsinghua-server:23333 -`;
echo :$my_inet_ip:
fi
}
function monitor()
{
ensure_inet;
ps aux | grep "ssh -fN -D 8087" | grep -v 'grep' 2>&1 >/dev/null && \
ps aux | grep 'sslocal -c /etc/shadowsocks/config_server6.json' | grep -v 'grep' 2>&1 >/dev/null;
}
function sshexp()
{
addr=$1
kill `ps aux | grep 'ssh -fN -D 8087' | grep -v 'grep' | awk '{print $2}'`
$path/ssh-conn.exp $addr;
if [ $? -ne 0 ]; then
echo err: $addr
fi
killall sslocal;
}
function ping_22()
{
nc $1 22 -z -w 1
}
function connect()
{
ensure_inet;
ipv6_header=`ip -6 addr show wlp2s0 | grep inet6 \
| awk '{print $2}' | cut -d ":" -f1 | grep -v fe80 | head -n1`;
ipv4_addr=`drill @tsinghua-server home.us A | sed -n 's/^home.us.*A\t\(.*\)/\1/p'`;
ipv6_addr=`drill @tsinghua-server home.us AAAA | sed -n 's/^home.us.*AAAA\t\(.*\)/\1/p'`;
if [ $ipv4_addr = $my_inet_ip ]; then
ipv4_addr='cjr-Lenovo';
fi
if [[ $ipv6_header == '2001' || $ipv6_header == '2002' ]]; then
sshexp $ipv6_addr;
sslocal -c /etc/shadowsocks/config_server6.json &
else
ping_22 $dorm_ip
if [ $? -eq 0 ]; then
sshexp $dorm_ip;
else
sshexp $ipv4_addr;
fi
proxychains sslocal -c /etc/shadowsocks/config_server6.json &
fi
sleep 10;
}
while [ 1 ]; do
monitor;
while [ $? -eq 0 ]; do
sleep 5;
monitor;
done
connect;
done
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/5 * * * * /home/cjr/scripts/sendip/sendip.sh UDP tsinghua-server 23333
*/5 * * * * /home/cjr/scripts/sendip/sendip.sh UDP cjr-server 22223
*/5 * * * * /home/cjr/scripts/sendip/sendip.sh UDP cjr-server4 22223
*/5 * * * * ipv4=`head -n1 /home/jrchen/scripts/ipaddr.txt` && sed -i "s/\tIN\tA\t.*/\tIN\tA\t${ipv4}/" /var/named/home.us.b && ipv6=`tail -n1 /home/jrchen/scripts/ipaddr.txt` && sed -i "s/\tIN\tAAAA\t.*/\tIN\tAAAA\t${ipv6}/" /var/named/home.us.b && /usr/sbin/rndc reload home.us
import socket, sys
import ipaddress
from threading import Thread
class MiniServer:
h = ''
p = ''
m = []
def __init__(self, port, mode):
self.h = '0.0.0.0'
self.h6 = '::'
self.p = int(port)
self.m = mode
self.passphrase = open('passphrase.txt', 'r').read()
def tcp4(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "TCP4 Socket Created......."
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((self.h, self.p))
print "Wating for connecting......."
sock.listen(50)
while True:
cl, addr = sock.accept()
print "Connected from: ", cl.getpeername()
cl.sendall(str(addr[0]) + '\n')
cl.close()
def udp4(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((self.h, self.p))
print "UDP6 Server Start"
while True:
data, rinfo = sock.recvfrom(1024)
print "Receive from ", rinfo, " and The Data send from The Client is :", data
sock.sendto(rinfo[0] + '\n', rinfo)
self.handle(rinfo[0], data)
def udp6(self):
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((self.h6, self.p))
print "UDP6 Server Start"
while True:
data, rinfo = sock.recvfrom(1024)
print "Receive from ", rinfo, " and The Data send from The Client is :", data
sock.sendto(rinfo[0] + '\n', rinfo)
self.handle(rinfo[0], data)
def handle(self, addr, data):
'''
verify and save my ipv4 adn ipv6 addr to ipaddr.txt
'''
if (not data.startswith(self.passphrase)):
return
if (ipaddress.ip_address(unicode(addr)).version != 4):
return
addr_v6 = data[len(self.passphrase):]
fout = open('ipaddr.txt', 'w')
fout.write(addr + '\n')
if (len(addr_v6) > 1):
fout.write(addr_v6)
fout.close()
if __name__ == "__main__":
if (len(sys.argv) <= 2):
print '%s <port> <methods>' % sys.argv[0]
sys.exit(1)
x = MiniServer(sys.argv[1], sys.argv[2:])
threads = []
for m in x.m:
if m == 't4':
threads.append(Thread(target=x.tcp4))
elif m == 'u4':
threads.append(Thread(target=x.udp4))
elif m == 'u6':
threads.append(Thread(target=x.udp6))
if (len(threads) == 0):
raise(1)
for t in threads:
t.start()
for t in threads:
t.join()
#!/bin/bash
proto=$1
host=$2
port=$3
ipv6=`ip addr | grep inet6 | grep -v fe80 | grep -v ::1 | awk '{print $2}' | head -n1` && ipv6=${ipv6%%/*} && echo -e "This is the happiest moment of my life.\n${ipv6}" | socat - $proto:$host:$port
#!/usr/bin/expect -f
set ip [lindex $argv 0 ]
set timeout 5
spawn ssh -fN -D 8087 $ip
expect {
"*yes/no" { send "yes\r"; exp_continue}
}
expect eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment