This repository was archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbigdiff_remote.sh
More file actions
115 lines (111 loc) · 3.28 KB
/
Copy pathbigdiff_remote.sh
File metadata and controls
115 lines (111 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
# Author: Leonardo Souza
# Version: 1.0.0
# Date: 03/04/2020
# Version History
# 1.0.0 - First Version
## Variables ##
# No need to declare variables in Bash, but this is just to keep track
file="" # File with list of devices
option="" # Option to pass to BIGdiff script
username="" # Username to login to the devices
password="" # Username to login to the devices
SSHPASS="" # Password variable used by sshpass
output="" # Output from BIGdiff script
exit_code="" # Exit code from BIGdiff script
valid_options=( "--before" "--after" "--before-without" "--after-without" "--backup" "--ucs" "--qkview" "--logs" ) # Valid options to call BIGdiff script remotely
before_file="" # File to copy to perform after or aftre-without options for BIGdiff script
# Provide information about how to run the script
usage()
{
echo "Usage: `basename $0` --option file"
echo "option - Option that will be passed to BIGdiff script"
echo "file - File with the list of devices, on per line, either with IP or name"
exit 0
}
check_file()
{
if [[ ! -f $file ]]
then
echo "Error: Not a valid file."
exit 0
fi
}
check_option()
{
printf "%s \n" ${valid_options[@]} | fgrep -w -- $option &> /dev/null
[[ $? != 0 ]] && echo "Error: Not a valid option." && exit 0
}
get_credentials()
{
echo -n "Username:"
read username
echo -n "Password:"
read -s password
echo
}
copy_files()
{
for line in ${2}
do
SSHPASS=$password sshpass -e scp -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${username}@${1}:${line} . &> /dev/null
if [[ $? == 0 ]]
then
echo "Downloaded file ${line}"
SSHPASS=$password sshpass -e ssh -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${username}@${1} rm -f ${line} &> /dev/null
[[ $? != 0 ]] && echo "Error: While deleting file ${line}." && exit 0
else
echo "Error: While downloading file ${line}."
fi
done
}
run()
{
SSHPASS=$password sshpass -e scp -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bigdiff.sh ${username}@${1}:~ &> /dev/null
[[ $? != 0 ]] && echo "Error: While uploading bigdiff.sh." && exit 0
output=`SSHPASS=$password sshpass -e ssh -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${username}@${1} bash bigdiff.sh $option`
exit_code=$?
if [[ $exit_code != 0 ]]
then
echo "Error: While running BIGdiff."
echo "BIGdiff error code: $exit_code"
exit 0
else
if [[ $option == "--before" ]] || [[ $option == "--before-without" ]]
then
echo "Before file created."
output=`printf "%s\n" ${output[@]} | grep -v -e '-before-.*txt'`
fi
copy_files ${1} "$output"
if [[ $option != "--before" ]] && [[ $option != "--before-without" ]]
then
SSHPASS=$password sshpass -e ssh -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${username}@${1} rm -f bigdiff.sh &> /dev/null
[[ $? != 0 ]] && echo "Error: While deleting bigdiff.sh." && exit 0
fi
fi
}
devices()
{
for line in `cat $file`
do
echo "Device: $line"
run $line
done
}
if [[ $# -eq 2 ]]
then
{
option=$1
file=$2
check_file
check_option
get_credentials
devices
[[ $option == "--before" ]] && [[ $option != "--before-without" ]]
}
else
{
usage
}
fi
exit 0