Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
ayuLiao / scp.py
Last active December 11, 2018 08:21
scp因为安全考虑,每次使用前需要输入密码,但我希望脚本自动化时,可以自动输入密码 此时可以使用python的pexpect自动输入密码
'''
path 本地文件夹路径,scp使用了-r
user 服务器的用户名
passwd 服务器的密码
tpath 上传到服务器的路径
'''
def scp(path, user,ip, passwd, tpath):
passwd_key = '.*assword.*'
shell = 'scp -r {path} {user}@{ip}:{tpath}'.format(
path=path, user=user, ip=ip, tpath=tpath
@ayuLiao
ayuLiao / iptables.sh
Last active December 6, 2018 08:46
centos6的防火墙,iptables的关闭与启动以及状态查看
# iptables操作,两种操作
# 立即生效
开启: service iptables start
关闭: service iptables stop
# 重启生效
@ayuLiao
ayuLiao / sys.py
Last active November 22, 2018 06:02
python程序中使用 import XXX 时,python解析器会在当前目录、已安装和第三方模块中搜索 xxx,如果都搜索不到就会报错。
'''
加入上层目录和绝对路径
'''
import sys
sys.path.append('..') #表示导入当前文件的上层目录到搜索路径中
sys.path.append('/home/model') # 绝对路径
from folderA.folderB.fileA import functionA
'''
@ayuLiao
ayuLiao / mysqlconnect.sql
Created November 15, 2018 12:58
查看、修改MySQL最大链接数
mysql root@localhost:(none)> show variables like '%max_connections%'; #查看mysql最大链接数
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set
Time: 0.017s
mysql root@localhost:(none)> set global max_connections=1000; #将最大链接修改成1000
Query OK, 0 rows affected
@ayuLiao
ayuLiao / requests.py
Last active January 4, 2019 02:34
requests 简单用法
'''
Get function
'''
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
print(r.text)
# 如果返回信息是json
print(r.json())
'''
@ayuLiao
ayuLiao / modify_permiss.py
Last active November 14, 2018 07:38
python修改文件或文件夹权限
def modify_permiss(self, path, name,R=True):
'''
change file or directory permission
:param name: linux user name\
:param R: Recursive modify permission
:return:
'''
idshell = 'id %s'%(name)
code, output = self.execshell(idshell)
if 'No such' not in output:
@ayuLiao
ayuLiao / pythonandshell.py
Last active December 15, 2018 07:33
python执行shell脚本有多种方法,这里展示个人常用的两种 1. 使用 python的 command库 2.使用subprocess 库
'''
使用commdans执行shell命令
这种方式不会创建独立进程去执行shell命令,即python程序退出,shell命令启动的程序也就退出了
这种方式适合于短时任务,即不是后台进程型任务
python2才会有
'''
def execshell(self, shell):
@ayuLiao
ayuLiao / programrun.py
Last active November 9, 2018 03:41
python使用shell命令查看程序是否允许
def execshell(shell):
exit_status, output = commands.getstatusoutput(shell)
if int(exit_status) == 0:
print('%s execute success!'%shell)
else:
print('%s execute error: exit_status [%s] err [%s]' % (shell, str(exit_status), output))
# if shell error , exit python program
exit()
return exit_status, output
@ayuLiao
ayuLiao / get_today.py
Last active November 8, 2018 06:58
python获得当天时间戳的差值
def get_today():
'''
Get today timestamp
:return:
'''
today = datetime.date.today()
today_timestamp = int(time.mktime(today.timetuple()))
now_timestamp = int(time.time())
# yesterday = today - datetime.timedelta(days=1)
# yesterday_timestamp = int(time.mktime(yesterday.timetuple()))
@ayuLiao
ayuLiao / sql2.py
Created November 8, 2018 01:40
python执行sql,当遇到该sql语句报错没有该表时自己创表 适用于表结构相同单纯表名不同的系统
@debug
def sql2(self, sql):
def execsql2(db_con,cur,sql):
'''
if you first close the conn then use ping or any other operating, you
will get error about : err.InterfaceError("(0, '')").
so , do any operate before the close operating
'''
#reconnect MySQL