gpt4 book ai didi

exec - 理解 TCL CATCH exec 问题的问题

转载 作者:行者123 更新时间:2023-12-04 10:54:38 30 4
gpt4 key购买 nike

过去 2 天我遇到了一个问题。

我正在运行一个 tcl 脚本(用于 eggdrop),该脚本在被触发时执行本地 shell 命令(子进程),如果命令成功,它会输出结果。但是,如果命令不成功,我会收到错误消息“Tcl 错误 [proc_ports]:子进程异常退出:。

如果子进程没有找到任何结果,我想要创建一个自定义响应。

脚本是:

set chan "#help"
bind pub -|- .port proc_ports

proc proc_ports {nick host handle channel testes} {
global chan
if {"$chan" == "$channel"} {
return 0
}

if [matchattr $nick |fmn $channel] {
set ports [lindex $testes 0]
set fp [ exec grep -w "$ports" scripts/ports | awk {{$1=""; print $0}} ]

putserv "PRIVMSG $channel :Port \002$ports\002 is normally used for: \002$fp\002"

return 1
} else {
putserv "PRIVMSG $channel :$nick, you do \002NOT\002 have access to this command!"
return 1
}
}

我很乐意使用 TCL 来解决这个问题,以帮助我了解更多信息,而不是将 exec 更改为会返回任何错误的 shell 脚本。

我已经阅读了 TCL 中的 CATCH 命令,并尝试了很多不同的脚本方法,但是都失败了:(

如有任何帮助,我们将不胜感激。

干杯。

最佳答案

  1. 您有严重的安全问题。1a) 变量“testes”包含用户 TEXT。您认为“testes”包含有效的 TCL 列表并在其上使用“lindex”。您至少应该使用命令 set ports [lindex [split $testes] 0]1b) 在发送自定义文本以在 shell 中运行之前,您应该检查它是否包含非法字符。使用string isregexpregsub

  2. 要检查命令执行中的错误,您可以使用以下代码:

    set ports [lindex $testes 0]
    if { [catch {exec grep -w "$ports" scripts/ports | awk {{$1=""; print $0}}} fp] } {
    putserv "PRIVMSG $channel :Something wrong while executing command."
    } {
    putserv "PRIVMSG $channel :Port \002$ports\002 is normally used for: \002$fp\002"
    }

关于exec - 理解 TCL CATCH exec 问题的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14797432/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com