python -c 'import os; os.system("/usr/bin/expect -c \'spawn ssh [email protected]; expect \"password:\" { send \"root\r\"}; interact\'")'
在CLI中执行上述命令时,出现引号不匹配的问题(>提示)
但是可以在python脚本中执行而不是在命令行中运行。另外,期望脚本语法是正确的。
在这种情况下如何平衡/均衡报价?我想了解窍门。是否有任何在线验证检查工具(如正则表达式解析器在线检查)可用?
首先,在shell中(我正在使用Bash
),编写正确的代码expect -c "..."
:
[STEP 101] # expect -c "spawn ssh [email protected] date; expect \"assword:\" { send \"foobar\r\"}; expect eof"
spawn ssh [email protected] date
[email protected]'s password:
Wed 13 Jan 2021 10:26:53 AM CST
[STEP 102] #
(在这里,我仅使用双引号,因此在后面加上单引号会更容易python -c '...'
。)
然后,编写python -c 'print(...)'
将输出前一个的expect -c
:
[STEP 103] # python -c 'print("""expect -c "spawn ssh [email protected] date; expect \\"assword:\\" { send \\"foobar\\r\\"}; expect eof" """)'
expect -c "spawn ssh [email protected] date; expect \"assword:\" { send \"foobar\r\"}; expect eof"
[STEP 104] #
然后,更换print
有os.system
:
[STEP 105] # python -c 'import os; os.system("""expect -c "spawn ssh [email protected] date; expect \\"assword:\\" { send \\"foobar\\r\\"}; expect eof" """)'
spawn ssh [email protected] date
[email protected]'s password:
Wed 13 Jan 2021 10:27:33 AM CST
[STEP 106] #
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句