Skip to content

Instantly share code, notes, and snippets.

View zhanglintc's full-sized avatar
🐦
Working from home

Lane zhanglintc

🐦
Working from home
View GitHub Profile
@zhanglintc
zhanglintc / each_file.py
Last active November 30, 2019 09:24
yield each file's path in a given folder
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
def each_file(target):
for root, dirs, files in os.walk(target):
for f in files:
yield os.path.join(root, f)
@zhanglintc
zhanglintc / each_file.pl
Last active December 30, 2015 15:18
return a list which contains each file's path
use strict;
use Cwd;
use File::Spec;
use File::Basename;
sub each_file {
my $givenFolder = shift @_;
our @pList; # path list to be returned
&innerEachFile($givenFolder);
@zhanglintc
zhanglintc / each_file.rb
Last active January 4, 2016 14:20
return a list which contains each file's path
require 'Find'
def each_file target
Find.find(target) do |path|
if not File.directory? path
yield path
end
end
end
@zhanglintc
zhanglintc / wanted_file.pl
Last active November 1, 2018 14:49
traverse a given dir(wanted version)
use 5.016;
use File::Find;
find(\&wanted, $target);
sub wanted {
my $path = $File::Find::name;
say $path;
}
@zhanglintc
zhanglintc / sshtunnel.py
Last active December 31, 2019 15:32
SSHTunnelForwarder example
# sudo pip install sshtunnel
import sshtunnel
with sshtunnel.open_tunnel(
# jumpserver host, port. port 22 in general(ssh port)
("jumpserver_host", 22),
# username for jumpserver
ssh_username="jumpserver_username",
@zhanglintc
zhanglintc / paramiko.py
Created November 1, 2018 14:43
paramiko example
import paramiko
# create ssh clinet
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("domain", 22, "username", "password")
# execute ssh command
cmd = 'echo $HOSTNAME'
stdin, stdout, stderr = ssh.exec_command(cmd)
@zhanglintc
zhanglintc / logger.py
Created November 6, 2018 06:45
logging format
import logging
LOG_LEVEL = logging.DEBUG
formatter = logging.Formatter('%(asctime)s - %(name)s:%(lineno)-4s - %(levelname)s - %(message)s')
logger = logging.getLogger("logger.py")
handler = logging.FileHandler(LOG_FILE)
logger.setLevel(level=LOG_LEVEL)
handler.setLevel(level=LOG_LEVEL)
handler.setFormatter(formatter)
@zhanglintc
zhanglintc / autossh.sh
Last active September 21, 2023 04:38
Reverse Proxy (autossh)
sudo autossh -f -M 999 -N -o "PubkeyAuthentication=yes" -o "StrictHostKeyChecking=false" -o "PasswordAuthentication=no" -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -R 0.0.0.0:9091:localhost:9091 -i /home/lane/.ssh/id_rsa lane@vultr.zhanglintc.work
sudo autossh -f -M 888 -N -o "PubkeyAuthentication=yes" -o "StrictHostKeyChecking=false" -o "PasswordAuthentication=no" -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -R 0.0.0.0:2222:localhost:22 -i /home/lane/.ssh/id_rsa lane@vultr.zhanglintc.work
@zhanglintc
zhanglintc / MonkeyPatch.py
Last active May 23, 2019 11:09
Monkey patch context manager
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class MonkeyPatch(object):
def __init__(self, original, patch):
self.original = original
self.patch = patch
self.cache = None
@zhanglintc
zhanglintc / thread_run.py
Created May 16, 2020 09:08
decorator make funciton thread runable
def thread_run(func):
import functools
import threading
@functools.wraps(func)
def wrapper(*args):
threading.Thread(target=func, args=(*args,), daemon=True).start()
return wrapper