gpt4 book ai didi

linux - 期望登录后运行 shell 命令

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:39 26 4
gpt4 key购买 nike

我正在尝试使用 expectinteract 登录到 linux RHEL box 后执行命令。下面是脚本

#!/usr/bin/expect
set timeout 100
set temp [lindex $argv 0]
spawn ssh userid@10.20.30.40
expect "Password:"
send "password\n";
interact
expect "*3.2*"
send "./p.sh\n";

它成功登录到盒子,但之后它没有执行命令。

这是登录后 commnad 的实际输出,我正在尝试执行。

Using keyboard-interactive authentication.
Password:
Last login: Sun Mar 22 11:04:01 2015 from com
-sh-3.2$ pbrun pbapp wasapp=ksh

Please note home directories are intended only for user/application profiles.
$

这些是我收到的错误

-sh-3.2$ exit
logout
Connection to 10.20.30.40 closed.
expect: spawn id exp7 not open
while executing
"expect "*$""
(file "./testWas.sh" line 8)

当我尝试期待“*$”执行“密码”

-sh-3.2$ exit
logout
Connection to 10.20.30.40 closed.
couldn't execute " pwd ": no such file or directory
while executing
"exec { pwd }"
(file "./testWas.sh" line 8)

编辑:感谢红色@glenn jackman我能够在登录后执行 pbrun 命令..

但在执行 pbrun 命令脚本后退出

 #!/usr/bin/expect
set timeout 100
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]
spawn ssh $username@$host expect "Password:"
send "$password\n";
expect -re {\$ $} ; # put here string from your server prompt
send "./p.sh\n";
expect -re {\$ $} ;
send "pwd\n";

这是p.sh的内容只有脚本的第一行正在执行..

-sh-3.2$ cat p.sh
pbrun pbapp wsapp=ksh
pwd
clear
-sh-3.2$

还有一个类似的悬而未决的问题 How to run "pbrun pbapp wasapp=ksh" command using SSH java client?

最佳答案

interact 告诉 expect 您将进入手动模式,在这种模式下,您(人类)控制生成的命令。我看到您随后键入了 exit 结束了 ssh session 。由于生成的命令结束,interact 命令结束并且控制返回给脚本。下一个命令终止,因为生成的命令未运行。

简单的说,去掉interact:

#!/usr/bin/expect
set timeout 100
set temp [lindex $argv 0]
spawn ssh userid@10.20.30.40

expect "Password:"
send "password\r" # a carriage return more exactly represents
# "hitting enter"

expect -re {\$ $} # this regular expression matches the end of the prompt
send "./p.sh\r"

if { you want to interact manually with the ssh session } {
interact
} else {
expect -re {\$ $} # if p.sh exits the ssh session, remove this line
send "exit\r" # and this one too.
expect eof
}

关于linux - 期望登录后运行 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29196493/

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