繁体   English   中英

Bash脚本生成和执行命令

[英]Bash script to spawn and execute commands

我正在尝试创建一个bash脚本以ssh进入会话并运行命令,然后退出会话。 目前这是我到目前为止所拥有的:

#!/usr/bin/expect -f

spawn ssh root@sc
expect "assword:"
send "password\r"
expect "#"
send "cd /data2/someDirectory\r"------> don't see this command being executed

和输出

[user@San ddb]$ test1
spawn ssh root@sc
root@sc's password:
SC02 RH 7.3 (0000009B 02.11.0.1)
[root@sc /]# [user@san1 ddb]$
[user@san1 ddb]$

所以我的问题是,为什么目录不设置为myDirectory而是退出会话呢?

您的expect脚本可以完成您的expect工作,但是一旦完成任务send就退出。

设置一些“期望”像expect "$"在脚本的结束和尝试。

#!/usr/bin/expect -f

spawn ssh root@10.200.2.19
expect "assword:"
send "pass\r"
expect "#"
send  "\r"
send  "pwd\r"
send  "\r"
send  "cd /tmp\r"
send  "touch dummy\r"
expect "$"

您不需要为此:

ssh root@host 'cd /tmp; touch afile'

对于无密码操作,请设置ssh密钥。 这是一个不错的教程: http : //support.suso.com/supki/SSH_Tutorial_for_Linux

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM