懒人小脚本

作为一个超过10次重复工作就要上脚本的程序员懒人
有必要记录一下那些因为懒而写出来的行行代码~

1. 把下载文件夹的最新文件移动到当前位置

因为经常要用tg传东西,就有了这个东西

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#! /bin/sh
name=`find ~/Downloads ~/Desktop/sync -type f -mmin -20 -printf "%T@ %p\n" | sort -n | tail -1 | cut -f2- -d" "`
if [ "$name" = "" ]
then
echo -e "\033[31mno latest file in 20m\033[0m"
exit
fi
echo -e "latest file: \033[36m$name\033[0m"
read -p "move?[y/n]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
mv "$name" `pwd`
echo 'moved'
fi

2. 用python连接到DHU-wifi

在没有图形化的linux里面链接wifi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /usr/bin/python3
import requests as req
import getpass
import re
import time
head = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.9 Safari/537.36','Accept':'text/html,application/xhtml+xml,application/xml;'}

data = {'password':'','username':'','savePWD':'on'}

t = req.get("http://detectportal.firefox.com/success.txt",headers = head,allow_redirects=False)
if(t.status_code == 200):
arubalp = re.search(r"&arubalp=(.*)'>",str(t.content,"utf-8"))
try:
arubalp = arubalp.groups()[0]
except(AttributeError):
if(t.content == b"success\n"):
print("\033[31;1malready connected to the network \033[0m")
exit()
else:
print("\033[31;1munexpect error \033[0m")
exit()
s = req.session()
t = s.get("http://detectportal.firefox.com/success.txt/&arubalp="+arubalp,headers = head)
portal = t.url[:t.url.find("dhu.edu.cn")]+"dhu.edu.cn/post.php"
else:
s = req.session()
t = s.get("http://detectportal.firefox.com/success.txt",headers = head)
portal = t.url[:t.url.find("dhu.edu.cn")]+"dhu.edu.cn/post.php"

if (data["username"] == "") or (data["password"] == ""):
data["username"] = input("userID:")
data["password"] = getpass.getpass("password:")
t = s.post(portal,data,headers = head,allow_redirects=False)
if(t.content == b""):
t = req.get("http://detectportal.firefox.com/success.txt")
if(t.content == b"success\n"):
print("\033[32;1mconnected! \033[0m")
else:
print("\033[31;1musername or password error\033[0m")
else:
print("\033[31;1m[error]:"+str(t.content,"utf-8")+"[0m")

3. 一键更换壁纸、锁屏、SDDM背景、GRUB背景

之前都是手动替换四个配置,所以写了这个脚本实现一键替换

几个问题

  1. 网上用org.kde.PlasmaShell.evaluateScript替换壁纸的脚本,好像不太能用
  2. 改变桌面壁纸、锁屏找没有到对应的API,但是可以直接替换目标文件之后重启来刷新缓存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#! /bin/sh
if [ x$1 == x ]; then
echo "changewall [wallpaper]"
else
wallpaper=$(realpath "$1")
convert $wallpaper -resize 2048 -quality 80 ~/Pictures/wallpaper/background.png > /dev/null 2>&1
if [ $? -eq 1 ]; then
echo "no such file or type not supported"
else
sudo cp ~/Pictures/wallpaper/background.png /usr/share/sddm/themes/plasma-chili/background.png
sudo cp ~/Pictures/wallpaper/background.png /boot/grub/themes/Vimix/background.png
sudo grub-mkconfig -o /boot/grub/grub.cfg
fi
fi

4. DDNS动态修改DNS的脚本 适用于dnspod

  1. 可以添加计划任务脚本来执行
  2. 日志会被记录到ddns.log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /bin/bash
login_token=""
domain=""
record_id=""
record_line_id=""
sub_domain=""

current_ip=$(curl https://ip.cn/ -s | awk '{print $3}')
old_ip=$(cat .old_ip)

if [ $old_ip != $current_ip ]; then
res=$(curl -s -X POST https://dnsapi.cn/Record.Ddns -d "login_token=$login_token&format=json&domain=$domain&record_id=$record_id&record_line_id=$record_line_id&sub_domain=$sub_domain&value=$current_ip")
echo $res > tmp.txt
printf "[%(%Y-%m-%d %H:%M:%S)T][change]ip to $current_ip\n" >> ddns.log
res_code=$(jq '.status.code' -r <<< $res)
if [ $res_code -eq 1 ]; then
echo $current_ip > .old_ip
else
res_message=$(jq '.status.message' -r <<< $res)
printf "[%(%Y-%m-%d %H:%M:%S)T][error]$res_message\n" >> ddns.log
fi
else
echo "not changed"
fi