bash脚本循环中断

时间:2018-08-03 20:21:46

标签: linux bash loops ssh

为什么在单台服务器上完成任务(列表顶部)后循环就自己结束了? 下面的脚本用于读取ftp-user-and-pass.txt文件并将凭据分配给var,并使用SSH执行远程命令。

  

script.sh

#!/bin/bash
count=0
while read info
do
    let count=$count+1
    ip=$(echo $info | cut -d' ' -f1)
    echo "********************************************"
    echo "Executing on server $count ssh: remoteuser@$ip"
    user=$(echo $info | cut -d' ' -f2)
    pass=$(echo $info | cut -d' ' -f3)
    echo "user:$user pass:$pass"
    echo " using remote ssh execution"
    ssh -tt -T -i /home/remoteuser/ssh.key remoteuser@$ip "/bin/bash <<-'EOF'
 ##
 #  commands inside ssh session on remote server
 ##
    rm mount.sh*
    wget http://******.*******.com/scripts/mount.sh > output.txt
    sleep 4
        sudo chmod a+x mount.sh
    sudo ./mount.sh -v -u $user -p $pass >> output.txt
    cat output.txt
    exit
        EOF"    
    echo "Remote mount execution completed for:"
    echo "$info"
    echo "" 
done < /home/remoteuser/ftp-user-and-pass
echo "$count"

此脚本用于在/ opt / remote_backup_server /

上安装远程ftp服务器。
  

mount.sh

#!/bin/bash
if [ $# -eq 0 ]
then
    echo "Missing Credential arguments "
    echo "run:$0 -h for help"
    exit 0
fi
 #
 #  verbosity funcion for level 2 
 #
function log () {
    if [[ $verbose -eq 1 ]]; then
        echo "$@"
    fi
}
#
while getopts "vhu:p:s:" OPTION; do
    case ${OPTION} in
#
        h)  # setting help option
            echo "Usage:"
            echo "$0  -h for help, means this."
            echo "  -u Username for authentication &  mounting FTP Server."
            echo "  -p Password for authentication & mounting FTP Server."
            echo "  -v Verbosity"
            echo "      Level v=1 Default will show completed back up for each dir. "
            echo "      Level v=2 It will show each file bieng compressed and dir completion."
            ;;
#
#
        v)  # setting verbosity level
#           echo "Verbose set to level 2 "
            verbose=2
            ;;
#
#
        u)  # setting user name
            username=$OPTARG
            echo "$0 Username: $username"
            ;;
#
#
        p)  # setting password for user
            password=$OPTARG
            echo "$0 Password: $password"
            ;;
        :)              
            echo "Option -$OPTARG requires an argument, check help." >&2
                exit 1
                ;;
            \?)
                echo "Invalid option: -${OPTARG}" >&2
                ;;
#
#
    esac
done    
#
 #server=''
 #username=''
 #password=''
#
function mount_cifs (){
    package=$(dpkg -s cifs-utils |grep 'installed' |cut -d' ' -f3)
    if [[ $package != "ok" ]];
    then
        sudo apt-get install cifs-utils -y
        wait
        mount_cifs
        wait
    else
        echo "Package alreadty installed."
        echo "generated credential file: /root/.creds.txt"
        printf "username=$username\npassword=$password" | sudo tee /root/.creds.txt
        sudo chmod 600 /root/.creds.txt
        echo ""
        echo "Local mounting dir: /opt/remote_ftp_backup"
        sudo mkdir /opt/remote_ftp_backup
        fstab_entry="//$username.your-backup.de/backup /opt/remote_ftp_backup       cifs    iocharset=utf8,rw,credentials=/root/.creds.txt,uid=0,gid=0,file_mode=0660,dir_mode=0770 0       0"
        #
        #
        echo "Adding entries to file: /etc/fstab"
#
#
#
if [ `grep -c 'your-backup.de*' /etc/fstab` -lt 1 ]
then
    #echo "$(grep -c '*.your-backup.de*' /etc/fstab)"   
        sudo echo "$fstab_entry" | sudo tee /etc/fstab
else
    echo "Entry already found: /etc/fstab"
fi
#
        wait
        echo ""
        sudo mount -a
        df -h |tail -n 3
        echo ""
        echo "Done..."
    fi
}
mount_cifs
wait

此凭证文件包含服务器ip,ftp用户,ftp传递

  

ftp-user-and-pass.txt

192.168.1.1 username password
192.168.1.2 username1 password1

0 个答案:

没有答案