Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.
Do more with
#!/bin/sh
HOST_LIST_FILE=file.txt
USER_NAME=user
USER_PASSWD=pass
EXIST_FILE=/the/remote/exist/file
LOG_FILE=/log/file
EXCLUDE_PATTERN="exclude /smarty"
function write2log()
{
echo $(date '+%m.%d.%Y-%H:%M:%S'): $*
} >> $LOG_FILE
for hostname in $( awk -F: '{print $1;}' ${HOSTS_LIST_FILE} ); do
auto_ssh.exp $USER_NAME $USER_PASSWD $hostname 0 ls $EXIST_FILE
if [ $? ne 0 ]; then
write2log $hostname : $EXIST_FILE [Not exist]
else
auto_ssh.exp $USER_NAME $USER_PASSWD $hostname 1 grep "${EXCLUDE_PATTERN}" $EXIST_FILE
if [ $? eq 0 ]; then
# you can remove the following log msg if it not needed
write2log $hostname : $EXIST_FILE [Exist] - ${EXCLUDE_PATTERN} [ Already Exist ] - do nothing
else
tf=tempfile
auto_ssh.exp $USER_NAME $USER_PASSWD $hostname 0 cat ${EXIST_FILE} | awk -vexclude_dir_name="${EXCLUDE_PATTERN}" 'BEGIN{ exclude_flag=0 printed=0 } /^exclude/{exclude_flag=1;} ! /^exclude/ && exclude_flag==1 && printed==0 {printed = 1; printf( "%s\n" , exclude_dir_name); } /^include/ && printed==0 { printed=1; printf("%s\n", exclude_dir_name); } {print $0;}' > $tf
auto_ssh.exp $USER_NAME $USER_PASSWD $hostname 2 $tf ${EXIST_FILE}
# to be more safe you can use the following instead
#cat $tf | auto_ssh.exp $USER_NAME $USER_PASSWD $hostname 0 cat ${EXIST_FILE}.tmp
#auto_ssh.exp $USER_NAME $USER_PASSWD $hostname 1 rm ${EXIST_FILE}
#auto_ssh.exp $USER_NAME $USER_PASSWD $hostname 2 ${EXIST_FILE}.tmp ${EXIST_FILE}
rm $tf
write2log $hostname : $EXIST_FILE [Exist] - ${EXCLUDE_PATTERN} [Added]
fi
fi
done
#!/usr/bin/expect -f
# Author: Arnon Kaufman
# based on http://bash.cyberciti.biz/security/expect-ssh-login-script/
# ./sshe.exp <user> <password> <ipaddress> <mode> <remote script/command> <remote command args>
# ------------------------------------------------------------------------
# Copyright (c) Arnon Kaufman
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
set user [lrange $argv 0 0]
set password [lrange $argv 1 1]
set ipaddr [lrange $argv 2 2]
set mode [lrange $argv 3 3]
set remote_command [lrange $argv 4 4]
set args [lrange $argv 5 end]
#set timeout -1
if { $mode eq "1" } {
spawn ssh $user@$ipaddr $remote_command $args
} else {
if { $mode eq "0" } {
spawn ssh $user@$ipaddr $remote_command $args ">/dev/null 2>&1; echo $?"
} else {
if { $mode eq "2" } {
spawn scp $remote_command $user@$ipaddr:$args
}
}
}
match_max 100000
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
if { $mode ne "0" } {
expect {
"0" { exit 0; }
eof { exit 1; }
}
} else {
expect eof
}
Premium Content
You need an Expert Office subscription to comment.Start Free Trial