用于生成和执行命令的 Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30408140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash script to spawn and execute commands
提问by kkmoslehpour
I am trying to create a bash script to ssh into a session and run a command then exit out of the session. currently this is what i have so far:
我正在尝试创建一个 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
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 而它只是退出会话?
回答by Prabhu
Your expect
script is doing what you are intending but is quitting once the job is send
is done.
您的expect
脚本正在执行您的意图,但一旦工作send
完成就会退出。
set some "expectation" like expect "$"
at the end of the script and try.
expect "$"
在脚本的末尾设置一些“期望”并尝试。
#!/usr/bin/expect -f
spawn ssh [email protected]
expect "assword:"
send "pass\r"
expect "#"
send "\r"
send "pwd\r"
send "\r"
send "cd /tmp\r"
send "touch dummy\r"
expect "$"
回答by glenn Hymanman
You don't need expect for this:
你不需要期望这个:
ssh root@host 'cd /tmp; touch afile'
For password-less operation, set up ssh keys. Here's a decent tutorial: http://support.suso.com/supki/SSH_Tutorial_for_Linux
对于无密码操作,请设置 ssh 密钥。这是一个不错的教程:http: //support.suso.com/supki/SSH_Tutorial_for_Linux