mac 环境下执行 ps -ef 打印信息太长,影响问题定位的解决办法。

文章介绍了在Linux环境中如何使用setterm和tput命令来控制终端的自动换行功能。用户可以通过这两个命令禁止或恢复自动换行,或者在特定命令输出中进行调整,如通过管道传递给less命令以手动滚动查看。这对于处理长行输出或保持终端整洁非常有用。

例如这种的一大片,不易读。

 有两种办法。

1,使用setterm命令

在Linux中,您可以使用setterm命令来禁止或恢复终端的自动换行功能。

要禁止终端的自动换行功能,可以使用以下命令:


vbnetCopy code

setterm -linewrap off

要恢复终端的自动换行功能,可以使用以下命令:


csharpCopy code

setterm -linewrap on

请注意,这些命令将会影响整个终端会话,包括所有后续执行的命令的输出。如果您只想在当前命令的输出中禁止或恢复自动换行功能,可以使用命令本身提供的选项或管道输出到less命令并手动滚动查看输出结果。例如:


bashCopy code

ls -l | less -S

这将使用ls -l命令的输出结果,并通过管道将其传递给less命令。-S选项将禁用less命令的自动换行功能,因此您可以手动滚动查看输出结果。

2,tput命令

如果您的Linux系统上没有setterm命令,则可以尝试使用tput命令来禁用或启用终端的自动换行功能。

要禁用自动换行功能,请使用以下命令:


Copy code

tput rmam

要恢复自动换行功能,请使用以下命令:


Copy code

tput smam

同样地,这些命令将影响整个终端会话,包括所有后续执行的命令的输出。

如果您只想在当前命令的输出中禁用或恢复自动换行功能,可以将命令输出重定向到文件中,然后使用less命令手动滚动查看文件内容,例如:


luaCopy code

ls -l > output.txt less -S output.txt

这将使用ls -l命令的输出结果,并将其重定向到output.txt文件中。然后,您可以使用less命令打开文件并手动滚动查看输出结果。-S选项将禁用less命令的自动换行功能。

-- ==================== 工具函数 ==================== -- MAC地址验证函数 function validate_mac(mac) mac = string.gsub(mac, "%-", ":") -- 替换 '-' 为 ':' mac = string.gsub(mac, "%s+", "") -- 去除所有空格 local pattern = "^%x%x:%x%x:%x%x:%x%x:%x%x:%x%x$" -- 正确模式 return string.match(mac, pattern) ~= nil end -- 查找并杀死Java进程 function find_and_kill_java_processes() local handle = io.popen("ps -ef") local output = handle:read("*a") handle:close() local pids = {} for line in string.gmatch(output, "[^\n]+") do if string.find(line, "java") and (string.find(line, "Central") or string.find(line, "Controller")) then local pid = string.match(line, "%S+%s+(%d+)") if pid then table.insert(pids, pid) end end end if #pids == 0 then print("未找到匹配的 Central 或 Controller java 进程。") return end for _, pid in ipairs(pids) do os.execute("kill -9 " .. pid) print("正在 kill -9 进程 PID=" .. pid) end print("进程清理完成。") os.execute("sleep 3") end -- 启动Central服务 function start_central() local target_dir = "/opt/tplink/Central/lib/" if os.execute("test -d " .. target_dir) ~= 0 then print("未找到 Central 程序目录,跳过启动。") return end os.execute("cd " .. target_dir) local cmd = "./central-starter-port-local -Djava.home=/opt/tplink/EAPController/lib/ -XX:ParallelGCThreads=2 -Xms256m -Xmx300m -Xmn128m -Xss1024k -XX:MaxHeapFree=32m -XX:StackSize=1024k -R:ExpectedEdenSize=160m &" os.execute(cmd) print("Central 程序已启动。") end -- 启动EAPController服务 function start_eap_controller() local target_dir = "/opt/tplink/EAPController/lib/" if os.execute("test -d " .. target_dir) ~= 0 then print("未找到 EAPController 程序目录,跳过启动。") return end os.execute("cd " .. target_dir) local cmd = "./local-starter -Djava.home=/opt/tplink/EAPController/lib/ -XX:ParallelGCThreads=2 -Xms256m -Xmx640m -Xmn128m -Xss1024k -XX:MaxHeapFree=32m -XX:StackSize=1024k -XX:+VerboseGC -Dcom.sun.management.jmxremote.port=1090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=192.168.0.1 -R:ExpectedEdenSize=160m &" os.execute(cmd) print("EAPController 程序已启动。") end -- 重启云服务 function restart_cloud_service() local service_script = "/etc/init.d/zzzcloud_proc" if os.execute("test -f " .. service_script) ~= 0 then print("错误:服务脚本 " .. service_script .. " 不存在!") return false end local result = os.execute(service_script .. " restart") if result == 0 then print("服务重启成功!") return true else print("服务重启失败!") return false end end -- ==================== 配置文件更新函数 ==================== -- 更新UCI格式的配置文件 function update_uci_config(file_path, mac, device_id) local file = io.open(file_path, "r") if not file then print("错误:文件 " .. file_path .. " 不存在!") return false end local content = file:read("*a") file:close() -- 格式化MAC地址(保持原始格式) local normalized_mac = string.gsub(mac, "%-", ":") -- 替换MAC地址 content = string.gsub( content, "(option%s+macaddr%s+['\"])[%x:%-]+(['\"])", "%1" .. normalized_mac .. "%2" ) -- 替换deviceId content = string.gsub( content, "(option%s+deviceId%s+['\"])[%w%-]+(['\"])", "%1" .. device_id .. "%2" ) -- 写回文件 file = io.open(file_path, "w") if not file then print("写入文件失败:" .. file_path) return false end file:write(content) file:close() print(file_path .. " 更新成功!") return true end -- 更新JSON配置文件(使用文本替换) function update_json_config(file_path, updates) local file = io.open(file_path, "r") if not file then print("错误:文件 " .. file_path .. " 不存在!") return false end local content = file:read("*a") file:close() -- 执行所有更新 for key, new_value in pairs(updates) do -- 创建精确匹配模式 local pattern = string.format('("%s"%s*:%s*")([^"]+)(")', key) local _, count = string.gsub(content, pattern, function(prefix, _, suffix) return prefix .. new_value .. suffix end) if count == 0 then print("警告:未找到 " .. key .. " 字段") end end -- 写回文件 file = io.open(file_path, "w") if not file then print("写入文件失败:" .. file_path) return false end file:write(content) file:close() print(file_path .. " 更新成功!") return true end -- 特殊处理cloud_config.cfg中的sefDomain function update_sef_domain(new_domain) local config_path = "/etc/cloud_proc/cloud_config.cfg" local file = io.open(config_path, "r") if not file then print("错误:文件 " .. config_path .. " 不存在!") return false end local content = file:read("*a") file:close() -- 替换sefDomain值 local pattern = '("sefDomain"%s*:%s*")([^"]+)(")' local new_content, count = string.gsub( content, pattern, '%1' .. new_domain .. '%3' ) if count == 0 then print("错误:未找到 sefDomain 字段!") return false end -- 写回文件 file = io.open(config_path, "w") if not file then print("写入文件失败:" .. config_path) return false end file:write(new_content) file:close() print("sefDomain 更新成功:从 " .. string.match(content, pattern) .. " 更改为 " .. new_domain) return true end -- ==================== 主流程 ==================== function main() print("=== 开始执行完整流程 ===\n") -- 1. 输入 MAC 和 device_id io.write("请输入 MAC 地址(格式:00:2D:0F:72:17:01 或 00-2D-0F-72-17-01):") io.flush() local input_mac = string.upper(io.read()) if not validate_mac(input_mac) then print("错误:MAC 地址格式不正确!") return end io.write("请输入 device_id:") io.flush() local input_device_id = io.read() if input_device_id == "" then print("错误:device_id 不能为空!") return end -- 2. 更新配置文件 update_uci_config("/etc/config/tddp", input_mac, input_device_id) update_json_config("/tmp/sysinfo/device-info.json", { model_id = "72123002", model_version = "2.0", product_version = "2.0", product_id = "72123002" }) update_json_config("/tmp/sysinfo/tddp.json", { mac = string.gsub(input_mac, ":", "-"), -- 保持连字符格式 device_id = input_device_id }) update_sef_domain("n-device-entry-omada-beta.i.tplinkcloud.com") -- 3. 清理 Java 进程 find_and_kill_java_processes() -- 4. 重启服务 start_central() start_eap_controller() -- 5. 重启 zzzcloud_proc restart_cloud_service() print("\n=== 所有操作已完成! ===") end -- 启动主程序 main() 你只需要更新对应的字段就行,而不是整个替换
最新发布
11-25
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值