linux交换式及expect

xiaoxun1008 2010-01-25 06:04:03
过程:
使用的脚步有四个:【backup_unasyn.sh //备份数据所要执行的脚步
host_ip.sh //进行循环捕捉我服务器的ip
db_china.sh //服务器的hosts名
passwds.sh //登录服务器使用的密码

服务器结构:
IP1:113.11.226.112 IP2:113.11.226.113~116 IP3:113.11.226.118
IP1:执行脚本服务器【back_unasyn.sh、host_ip.sh、db_china.sh、passwds.sh】
Ip2:要备份的服务器
IP3:数据存放服务器

脚步展示如下所示:
[root@data ~]# vi passwds.sh
#!/bin/bash
pw1=123456
pw2=654321

============================================
[root@data ~]# vi host_ip.sh
#!/bin/bash
hosts=(
#china
113.11.226.113
113.11.226.114
113.11.226.115
113.11.226.116
)

==================================
[root@data ~]# vi db_china.sh
#!/bin/bash

#get the sh file.
shfile=$1

#set the script file path.
thepath=/root
date=`date +%Y%m%d`
source /root/host_ip.sh
source /root/passwds.sh
#set the update files.
servers=(
china1_db
china2_db //host名称对应ip
china3_db
china4_db
)
for i in ${servers
  • }
    do
    ${thepath}/${shfile} ${i} $pw1 $pw2 $date $hosts
    done
    =====================================
  • [root@data ~]#vi backup_unasyn.sh
    #!/usr/bin/expect

    set host [lindex $argv 0]
    set pwd1 [lindex $argv 1]
    set pwd2 [lindex $argv 2]
    set date [lindex $argv 3]
    set hosts [lindex $argv 4]
    set back_host "113.11.226.118"

    #======================================
    set timeout 3600
    # ssh to target machine
    spawn ssh -p 50589 dwy@$host
    expect "password:"
    send "$pwd1\n"

    # su as root
    sleep 3
    expect -re ".*"
    send "su -\n"

    expect "Password:"
    send "$pwd2\n"

    expect "#"
    send "cd /home/cyanogen/backup\n"

    # sftp put backup database
    expect "#"
    send "sftp -oPort=50589 dwy@$back_host\n"
    expect "password:"
    send "$pwd1\n"
    expect "sftp>"
    send "cd backup\n"
    expect "sftp>"
    send "put slg-$hosts-$date.gz\n"
    expect "sftp>"
    send "quit\n"

    expect "#"
    send "cd /var/log\n"
    expect "#"
    send "tail -21 secure >> /home/dwy/put_backup.log\n"

    expect "#"
    exit

    =============================
    脚本执行的过程:
    ./db_china.sh backup_unasyn.sh 时:

    我怎样调用host_ip.sh中的ip让它进行循环,来完成我的过程。
    请高手帮帮忙,让我怎样去调用host_ip.sh文件中的ip。
    我的邮箱:dwy1008@163.com qq:969856985
...全文
136 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoxun1008 2010-02-03
  • 打赏
  • 举报
回复
谢谢你
steptodream 2010-01-25
  • 打赏
  • 举报
回复
其实你可以把host名和hostip一一对应的写到一个文件里 然后来循环 比如host_ip.txt
内容如下
china1_db 113.11.226.113
china2_db 113.11.226.114
china3_db 113.11.226.115
china4_db 113.11.226.116

db_china.sh就变成了如下

#!/bin/bash

#get the sh file.
shfile=$1

#set the script file path.
thepath=/root
date=`date +%Y%m%d`

source /root/passwds.sh

host_ip_file=/path/host_ip.txt
hostname=`cat ${host_ip_file} | awk '{print $1}'`
for host_name in ${hostname}
do

host_ip=`cat ${host_ip_file} | grep "${host_name}" | awk '{print $2}'`
${thepath}/${shfile} ${host_name} $pw1 $pw2 $date ${host_ip}
done
steptodream 2010-01-25
  • 打赏
  • 举报
回复

hosts和host哪个是ip?

你就想取host_ip.sh 中的IP然后循环一遍吗?
如果是就是host_ip.sh那些IP的话 不用写成shell脚本 直接是普通文件即可 比如host_ip.txt
内容如下
113.11.226.113
113.11.226.114
113.11.226.115
113.11.226.116

在别的shell脚本中要循环使用host_ip.txt中的ip
hostip=`cat /path/host_ip.txt`
for ip in ${hostip}
do
echo $ip
done

18,773

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧