Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell脚本自动化_Shell_Expect - Fatal编程技术网

Shell脚本自动化

Shell脚本自动化,shell,expect,Shell,Expect,如何在shell脚本中使用expect和send 如何自动输入答案。我尝试使用自动输入密码的脚本登录su。这是我尝试过但不起作用的: #!/bin/bash #!/usr/bin/expect su expect "Password:" send -- "test" 错误消息是: couldn't read file "Password:": no such file or directory ./auto.sh: line 5: send: command not found 假设您希望使

如何在shell脚本中使用
expect
send

如何自动输入答案。我尝试使用自动输入密码的脚本登录su。这是我尝试过但不起作用的:

#!/bin/bash
#!/usr/bin/expect
su
expect "Password:"
send -- "test"
错误消息是:

couldn't read file "Password:": no such file or directory
./auto.sh: line 5: send: command not found

假设您希望使用密码登录,则以下脚本将执行此操作:

#!/usr/bin/expect -f
spawn su theuser
expect "Password: "
send -- "thepassword\r"
interact
在各种陷阱中:当然,您必须提供正确的shebang。不要忘记行尾字符(
\r
),因为它不是隐含的

引用
manexpect

发送[-flags]字符串

将字符串发送到当前进程。例如,命令

             send "hello world\r"
将字符,h e l o w o r l d发送到 当前进程。(Tcl包括一个类似printf的命令(称为 格式),可以生成任意复杂的字符串。)

虽然程序中有行,但字符会立即发送- 缓冲输入在返回字符之前不会读取字符- acter已发送返回字符表示为“\r”



尽管如此,我还是忍不住要提醒您在脚本文件中以明文形式提供密码;)根据您的实际需要,可能还有其他选择…

我从您的问题中删除了各种问候语,因为我们确实是来帮助您的。无论如何,这个问题还不是很清楚。请你试着重新措辞好吗?我应该在这里补充什么?呵呵,可能是进球吧?顶部的两条射门线不行。您可以将一个用于expect或一个用于bash。。。也许你应该。谢谢你的回答,这对我帮助很大。我想知道\r和\n之间的区别是什么?呵呵,当然我不会提供密码。我只是想知道如何使用expect。这对我来说是一个简单的例子,呵呵:)@adityosetyongroho我现在真的不知道为什么
expect
使用
\r
而不是
\n
——但是手册页上有明确的说明。。。我在答覆中引用了有关的部分。