通过ssh连接并在切换用户之后运行程序

时间:2017-08-10 17:14:51

标签: linux bash shell expect

我想在bash脚本中创建一个通过ssh连接到另一个服务器的函数(使用rsa键),执行切换用户(插入密码),启动程序然后退出并启动程序的退出代码

在我正在做的测试之下:

    #! /usr/bin/expect
set timeout 120
spawn ssh user1@10.211.55.24
expect ".*user1"
sleep 3
send   "whoami\r"
send  "/bin/su hdfs\r"
expect "*?assword:"
send  "hdfs\r"
expect "$"
send   "whomai\r"
send "exit\r"

直到交换机用户,我可以切换到hdfs用户,但不发送以下命令(whomai)。 $提示是正确的。此外,我无法获得命令的退出代码(在此示例中为echo命令)。

0 个答案:

没有答案
相关问题