C 中的字符转义

发布于 2024-10-11 15:31:52 字数 565 浏览 3 评论 0原文

我需要将一些命令传递给终端抛出 C 程序,然后获取它的输入。 作为其中的一部分,我有一行必须放置 Expect 脚本。

FILE *command = popen("script here","r");

我需要执行的脚本是:

expect -c 'spawn ssh  user@host cat /proc/stat

expect {
-re ".*yes/no.*" {
send "yes\r"
exp_continue
}
"password:" {
send -- "password\r"
}
}
interact'

因此,我需要转义几个字符,以便脚本可以正常工作。 我尝试了不同的转义顺序,但都不正确。

感谢您的关注。

UPD:

如果没有转义,我在编译时会遇到错误(“‘*’之前的语法错误”、“程序中的杂散‘\’”等)。 我认为这个问题是由新行引起的,但如果我只是将其写在一行中,脚本将无法工作。我尝试使用 \n ,但这对我没有帮助。

所以,我不能简单地将脚本复制并粘贴到 C 文件,它需要一些处理

I need to pass some commands to terminal throw C program and get it's input after that.
As a part of it, I have a line where Expect script must be placed.

FILE *command = popen("script here","r");

Script I need to execute is:

expect -c 'spawn ssh  user@host cat /proc/stat

expect {
-re ".*yes/no.*" {
send "yes\r"
exp_continue
}
"password:" {
send -- "password\r"
}
}
interact'

So, I need to escape several characters so script worked as it need to work.
I tried different sequences of escaping, but all of them are not right.

And thank you for your attention.

UPD:

Without escaping I get error while compiling ( "syntax error before `*'" , "stray '\' in program" and others).
I think that problem caused by new lines, but script don't work if I simply write it in one line. I tried to use \n , but this did not helped me.

So, I cannot simply copy and paste script to C file, it need some processing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼亦尘 2024-10-18 15:31:52

首先,C 的字符串化可以帮助您使多行字符串更美观:

char *script = "expect -c 'spawn ssh user@host cat /proc/stat\n\n"
               "expect {\n"
               "-re \".*yes/no.*\"\n"
               "send \"yes\\r\"\n"
               ...

编译器会很乐意为您将所有这些字符串粉碎在一起。

当然请注意,\n 在编译时会转换为字符串中的换行符,而 \\r 在编译时会转换为 \r编译时的字符串,希望在运行时变成回车符。

第二件事,您确定将 Expect 脚本嵌入到可执行程序中是正确的方法吗?也许您正在登录的主机会随之改变;如果将脚本与可执行文件分开,则替换脚本会容易得多。 (我无法告诉你我一生中编写了多少个 pppd 聊天脚本,我很高兴不需要重新编译 pppd 即可使它们工作!)

First things first, C's stringification can help you make multiline-strings easier on the eyes:

char *script = "expect -c 'spawn ssh user@host cat /proc/stat\n\n"
               "expect {\n"
               "-re \".*yes/no.*\"\n"
               "send \"yes\\r\"\n"
               ...

The compiler will happily smash all those strings together for you.

Note of course that the \n are turned into newline characters in the string at compile time, while \\r is turned into \r in the string at compile time, which expect is hopefully turning into a carriage return at run time.

Second thing, are you sure embedding an expect script into an executable program is the right approach? Perhaps the host you are logging into will change along the way; replacing the script is much easier if it is broken out separate from the executable. (I can't tell you how many hundreds of pppd chat scripts I have written in my life, I'm just glad it didn't require a recompile of pppd to make them work!)

如梦亦如幻 2024-10-18 15:31:52

如果您在 C 程序中对“脚本”进行硬编码,则需要遵循 C 规则:这意味着转义嵌入的双引号和反斜杠...

const char script[] =
    "expect -c 'spawn ssh  user@host cat /proc/stat\n"
    "expect { -re \".*yes/no.*\" { send \"yes\\r\" exp_continue }\n"
    "             \"password:\" { send -- \"password\\r\" }\n"
    "       }\n"
    "interact'\n"

注意,我还使用 C 换行转义代码 ' 终止了这些行\n'。

If you're hardcoding the "script" in your C program, you need to follow the C rules: that means escaping the embedded double-quotes and backslashes...

const char script[] =
    "expect -c 'spawn ssh  user@host cat /proc/stat\n"
    "expect { -re \".*yes/no.*\" { send \"yes\\r\" exp_continue }\n"
    "             \"password:\" { send -- \"password\\r\" }\n"
    "       }\n"
    "interact'\n"

Notice I've also terminated the lines with the C newline escape code '\n'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文