美文网首页
自动创建samba目录的shell脚本

自动创建samba目录的shell脚本

作者: 丽雁解 | 来源:发表于2017-12-26 09:22 被阅读0次

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 <param index>],例如set username [lindexargv 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操作不需要输入密码。

相关文章

  • 自动创建samba目录的shell脚本

    1 shell脚本实现ssh自动登录远程服务器 spawn 开启一个子进程expect 预期收到的字符send 发...

  • MG--iOS自动化脚本打包(小总结)

    一、配置自动打包发布的流程 1、创建Shell脚本2、将Shell脚本文件拖入工程的根目录3、根据自己需求选择好描...

  • Samba文件共享服务

    安装Samba 配置文件smb.conf 创建用户并设置密码 创建Samba共享目录 挂载samba共享 开机自动挂载

  • linux下mysql数据备份

    1.创建文件 /home目录创建shell和mysqlbackup两个文件,shell存放脚本,mysqlback...

  • Oozie的使用

    案例一:Oozie调度shell脚本 目标:使用Oozie调度Shell脚本分步实现: 创建工作目录 在oozie...

  • holleworld

    创建一个目录学习shell脚本,叫shell_script吧 开始写shell脚本 进入vim编辑器,按 i输入一...

  • linux脚本运行的三种方法

    首先进入shell目录创建一个创建一个简单的脚本echo.sh $cd ~/shell $vi echo .sh ...

  • 将shell脚本制作成rpm包

    执行rpmdev-setuptree,创建目录 shell脚本放到SOURCES目录下 在SPECS目录下编写te...

  • 详解Shell脚本实现iOS自动化编译打包提交

    本文始发于我的博文详解Shell脚本实现iOS自动化编译打包提交,现转发至此。 目录 前言 Shell脚本涉及的工...

  • Shell脚本创建MVC目录

    项目中开发用的还是MVC的架构,所以在每创建一个模块后都要相应的创建Model/View/Controller三个...

网友评论

      本文标题:自动创建samba目录的shell脚本

      本文链接:https://www.haomeiwen.com/subject/wgxuwxtx.html