day12_kls

课程内容

shell脚本参数处理getopts

shell脚本模板

必须有的

脚本描述

脚本使用方法

参数处理

依赖检查

日志

模板可能需要的东西:

配置文件处理

调试模式

代理相关

分类

按协议:

http,https,socks

按功能:

正向代理,反向代理

按匿名性:

透明代理,匿名代理,高匿名代理

代理模式

系统代理,规则代理

实例&&作业

docker-tool

自动安装docker,添加代理,镜像等

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
# discription:
# a docker tool, intall script, config proxy, mirror
# author: lzf
# feature:
# 1. docker and docker-compose auto install(support centos,ubuntu,debian,kali)
# 2. proxy registry-mirrors insecure-registries auto config, via jq
help(){
echo "$0 install/i | proxy/p url | mirror/m url | mirror-addtrust/mt url | add-trust/at url | rm-stoped/rs | compose"
echo -e "\t install \t\t install docker"
echo -e "\t proxy url \t\t add proxy"
echo -e "\t mirror url \t\t add registry-mirrors"
echo -e "\t add-trust url \t\t add insecure-registries"
echo -e "\t mirror-addtrust url \t\t add registry-mirrors and insecure-registries"
echo -e "\t compose \t\t install docker-compose"
exit 0
}
init(){
source /etc/os-release
if command -v jq >/dev/null 2>&1 ;then
jq_exist=1
fi
case $ID in
centos | ubuntu | debian | kali)
;;
arch)
echo "正在编写代码,暂不支持$ID"
#exit 1
;;
*)
echo "Error: $ID is not supported."
exit 1
esac
# 整个配置文件
CONFIG_PATH=.docker_tools.conf
if [ -f $CONFIG_PATH ]; then
source $CONFIG_PATH
fi
}
# 检测依赖软件包
check_depen(){
local depen=("jq")
for c in ${depen[@]}; do
has_cmd $c || {
echo "Error: $c is not installed."
exit 1
}
done
}
has_cmd () {
command -v $1 >/dev/null 2>&1
}
install(){
case $ID in
ubuntu | debian | kali)
# 添加公钥
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
# 参加仓库
if "$(. /etc/os-release && echo "$VERSION_CODENAME")" = 'kali-rolling' ; then
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian \
buster stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
else
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
fi
# 安装
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
;;
centos)
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 这里我们换成清华的源
sed -i 's+https://download.docker.com+https://mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
yum makecache
yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
;;
arch)
# pacman -S docker
;;
esac
}
install_docker_compose(){
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
}
docker_config_init(){
config_file="/etc/docker/daemon.json"
#config_file="./daemon.json"
[ -f "$config_file" ] || echo "{}" > "$config_file"
}
modify_config(){
jq "$1" "$config_file" > "$config_file.tmp" && mv "$config_file.tmp" "$config_file"
}
proxy(){
modify_config ".proxies.\"http-proxy\" = \"$1\"" \
&& echo "http代理配置成功:$1" \
|| echo "ttp代理配置失败"
modify_config ".proxies.\"https-proxy\" = \"$1\"" \
&& echo "https代理配置成功:$1" \
|| echo "https代理配置失败"
# todo 校验
restart_docker
}
mirror(){
modify_config ".[\"registry-mirrors\"] |= if index(\"$1\") == null then . + [\"$1\"] else . end" \
&& echo "mirror配置成功:$1" \
|| echo "mirror配置失败"
# todo 校验
restart_docker
}
mirror_trust(){
if [[ $1 =~ http://* ]]; then
trust=${1:7:${#1}}
echo $trust
modify_config "if .\"insecure-registries\" | index(\"$trust\") | not then .\"insecure-registries\" += [\"$trust\"] else . end" \
&& echo "mirror_trust配置成功:$trust" \
|| echo "mirror_trust配置失败"
# todo 校验
restart_docker
else
echo "mirror is not http, not change mirror_trust"
fi
}
enable_doceker(){
systemctl enable --now docker
}
restart_docker(){
systemctl daemon-reload
systemctl restart docker
}
rm_stoped(){
docker ps -a -f "status=exited" -q | xargs docker rm
}
main(){
init
docker_config_init
if [ $# -eq 0 ]; then
install
install_docker_compose
enable_doceker
else
case $1 in
install | i)
install
;;
proxy | p)
# todo 校验输入的参数
[ -z "$2" ] && [ -z "$config_proxy" ] && exit 1
proxy ${2:-$config_proxy}
;;
mirror | m)
[ -z "$2" ] && [ -z "$config_mirror" ] && exit 1
mirror ${2:-$config_mirror}
;;
mirror-addtrust | mt)
[ -z "$2" ] && [ -z "$config_mirror_trust" ] && exit 1
mirror_trust ${2:-$config_mirror_trust}
[ -z "$2" ] && [ -z "$config_mirror" ] && exit 1
mirror ${2:-$config_mirror}
;;
add-trust | at)
[ -z "$2" ] && [ -z "$config_mirror_trust" ] && exit 1
mirror_trust ${2:-$config_mirror_trust}
;;
rm-stoped | rs)
rm_stoped
;;
compose)
install_docker_compose
;;
*)
help
;;
esac
fi
}
main $@

archjava

archlinux官方提供的切换脚本,我们对其稍作修改,让其支持ubuntu,并附带了install功能

使用

安装

image.png

查看当前安装了那些

image.png

使用指定版本

image.png

代码

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#! /bin/bash
# Arch Linux helper script to set/unset/check/fix the enabled Java environment
# This program may be freely redistributed under the terms of the GNU General Public License
#
# Author: Guillaume ALAUX <guillaume@archlinux.org>
# Modify: by lzf, 2024-11-26
# feature:
# 1. add jdk download
# 2. support other linux
# warning: this script will change /bin/java link
JVM_DIR=/usr/lib/jvm
#JVM_DIR=./jvm
DEFAULT_NAME=default
DEFAULT_PATH=${JVM_DIR}/${DEFAULT_NAME}
DEFAULT_NAME_JRE=default-runtime
DEFAULT_PATH_JRE=${JVM_DIR}/${DEFAULT_NAME_JRE}
#ORIGIN_JAVA_LN=$PWD/bin/java
ORIGIN_JAVA_LN=/bin/java
# Utility functions
check_root() {
return
if [ $(id -u) -ne 0 ]; then
echo 'This script must be run as root'
exit 1
fi
}
# $1: parameter count given to this script for this option
# $2: expected parameter count for this option
check_param_count() {
if [ $1 -ne $2 ]; then
echo 'Wrong parameter count'
exit 2
fi
}
# Second level functions
get_default_java() {
path=$(readlink -e ${DEFAULT_PATH})
if [ "x${path}" != "x/dev/null" ]; then
echo ${path/${JVM_DIR}\/}
else
echo ""
fi
}
get_installed_javas() {
if [ -d ${JVM_DIR} ]; then
for dir in $(find ${JVM_DIR} -mindepth 1 -maxdepth 1 -type d | sort); do
if [ -x ${dir}/bin/java ]; then
javas+=(${dir/${JVM_DIR}\/})
else
if [ -x ${dir}/jre/bin/java ]; then
javas+=(${dir/${JVM_DIR}\/}/jre)
fi
fi
done
fi
echo ${javas[@]}
}
# $1: Java environment name to test
is_java_valid() {
test "x$1" != "x${DEFAULT_NAME}" && test -x ${JVM_DIR}/$1/bin/java
}
# $1: Java environment name to set as default
set_default_link_to() {
new_default=$1
unlink ${DEFAULT_PATH} 2>/dev/null
ln -sf ${new_default} ${DEFAULT_PATH}
unlink ${DEFAULT_PATH_JRE} 2>/dev/null
if [[ -d ${new_default}/jre ]]; then
ln -sf ${new_default}/jre ${DEFAULT_PATH_JRE}
else
ln -sf ${new_default} ${DEFAULT_PATH_JRE}
fi
}
unset_default_link() {
unlink ${DEFAULT_PATH} 2>/dev/null
unlink ${DEFAULT_PATH_JRE} 2>/dev/null
}
# First level functions
do_status() {
installed_java=($(get_installed_javas))
if [ ${#installed_java[@]} -eq 0 ]; then
echo 'No compatible Java environment installed'
else
default_java=$(get_default_java)
echo 'Available Java environments:'
for java in ${installed_java[@]}; do
# We discoverd this Java env but its JRE is actually set as default
if [ "${java}/jre" = "${default_java}" ]; then
echo -e " ${java} (${java}/jre default)"
elif [ ${java} = "${default_java}" ]; then
echo -e " ${java} (default)"
else
echo " ${java}"
fi
done
if [ -z ${default_java} ]; then
echo "No Java environment set as default"
fi
fi
}
do_get() {
get_default_java
}
# $1: Java environment name to set as default
do_set() {
if ! is_java_valid $1; then
echo "'${JVM_DIR}/$1' is not a valid Java environment path"
exit 1
fi
default=$(get_default_java)
if [ "x$1" != "x${default}" ] || ! is_java_valid ${default}; then
unset_default_link
set_default_link_to $1
fi
#parent_dir=$(dirname $1)
#if is_java_valid ${parent_dir}; then
# echo "Warning: '${parent_dir}' looks like a valid JDK whereas '$1' is set as default"
# echo "Fix this with 'archlinux-java set ${parent_dir}'"
#fi
}
# $1: Java environment name to unset
do_unset() {
unset_default_link
}
do_fix() {
default=$(get_default_java)
if is_java_valid ${default}; then
# If its parent is also a valid Java environment
if is_java_valid $(dirname ${default}); then
unset_default_link
set_default_link_to $(dirname ${default})
fi
else
prev=$(readlink ${DEFAULT_PATH})
unset_default_link
potential_fixes=("${prev/\/jre}" "${prev}/jre")
openjdk8=('java-8-openjdk' 'java-8-openjdk/jre')
# List of environments to check by order of preference:
# - first potential fixes of user choices,
# - then OpenJDK8 as it is considered a default in Arch Linux
# - finally, any installed environment
to_check=(${potential_fixes[@]} ${openjdk8[@]} $(get_installed_javas))
for java in ${to_check[@]}; do
if ! is_java_valid $(get_default_java) && is_java_valid ${java}; then
set_default_link_to ${java}
fi
done
fi
if ! is_java_valid $(get_default_java); then
echo 'No valid Java environment found'
fi
}
# 下载并解压,避免繁琐的操作
download_and_untar(){
file="$JVM_DIR/$1"
wget -O "$file" "$2"
tar -xf "$file" -C "$JVM_DIR"
}
# 这里写死了
install(){
case $1 in
8)
download_and_untar jdk-8u421-linux-x64.tar.gz https://d.injdk.cn/d/download/oraclejdk/8/jdk-8u421-linux-x64.tar.gz
;;
11)
download_and_untar jdk-11.0.24_linux-x64_bin.tar.gz https://d.injdk.cn/d/download/oraclejdk/11/jdk-11.0.24_linux-x64_bin.tar.gz
;;
17)
download_and_untar jdk-17_linux-x64_bin.tar.gz https://d.injdk.cn/d/download/oraclejdk/17/jdk-17_linux-x64_bin.tar.gz
;;
21)
download_and_untar jdk-21_linux-x64_bin.tar.gz https://d.injdk.cn/d/download/oraclejdk/21/jdk-21_linux-x64_bin.tar.gz
;;
23)
download_and_untar jdk-23_linux-x64_bin.tar.gz https://d.injdk.cn/d/download/oraclejdk/23/jdk-23_linux-x64_bin.tar.gz
;;
*)
echo "暂不支持$1"
exit 1
;;
esac
}
# 为了兼容其他发行版,强制将/bin/java连接到了$JDK_DIR/default/bin/java
change_ln(){
if [ -f $ORIGIN_JAVA_LN ];then
if ! [[ $(readlink $ORIGIN_JAVA_LN) == *default* ]]; then
cp $ORIGIN_JAVA_LN ".$ORIGIN_JAVA_LN.back"
unlink /bin/java
ln -s "$JVM_DIR/default/bin/java" $ORIGIN_JAVA_LN
fi
else
ln -s "$JVM_DIR/default/bin/java" $ORIGIN_JAVA_LN
fi
}
usage() {
echo "$(basename $0) <COMMAND>"
echo -e "\nCOMMAND:"
echo -e '\tstatus\t\tList installed Java environments and enabled one'
echo -e '\tget\t\tReturn the short name of the Java environment set as default'
echo -e '\tset <JAVA_ENV>\tForce <JAVA_ENV> as default'
echo -e '\tunset\t\tUnset current default Java environment'
echo -e '\tfix\t\tFix an invalid/broken default Java environment configuration'
echo -e '\tinstall 8/11/17/21/23\t\tInstall jdk tar'
}
# 初始化
init(){
# 判断jvm目录是否存在
if [ ! -d "$JVM_DIR" ]; then
mkdir -p "$JVM_DIR"
fi
}
## Main
init
case $1 in
'status') do_status;;
'get') do_get;;
'set') check_root; check_param_count $# 2; do_set $2; change_ln;;
'unset') check_root; do_unset;;
'fix') check_root; do_fix;;
'install') check_root; install $2;;
'help' | '--help' | '-h' | '') usage;;
*) echo "$(basename $0): unknown option '$@'"; exit 1;;
esac

网卡配置

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
#!/bin/bash
# 检测系统类型
function detect_os() {
if [[ -f /etc/os-release ]]; then
. /etc/os-release
echo "$ID"
else
echo "无法检测操作系统类型。"
exit 1
fi
}
# 修改 IP 地址
function set_ip() {
local iface="$1"
local ip="$2"
local netmask="$3"
sudo ip addr flush dev "$iface" # 清除现有 IP 地址
sudo ip addr add "$ip/$netmask" dev "$iface" # 添加新 IP 地址
sudo ip link set "$iface" up # 启动网卡
}
# 修改 DNS
function set_dns() {
local dns="$1"
local connection_name="$(nmcli -t -f name con show --active | grep -m 1 "$iface")"
if [ -z "$connection_name" ]; then
echo "未找到活动连接,无法设置 DNS。"
exit 1
fi
nmcli con mod "$connection_name" ipv4.dns "$dns"
nmcli con up "$connection_name" # 重新启动连接
}
# 主程序
OS_NAME=$(detect_os)
read -p "请输入网卡名称 (例如 ens33): " iface
read -p "请输入新的 IP 地址 (例如 192.168.1.122): " ip
read -p "请输入子网掩码 (例如 24): " netmask
read -p "请输入新的 DNS 地址 (例如 8.8.8.8): " dns
set_ip "$iface" "$ip" "$netmask"
set_dns "$dns"

思考&&todo

参考