自動建立samba目錄的shell指令碼
1 shell指令碼實現ssh自動登入遠端伺服器
spawn 開啟一個子程式
expect 預期收到的字元
send 傳送字元
interact 互動
#!/usr/bin/expect
spawn ssh root@192.168.22.194
expect "*password:"
send "123\r"
expect "*#"
interact
2 如何向expect指令碼傳遞引數
expect是通過set <變數名稱> [lindex argv 0]
#!/usr/bin/expect
set timeout 10
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
spawn ssh-copy-id -i .ssh/id_rsa.pub $username@$hostname
expect "yes/no"
send "yes\r"
expect "password:"
send "$password\r"
expect eof
3 如何解決echo時重定向到檔案時permission denied
加一個“ sh -c ”就可以把許可權指定到整條shell了。如:
sudo sh -c "echo '[yaf]' > /usr/local/php/etc/include/yaf.ini"
另一種方法是利用管道和 tee 命令,該命令可以從標準輸入中讀入資訊並將其寫入標準輸出或檔案中,tee 命令de “-a” 選項的作用等同於 “>>” 命令,如:
echo “xxxx” | sudo tee -a test.txt
4 指令碼示例
#!/usr/bin/expect
set HostIp [lindex $argv 0]
set UserId [lindex $argv 1]
set UserPwd [lindex $argv 2]
set new_smb_folder [lindex $argv 3]
set path_of_samba [lindex $argv 4]
set timeout -1
spawn ssh $UserId@$HostIp
expect "*?password:" { send "$UserPwd\r"}
expect "$ " { send "sudo mkdir -p '$path_of_samba'\r" }
expect ": " { send "$UserPwd\r" }
expect "$ " { send "echo -e '$new_smb_folder' | sudo tee -a /etc/samba/smb.conf\r"}
expect "$ " { send "sudo service smbd restart\r" }
呼叫:
full_string="
[$name_of_sambaRoot]
path = $path_of_sambaRoot
available = yes
browseable = yes
public = yes
writable = yes
"
./enableSamba.expect "$ipAddress" "$UserId" "$UserPwd" "$full_string" "$path_of_sambaRoot"
5 指令碼示例,寫成指令碼用expect -f script.sh執行
echo "spawn scp auto_operation_remote.sh $l_user@$l_machine_ip:/tmp/
expect \"*want to continue connecting (yes/no)*\" {send \"yes\r\"}
expect \"*password*\" {send \"$l_rsync_sudo_pwd\r\"}
expect eof" > temp.sh
expect -f temp.sh > rsync_error_tmp.log
常見錯誤:
expect: spawn id exp5 not open
while executing
"expect "password" {send "123456\r"}"
(file "temp.sh" line 3)
原因:特定的機器上,因為之前已經ssh_key加入信任,因此scp操作不需要輸入密碼。
相關文章
- IOS-自動打包Shell指令碼(支援目錄帶空格)iOS指令碼
- shell指令碼:建立函式並指定目錄進行下載指令碼函式
- ssh自動輸入密碼指令碼 切換目錄指令碼密碼指令碼
- rsync 自動建立目錄的坑點
- 自動重建失效index的shell指令碼Index指令碼
- ORACLE自動備份shell指令碼Oracle指令碼
- svn and maven 自動部署shell指令碼Maven指令碼
- 監控某個目錄使用情況的shell指令碼指令碼
- 幾個shell自動化指令碼(轉)指令碼
- 自動化指令碼安裝mysql shell指令碼範例指令碼MySql
- 快速建立分支Shell指令碼指令碼
- 清理目錄指令碼指令碼
- shell指令碼-透過lftp同步遠端目錄到本地指令碼FTP
- Maven專案目錄結構與自動建立maven目錄Maven
- shell 備份檔案指令碼+自動清理指令碼
- shell指令碼自動化採集效能sql指令碼SQL
- Hadoop自動化安裝shell指令碼Hadoop指令碼
- 使用shell生成orabbix自動化配置指令碼指令碼
- 建立互動式shell指令碼對話方塊指令碼
- 一個自動生成awr報告的shell指令碼指令碼
- 更改後的監控某個目錄的使用情況的shell指令碼指令碼
- shell指令碼自動記錄登入使用者ip和歷史命令指令碼
- 建立自啟動檔案指令碼的例項指令碼
- word怎麼自動生成目錄?Word自動生成目錄的方法教程
- linux建立賬戶並自動生成主目錄和主目錄下的檔案Linux
- 案例八:Shell自動化管理賬號指令碼指令碼
- shell指令碼實現自動生成awr報告指令碼
- shell指令碼自動清理超過指定大小的檔案指令碼
- Shell指令碼-壓縮指定目錄下前一天檔案指令碼
- 《使用shell位置變數進行目錄檔案的備份小指令碼》變數指令碼
- [shell]shell指令碼實現每天自動抽取資料插入hive表指令碼Hive
- shell oracle 建立使用者指令碼Oracle指令碼
- 建立管理MySQL資料庫的shell指令碼MySql資料庫指令碼
- SHELL指令碼實現Oracle自啟動與關閉指令碼Oracle
- 自動定時備份 mysql 資料庫 的 shell 指令碼MySql資料庫指令碼
- 用於自動監控磁碟使用情況的 Shell 指令碼指令碼
- samba服務的配置(linux共享目錄)SambaLinux
- linux - 怎麼自動填寫有互動的shell指令碼 - SegmentFaultLinux指令碼