diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..91ff53e --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +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/FileSize.o objs/PrintColor.o +TARGET = cil + +all: $(TARGET) + +objs: + @mkdir -p objs + +objs/bootstrap.o: src/bootstrap.c objs + $(CC) $(CFLAGS) -c $< -o $@ + +objs/cmd_utils.o: src/Utils/cmd_utils.c objs + $(CC) $(CFLAGS) -c $< -o $@ + +objs/install_utils.o: src/Utils/install_utils.c objs + $(CC) $(CFLAGS) -c $< -o $@ + +objs/shell_utils.o: src/Utils/shell_utils.c objs + $(CC) $(CFLAGS) -c $< -o $@ + +objs/ExamAll.o: src/LoginShell/ExamAll.c objs + $(CC) $(CFLAGS) -c $< -o $@ + +objs/FileSize.o: src/FileManagement/FileSize.c objs + $(CC) $(CFLAGS) -c $< -o $@ + +objs/PrintColor.o: src/Library/PrintColor.c objs + $(CC) $(CFLAGS) -c $< -o $@ + + +$(TARGET): $(OBJFILES) + $(CC) $(OBJFILES) -o $(TARGET) + +clean: + rm -f $(OBJFILES) $(TARGET) \ No newline at end of file diff --git a/includes/ExamAll.h b/includes/ExamAll.h new file mode 100644 index 0000000..5915aca --- /dev/null +++ b/includes/ExamAll.h @@ -0,0 +1,6 @@ +#ifndef EXAMALL_H +#define EXAMALL_H + +void login_scripts(const char *script); + +#endif // EXAMALL_H \ No newline at end of file diff --git a/includes/FileSize.h b/includes/FileSize.h new file mode 100644 index 0000000..2ebca76 --- /dev/null +++ b/includes/FileSize.h @@ -0,0 +1,6 @@ +#ifndef FILESIZE_H +#define FILESIZE_H + +void printFileSize(const char *script); + +#endif // FILESIZE.H \ No newline at end of file diff --git a/includes/PrintColor.h b/includes/PrintColor.h new file mode 100644 index 0000000..f667bc2 --- /dev/null +++ b/includes/PrintColor.h @@ -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 \ No newline at end of file diff --git a/includes/cmd_utils.h b/includes/cmd_utils.h new file mode 100644 index 0000000..08b5909 --- /dev/null +++ b/includes/cmd_utils.h @@ -0,0 +1,6 @@ +#ifndef CMD_UTILS_H +#define CMD_UTILS_H + +void show_command_usage(const char *command); + +#endif // CMD_UTILS_H \ No newline at end of file diff --git a/includes/install_utils.h b/includes/install_utils.h new file mode 100644 index 0000000..c7d50c1 --- /dev/null +++ b/includes/install_utils.h @@ -0,0 +1,6 @@ +#ifndef INSTALL_UTILS_H +#define INSTALL_UTILS_H + +void install_scripts(const char *script); + +#endif // INSTALL_UTILS_H \ No newline at end of file diff --git a/includes/shell_utils.h b/includes/shell_utils.h new file mode 100644 index 0000000..74ce67f --- /dev/null +++ b/includes/shell_utils.h @@ -0,0 +1,6 @@ +#ifndef SHELL_UTILS_H +#define shell_UTILS_H + +void shell_scripts(const char *script); + +#endif // shell_UTILS_H \ No newline at end of file diff --git a/objs/ExamAll.o b/objs/ExamAll.o new file mode 100644 index 0000000..aaafb36 Binary files /dev/null and b/objs/ExamAll.o differ diff --git a/objs/FileSize.o b/objs/FileSize.o new file mode 100644 index 0000000..8154ebd Binary files /dev/null and b/objs/FileSize.o differ diff --git a/objs/PrintColor.o b/objs/PrintColor.o new file mode 100644 index 0000000..8ad7bed Binary files /dev/null and b/objs/PrintColor.o differ diff --git a/objs/bootstrap.o b/objs/bootstrap.o new file mode 100644 index 0000000..0a93e81 Binary files /dev/null and b/objs/bootstrap.o differ diff --git a/objs/cmd_utils.o b/objs/cmd_utils.o new file mode 100644 index 0000000..488e397 Binary files /dev/null and b/objs/cmd_utils.o differ diff --git a/objs/install_utils.o b/objs/install_utils.o new file mode 100644 index 0000000..e3b930c Binary files /dev/null and b/objs/install_utils.o differ diff --git a/objs/shell_utils.o b/objs/shell_utils.o new file mode 100644 index 0000000..6ebcbe5 Binary files /dev/null and b/objs/shell_utils.o differ diff --git a/src/FileManagement/FileSize.c b/src/FileManagement/FileSize.c new file mode 100644 index 0000000..eeb8fb7 --- /dev/null +++ b/src/FileManagement/FileSize.c @@ -0,0 +1,17 @@ +// 新增功能:遍历该目录下文件以及文件夹内存大小 + +#include "../../includes/FileSize.h" +#include +#include +#include + +void printFileSize(const char *script){ + if (strcmp(script, "size") == 0){ + system("ls -lh"); + printf("\033[1;34m目录下文件内存检查结束\033[0m\n"); + } + else { + printf("\033[1;31mCmdUtils的文件功能暂不支持该参数\033[0m\n"); + } + +} \ No newline at end of file diff --git a/src/Library/PrintColor.c b/src/Library/PrintColor.c new file mode 100644 index 0000000..1804924 --- /dev/null +++ b/src/Library/PrintColor.c @@ -0,0 +1,53 @@ +// 手写库改写printf +#include "../../includes/PrintColor.h" +#include +#include +#include + +// 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; + *} + */ \ No newline at end of file diff --git a/src/LoginShell/ExamAll.c b/src/LoginShell/ExamAll.c new file mode 100644 index 0000000..e56a315 --- /dev/null +++ b/src/LoginShell/ExamAll.c @@ -0,0 +1,15 @@ +#include "../../includes/ExamAll.h" +#include +#include +#include + +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"); + } + +} \ No newline at end of file diff --git a/src/Utils/cmd_utils.c b/src/Utils/cmd_utils.c new file mode 100644 index 0000000..98c65d4 --- /dev/null +++ b/src/Utils/cmd_utils.c @@ -0,0 +1,44 @@ +#include "../../includes/cmd_utils.h" +#include +#include +#include + +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); + } +} \ No newline at end of file diff --git a/src/Utils/install_utils.c b/src/Utils/install_utils.c new file mode 100644 index 0000000..7d553bf --- /dev/null +++ b/src/Utils/install_utils.c @@ -0,0 +1,27 @@ +#include "../../includes/install_utils.h" +#include +#include +#include + +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("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"); + } + +} \ No newline at end of file diff --git a/src/Utils/shell_utils.c b/src/Utils/shell_utils.c new file mode 100644 index 0000000..40ea587 --- /dev/null +++ b/src/Utils/shell_utils.c @@ -0,0 +1,25 @@ +#include "../../includes/shell_utils.h" +#include +#include +#include + +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"); + } + +} \ No newline at end of file diff --git a/src/bootstrap.c b/src/bootstrap.c new file mode 100644 index 0000000..714c9f9 --- /dev/null +++ b/src/bootstrap.c @@ -0,0 +1,48 @@ +#include +#include +#include "../includes/cmd_utils.h" +#include "../includes/install_utils.h" +#include "../includes/shell_utils.h" +#include "../includes/ExamAll.h" +#include "../includes/FileSize.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) { // 文件内存检测功能 + printFileSize(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/FileSize.c -o cil +// 注意:确保在编译时包含install_utils.c,因为install_scripts函数定义在此文件中。 \ No newline at end of file