linux - 预期失败时登录 sudo - BASH

标签 linux bash ssh expect

这是我期望的 bash 脚本调用的脚本:

  1 #!/usr/bin/expect -f
  2 
  3 set timeout 1
  4 set user [lindex $argv 0]
  5 set pw [lindex $argv 1]
  6 
  7 spawn ssh $user@remotehost.com
  8 expect "$"
  9 send -- "cd /opt/Data/\r"
 10 expect "$"
 11 send -- "sudo su runtime\r"
 12 expect "*?assword for*"
 13 send -- "$pw\r"
 14 expect "$"
 15 send -- "./run.sh\r"
 16 expect "$"
 17 send -- "exit\r";
 18 send -- "exit\r";
 19 exit 

我的结果是:

user@localhost:~/Documents/scripts$ ./bashscript
Enter your web password: 
spawn ssh user@remotehost.com
Last login: Tue Dec 10 11:10:37 2013 from user@localhost
[user@remotehost.com ~]$ cd /opt/Data/
[user@remotehost.com /opt/Data]$ sudo su runtime
[sudo] password for user: user@localhost:~/Documents/scripts$

注意最后一行,我不确定为什么密码没有通过。有什么想法吗?

最佳答案

你可能会在意想不到的情况下做到这一点:看看你的系统的 sudo 是否有一个 -S 选项,它从 stdin 读取密码。

#!/bin/sh
echo "$2" | ssh "$1"@remote sudo -S sh -c ./run.sh runtime

关于linux - 预期失败时登录 sudo - BASH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20502877/

相关文章:

Python3.6 优于 Python 3.4

php - 为什么 laravel 应用程序无法正常启动?

bash - 如何在本地删除 docker 存储库的所有标签?

php - 使用 SSH 与 Laravel 连接 mysql_connect

linux - 在 Linux 机器上运行 Firefox 时“无法打开共享对象文件”

linux - 在 linux 中创建可执行程序的路径时遇到问题

bash - if [[ $# -ge 1 ]] 在 shell 脚本中是什么意思

linux - 为什么这个变量没有被改变?

java - 每当我关闭时,vps ssh,在 jar 停止运行后

linux - 为什么不使用 sshrc 中设置的 $PATH?