簡體   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