Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lmliheng authored Jun 14, 2024
1 parent 4ec3e6f commit c52df6a
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 0 deletions.
36 changes: 36 additions & 0 deletions CmdUtils/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
CC = gcc
CFLAGS = -I./includes -Wall
OBJFILES = objs/bootstrap.o objs/cmd_utils.o objs/install_utils.o objs/shell_utils.o objs/ExamAll.o objs/PrintColor.o objs/FileInfo.o
TARGET = cil

all: $(OBJFILES) $(TARGET)

objs:
@mkdir -p objs

objs/bootstrap.o: src/bootstrap.c includes/*.h
$(CC) $(CFLAGS) -c $< -o $@

objs/cmd_utils.o: src/Utils/cmd_utils.c includes/cmd_utils.h
$(CC) $(CFLAGS) -c $< -o $@

objs/install_utils.o: src/Utils/install_utils.c includes/install_utils.h
$(CC) $(CFLAGS) -c $< -o $@

objs/shell_utils.o: src/Utils/shell_utils.c includes/shell_utils.h
$(CC) $(CFLAGS) -c $< -o $@

objs/ExamAll.o: src/LoginShell/ExamAll.c includes/ExamAll.h
$(CC) $(CFLAGS) -c $< -o $@

objs/PrintColor.o: src/Library/PrintColor.c includes/PrintColor.h
$(CC) $(CFLAGS) -c $< -o $@

objs/FileInfo.o: src/FileManagement/FileInfo.c includes/FileInfo.h
$(CC) $(CFLAGS) -c $< -o $@

$(TARGET): $(OBJFILES)
$(CC) $(OBJFILES) -o $(TARGET)

clean:
rm -f $(OBJFILES) $(TARGET)
47 changes: 47 additions & 0 deletions CmdUtils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# CmdUtils

CmdUtils是一个用于查询和执行常用命令的命令行工具。它可以帮助用户快速查找和执行各种命令,提高工作效率

目前拥有`命令速查``集成自动化脚本``一键安装脚本``代码统计`等高级功能

**注意仅适用于`linux centos 7.x`**

## 安装
1. linux执行脚本
```bash
curl -O https://heng1.oss-cn-beijing.aliyuncs.com/cil
```
2. 激活环境变量
```bash
vim /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
source /etc/profile
```
`/usr/local/nginx/sbin`改为您cil所在路径

## 使用
查看可用命令:
```bash
cil
```
终端输出如下
```bash
用法:cil (<参数>) <命令名>
例如:cil nginx
cil * :支持nginx,psql(PostgreSQL),pro(进程检查命令),exe(可执行文件操作),base(基础linux检查)——————提示功能
cil i * :支持nginx,psql,nvm,cloc——————安装功能
cil s * :支持env——————shell功能
cil f * :支持该目录下文件或目录名——————文件类型检查功能
cil l * :支持exam——————登录自动执行功能
```

## 编译
如果您要修改代码则可修改后编译运行
```bash
make
```

# 贡献
如果您发现任何错误或想要添加新功能,请随时提交`issue``pull request`
# 许可证
CmdUtils采用`MIT`许可证。
Binary file added CmdUtils/cil
Binary file not shown.
6 changes: 6 additions & 0 deletions CmdUtils/includes/ExamAll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef EXAMALL_H
#define EXAMALL_H

void login_scripts(const char *script);

#endif // EXAMALL_H
6 changes: 6 additions & 0 deletions CmdUtils/includes/FileInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef FILEINFO_H
#define FILEINFO_H

void PrintFileInfo(const char *script);

#endif // FILEINFO_H
18 changes: 18 additions & 0 deletions CmdUtils/includes/PrintColor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef PRINT_COLOR_H
#define PRINT_COLOR_H

typedef enum {
reset = 0,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
count // 用于数组大小
} Color;

void color_printf(Color color, const char *format, ...);

#endif // PRINT_COLOR_H
6 changes: 6 additions & 0 deletions CmdUtils/includes/cmd_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef CMD_UTILS_H
#define CMD_UTILS_H

void show_command_usage(const char *command);

#endif // CMD_UTILS_H
6 changes: 6 additions & 0 deletions CmdUtils/includes/install_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef INSTALL_UTILS_H
#define INSTALL_UTILS_H

void install_scripts(const char *script);

#endif // INSTALL_UTILS_H
6 changes: 6 additions & 0 deletions CmdUtils/includes/shell_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef SHELL_UTILS_H
#define shell_UTILS_H

void shell_scripts(const char *script);

#endif // shell_UTILS_H
20 changes: 20 additions & 0 deletions CmdUtils/src/FileManagement/FileInfo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 新增功能:遍历该目录下文件以及文件夹内存大小等检查功能

#include "../../includes/FileInfo.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void PrintFileInfo(const char *script){
if (strcmp(script, "size") == 0){
system("ls -lh");
printf("\033[1;34m目录下文件内存检查结束\033[0m\n");
}else if(strcmp(script,"nginx") == 0){
printf("\033[1;34mNginx安装检查:\033[0m\n");
system("find / -name \"nginx.conf\"");
}
else {
printf("\033[1;31mCmdUtils的文件功能暂不支持该参数\033[0m\n");
}

}
53 changes: 53 additions & 0 deletions CmdUtils/src/Library/PrintColor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 手写库改写printf
#include "../../includes/PrintColor.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

// ANSI 转义序列前缀
#define ANSI_ESCAPE "\033["



// 颜色代码数组
const char *color_codes[] = {
[reset] = ANSI_ESCAPE "0m",
[red] = ANSI_ESCAPE "31m",
[green] = ANSI_ESCAPE "32m",
[yellow] = ANSI_ESCAPE "33m",
[blue] = ANSI_ESCAPE "34m",
[magenta] = ANSI_ESCAPE "35m",
[cyan] = ANSI_ESCAPE "36m",
[white] = ANSI_ESCAPE "37m"
};

// 带颜色支持的简化版printf函数
void color_printf(Color color, const char *format, ...) {
// 输出颜色代码
printf("%s", color_codes[color]);

// 使用va_list处理可变参数
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);

// 重置颜色
printf("%s", color_codes[reset]);
}


/*
*int main() {
* // 使用colored_printf函数输出不同颜色的文本
* color_printf(red, "这是红色文本。\n");
* color_printf(green, "这是绿色文本。\n");
* color_printf(yellow, "这是黄色文本。\n");
* color_printf(blue, "这是蓝色文本。\n");
* color_printf(magenta, "这是洋红色文本。\n");
* color_printf(cyan, "这是青色文本。\n");
* color_printf(white, "这是白色文本。\n");
*
* return 0;
*}
*/
15 changes: 15 additions & 0 deletions CmdUtils/src/LoginShell/ExamAll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "../../includes/ExamAll.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void login_scripts(const char *script){
if (strcmp(script, "exam") == 0){
system("curl -O https://heng1.oss-cn-beijing.aliyuncs.com/systeminfo.sh && cp systeminfo.sh /etc/profile.d/ && chmod +x /etc/profile.d/systeminfo.sh");
printf("\033[1;34m成功设置登录自动检查系统信息,可自行扩展脚本\033[0m\n");
}
else {
printf("\033[1;31mCmdUtils的登录自动化执行功能暂不支持该参数\033[0m\n");
}

}
44 changes: 44 additions & 0 deletions CmdUtils/src/Utils/cmd_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "../../includes/cmd_utils.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void show_command_usage(const char *command) {
if (strcmp(command, "nginx") == 0) {
printf("\033[1;34mNginx常用命令:\033[0m\n");
printf("\033[1;34m1. 查看Nginx版本:nginx -v\033[0m\n");
printf("\033[1;34m2. 检查Nginx配置文件:nginx -t\033[0m\n");
printf("\033[1;34m3. 重启Nginx:sudo systemctl restart nginx \033[0m\n");
printf("\033[1;34m4. 停止Nginx:sudo nginx -s stop\033[0m\n");
printf("\033[1;34m5. 重新加载配置文件:sudo nginx -s reload\033[0m\n");
printf("\033[1;34m6. 一键安装nginx(centos 7.x适用):curl -O https://heng1.oss-cn-beijing.aliyuncs.com/nginx_install_three.sh && bash nginx_install_three.sh\033[0m\n");
} else if (strcmp(command, "psql") == 0) {
printf("\033[1;34mPostgreSQL常用命令:\033[0m\n");
printf("\033[1;34m1. 登录:(1)系统登录:sudo su - postgres (2)远程登录:psql -U postgres -h localhost -p 5432\033[0m\n");
printf("\033[1;34m2. 进入PostgreSQL的交互式终端:psql\033[0m\n");
printf("\033[1;34m3. 备份:pg_dump -U postgres -h localhost -p 5432 -F t mydb > mydb_backup.sql\033[0m\n");
printf("\033[1;34m4. 一键部署psql14:curl -O https://heng1.oss-cn-beijing.aliyuncs.com/psql_install.sh && bash psql_install.sh\033[0m\n");
} else if (strcmp(command, "pro") == 0) {
printf("\033[1;34m进程检查常用命令:\033[0m\n");
printf("\033[1;34m1. 检查端口:netstat -tuln | grep 80或者ps aux | grep 80\033[0m\n");
printf("\033[1;34m2. 检查资源:free -h\033[0m\n");
} else if (strcmp(command, "exe") == 0) {
printf("\033[1;34m可执行文件常用命令:\033[0m\n");
printf("\033[1;34m1. 添加可执行权限:chmod +x \033[0m\n");
printf("\033[1;34m2. 编辑软链接文件:vim /etc/profile\033[0m\n");
printf("\033[1;34m3. 写入链接:export PATH=$PATH:/usr/local/nginx/sbin\033[0m\n");
printf("\033[1;34m4. 激活软链接:source /etc/profile\033[0m\n");
} else if (strcmp(command, "base") == 0) {
printf("\033[1;34m基础linux检查常用命令:\033[0m\n");
printf("\033[1;34m1. 查看主机名:hostname\033[0m\n");
printf("\033[1;34m2. 查看发行版本:cat /etc/os-release\033[0m\n");
printf("\033[1;34m3. 查看系统信息:uname -a\033[0m\n");
printf("\033[1;34m4. 查看CPU信息:lscpu\033[0m\n");
printf("\033[1;34m5. 查看内存信息:free -h\033[0m\n");
printf("\033[1;34m6. 查看磁盘信息:df -h\033[0m\n");
printf("\033[1;34m7. 系统语系检查:localectl\033[0m\n");
printf("\033[1;34m8. 欢迎语设置:vim /etc/motd\033[0m\n");
} else {
printf("\033[1;31mCmdUtils的提示功能暂不支持命令%s\033[0m\n", command);
}
}
28 changes: 28 additions & 0 deletions CmdUtils/src/Utils/install_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "../../includes/install_utils.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void install_scripts(const char *script){
if (strcmp(script, "nginx") == 0){
system("curl -O https://heng1.oss-cn-beijing.aliyuncs.com/nginx_install_ssl_four.sh && bash nginx_install_ssl_four.sh");
system("source /etc/profile");
system("rm -rf /usr/local/nginx/nginx-1.18.0");
system("nginx -v");
}else if (strcmp(script, "psql") == 0) {
system("curl -O https://heng1.oss-cn-beijing.aliyuncs.com/psql_install.sh && bash psql_install.sh");
}else if (strcmp(script, "nvm") == 0) {
system("curl -O https://heng1.oss-cn-beijing.aliyuncs.com/nvm_install_five.sh && bash nvm_install_five.sh");
system("nvm -v");
}else if (strcmp(script, "cloc") == 0) {
system("sudo yum install epel-release -y && sudo yum install cloc -y");
}else if (strcmp(script, "gcc") == 0) {
system("sudo yum groupinstall \"Development Tools\"");
system("gcc -v");
}

else {
printf("\033[1;31mCmdUtils的安装功能暂不支持该软件\033[0m\n");
}

}
25 changes: 25 additions & 0 deletions CmdUtils/src/Utils/shell_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "../../includes/shell_utils.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void shell_scripts(const char *script){
if (strcmp(script, "env") == 0){
system("env");
}else if(strcmp(script, "banner") == 0){
system("vim /etc/motd");
}else if(strcmp(script, "exam") == 0){
system("cat /etc/os-release");
}else if(strcmp(script, "cpu") == 0){
system("lscpu");
}else if(strcmp(script, "free") == 0){
system("free -h");
}else if(strcmp(script, "system") == 0){
system("uname -a");
}

else {
printf("\033[1;31mCmdUtils的shell功能暂不支持该参数\033[0m\n");
}

}
47 changes: 47 additions & 0 deletions CmdUtils/src/bootstrap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <string.h>
#include "../includes/cmd_utils.h"
#include "../includes/install_utils.h"
#include "../includes/shell_utils.h"
#include "../includes/ExamAll.h"
#include "../includes/FileInfo.h"
#include "../includes/PrintColor.h"


int main(int argc, char *argv[]) {
if (argc == 1) { // argc参数为1时
color_printf(cyan,"用法:cil (<参数>) <命令名>\n");
color_printf(cyan,"例如:cil i nginx 安装中间件Nginx\n");
color_printf(cyan,"cil * :支持nginx,psql(PostgreSQL),pro(进程检查命令),exe(可执行文件操作),base(基础linux检查)——————提示功能\n");
color_printf(cyan,"cil i * :支持nginx,psql,nvm,cloc,gcc——————安装功能\n");
color_printf(cyan,"cil s * :支持env,banner,exam,cpu,free,system——————shell功能\n");
color_printf(cyan,"cil f * :支持size(检查内存大小)——————文件类型检查功能\n");
color_printf(cyan,"cil l * :支持exam——————登录自动执行功能\n");
return 1;
}

if (argc == 2) { // argc参数为2时
show_command_usage(argv[1]); // 提示功能

}

if (argc < 3) {// argc参数为3时
} else if (strcmp(argv[1], "i") == 0 && argc == 3) { // 安装功能,使用strcmp进行字符串比较,并修正逻辑判断顺序
install_scripts(argv[2]);
} else if (strcmp(argv[1], "s") == 0 && argc == 3) { // shell功能
shell_scripts(argv[2]);
}else if (strcmp(argv[1], "l") == 0 && argc == 3) { //登录自动执行功能
login_scripts(argv[2]);
}else if (strcmp(argv[1], "f") == 0 && argc == 3) { // 文件检测功能
PrintFileInfo(argv[2]);

} else if (argc > 2) { // 如果argc大于2且不符合参数要求则给出错误提示
color_printf(cyan,"CmdUtils不支持参数数量或未知命令\n");
return 1;
}

return 0;
}

// 编译:gcc -I../include src/bootstrap.c src/Utils/cmd_utils.c src/Utils/install_utils.c src/Utils/shell_utils.c src/LoginShell/ExamAll.c src/FileManagement/FileInfo.c src/Library/PrintColor.c -o cil
// 注意:确保在编译时包含install_utils.c,因为install_scripts函数定义在此文件中。

0 comments on commit c52df6a

Please sign in to comment.