最近的闲来无事,就想搞个简单的小脚本管理一下自己手上的几台vps的登录状况,所以就有了这个小脚本
。虽说不少终端都可以做到,而且做得更好,但毕竟闲来无事,打发时间也是好的。(笑)
脚本
如下:
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
|
#!/bin/bash
CONF=~/.config/vps-manager.conf
TEMP_FILE=~/.tmp_vconf
function list_vps() {
local num=1
echo -e "\n可用的VPS:"
cat ${CONF} | while read local line; do
echo -n "${num}. "
echo ${line} | awk '{print $1}'
num=$(expr ${num} + 1)
done
}
function choose_vps() {
local num=1
list_vps
read -p "请输入所选vps的序号: " local choice
cat ${CONF} | while read local line; do
if [ ${num} -eq ${choice} ]; then
echo ${line} >${TEMP_FILE}
break
elif [ ${num} -le ${choice} ]; then
num=$(expr ${num} + 1)
fi
done
vname=$(cat ${TEMP_FILE} | awk '{print $1}')
vaddr=$(cat ${TEMP_FILE} | awk '{print $2}')
vuser=$(cat ${TEMP_FILE} | awk '{print $3}')
vport=$(cat ${TEMP_FILE} | awk '{print $4}')
rm -f ${TEMP_FILE}
}
function list_vps_info() {
local num=1
local label="序号 名称 地址 用户 端口"
echo -e "\n可用VPS详情:\n"
printf "%-8s %-15s %-19s %-12s %-8s\n" ${label}
cat ${CONF} | while read local line; do
line="${num} ${line}"
printf "%-6s %-13s %-17s %-10s %-6s\n" ${line}
num=$(expr ${num} + 1)
done
}
function connect_vps() {
choose_vps
echo -e "正在连接${vname}.....\n"
ssh -p ${vport} ${vuser}@${vaddr}
}
function tranfer_file() {
choose_vps
read -p "请输入要传输的本地文件路径: " local local_path
read -p "请输入要传输的远程主机目录: " local remote_path
echo -e "正在连接${vname}.....\n"
scp -P ${vport} ${local_path} ${vuser}@${vaddr}:${remote_path}
}
function fetch_file() {
choose_vps
read -p "请输入要传输的本地文件路径: " local local_path
read -p "请输入要传输的远程主机目录: " local remote_path
echo -e "正在连接${vname}.....\n"
scp -P ${vport} ${vuser}@${vaddr}:${remote_path} ${local_path}
}
function add_vps() {
echo -e "\n添加VPS中..."
read -p "请输入名称: " local vname
read -p "请输入地址: " local vaddr
read -p "请输入用户: " local vuser
read -p "请输入端口: " local vport
echo "${vname} ${vaddr} ${vuser} ${vport}" >>${CONF}
sort -u ${CONF} -o ${CONF}
echo "VPS添加完成!"
}
function delete_vps() {
list_vps
read -p "请输入要删除的VPS的序号: " local choice
cat ${CONF} | sed -e "${choice}d" >${TEMP_FILE}
cat ${TEMP_FILE} >${CONF}
rm -f ${TEMP_FILE}
echo -e "选择的VPS信息已删除!\n"
}
function run_script() {
# whether config exits.
if [ ! -f ${CONF} ]; then
mkdir -p ~/.config && touch ${CONF}
fi
echo "--------脚本功能--------"
echo "| |"
echo "| 1.可用VPS列表 |"
echo "| 2.连接远程VPS |"
echo "| 3.向VPS传文件 |"
echo "| 4.从VPS取文件 |"
echo "| 5.添加可用VPS |"
echo "| 6.删除可用VPS |"
echo "| |"
echo "-----------------------"
echo " "
read -p "请输入选择: " local choice
case ${choice} in
1)
list_vps_info
;;
2)
connect_vps
;;
3)
tranfer_file
;;
4)
fetch_file
;;
5)
add_vps
;;
6)
delete_vps
;;
*)
exit 0
;;
esac
echo -e "\n------Goodbye!------\n"
exit 0
}
# run script.
run_script
|
说明
最好先把所需的密钥文件装好。保险起见,密码还是要自己输入的。
以后还会不定时按自己的需要来改。
OVER