简体   繁体   English

Bash脚本生成和执行命令

[英]Bash script to spawn and execute commands

I am trying to create a bash script to ssh into a session and run a command then exit out of the session. 我正在尝试创建一个bash脚本以ssh进入会话并运行命令,然后退出会话。 currently this is what i have so far: 目前这是我到目前为止所拥有的:

#!/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

and the output 和输出

[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]$

So my question is why isn't the directory being set to myDirectory and it just exits out of the session? 所以我的问题是,为什么目录不设置为myDirectory而是退出会话呢?

Your expect script is doing what you are intending but is quitting once the job is send is done. 您的expect脚本可以完成您的expect工作,但是一旦完成任务send就退出。

set some "expectation" like expect "$" at the end of the script and try. 设置一些“期望”像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 "$"

You don't need expect for this: 您不需要为此:

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

For password-less operation, set up ssh keys. 对于无密码操作,请设置ssh密钥。 Here's a decent tutorial: http://support.suso.com/supki/SSH_Tutorial_for_Linux 这是一个不错的教程: http : //support.suso.com/supki/SSH_Tutorial_for_Linux

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

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