Here are a few recommendations:
- You can always echo line in your shell script to see what's going on.
- You can also use
set -xv
to help you debug a shell script. Simply put set -xv
before the part you want to debug, and set +xv
to turn off the debug. You can also use export PS4="\$LINENO> "
to print out the line number in your shell script.
Here's how your script will look with the set -xv
. It's a really nice tool for debugging shell scripts. Use it:
export PS4="\$LINENO: "
set -xv
for ip1 in $IP1
do
sshpass -fpass1.txt scp -r root@$IP1:/attach/res* /user/path/to/$DIR1/
done
for ip2 in $IP2
do
sshpass -fpass2.txt scp -r root@$IP2:/attach/res* /user/path/to/$DIR2/
done
set +xv
I simply made the sshpass
command echo statements since the program wouldn't work on my system anyway. Here's my output with echo
. This is the command you're trying to execute:
sshpass -fpass1.txt scp -r root@1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4:/attach/res* /user/path/to/1 2 3 4/
sshpass -fpass1.txt scp -r root@1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4:/attach/res* /user/path/to/1 2 3 4/
sshpass -fpass1.txt scp -r root@1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4:/attach/res* /user/path/to/1 2 3 4/
sshpass -fpass1.txt scp -r root@1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4:/attach/res* /user/path/to/1 2 3 4/
sshpass -fpass2.txt scp -r root@5.5.5.5 6.6.6.6 6.6.6.6 7.7.7.7:/attach/res* /user/path/to/5 100 104 135/
sshpass -fpass2.txt scp -r root@5.5.5.5 6.6.6.6 6.6.6.6 7.7.7.7:/attach/res* /user/path/to/5 100 104 135/
sshpass -fpass2.txt scp -r root@5.5.5.5 6.6.6.6 6.6.6.6 7.7.7.7:/attach/res* /user/path/to/5 100 104 135/
sshpass -fpass2.txt scp -r root@5.5.5.5 6.6.6.6 6.6.6.6 7.7.7.7:/attach/res* /user/path/to/5 100 104 135/
First, that IP address is all wrong. You notice you seem to be looping, because the command is being executed the number of times you expect, but you're using the wrong variable for the IP address in the command. You have:
sshpass -fpass1.txt scp -r root@$IP1:/attach/res* /user/path/to/$DIR1/
^^^^
and not:
sshpass -fpass1.txt scp -r root@$ip1:/attach/res* /user/path/to/$DIR1/
^^^^
Note that $ip1
is your looping variable and $IP1
is the variable that contains all of your IP addresses. Don't call your variables by the same name that differ in case!. It would have been better if you gave them each different names. For example $ip_list1
instead of $IP1
and $ip
instead of $ip1
:
ip_list1="..."
for ip in $ip_list`
do
sshpass -fpass1.txt scp -r root@$ip:/attach/res* /user/path/to/$DIR1/
done
Correcting for that issue, I get:
sshpass -fpass1.txt scp -r root@1.1.1.1:/attach/res* /user/path/to/1
2
3
4/
sshpass -fpass1.txt scp -r root@2.2.2.2:/attach/res* /user/path/to/1
2
3
4/
sshpass -fpass1.txt scp -r root@3.3.3.3:/attach/res* /user/path/to/1
2
3
4/
sshpass -fpass1.txt scp -r root@4.4.4.4:/attach/res* /user/path/to/1
2
3
4/
sshpass -fpass2.txt scp -r root@5.5.5.5:/attach/res* /user/path/to/5
100
104
135/
sshpass -fpass2.txt scp -r root@6.6.6.6:/attach/res* /user/path/to/5
100
104
135/
sshpass -fpass2.txt scp -r root@6.6.6.6:/attach/res* /user/path/to/5
100
104
135/
sshpass -fpass2.txt scp -r root@7.7.7.7:/attach/res* /user/path/to/5
100
104
135/
See the problem now? You're directory that you're scping
to is not correct. It looks like you want to send your file to multiple directories, and assuming that somehow the scp
command will loop through itself.
Here's my version of your program. I added an inner loop to go through each directory for each IP address:
#!/bin/bash
ip_list1="1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4"
ip_list2="5.5.5.5 6.6.6.6 6.6.6.6 7.7.7.7"
dir_list1="1 2 3 4"
dir_list2="5 100 104 135"
for ip in $ip_list1
do
for dir in $dir_list1
do
echo "sshpass -fpass1.txt scp -r root@$ip:/attach/res* /user/path/to/$dir/"
done
done
for ip in $ip_list2
do
for dir in $dir_list2
do
echo "sshpass -fpass2.txt scp -r root@$ip:/attach/res* /user/path/to/$dir/"
done
done
You don't need to set the IP list and directory list on multiple lines because the for
will break the variable on white space. This is both a blessing and a curse. Or, better might leave you cursing if you're not careful. You have to make sure that there are no unintended white spaces.
The output is now a more reasonable:
sshpass -fpass1.txt scp -r root@1.1.1.1:/attach/res* /user/path/to/1/
sshpass -fpass1.txt scp -r root@1.1.1.1:/attach/res* /user/path/to/2/
sshpass -fpass1.txt scp -r root@1.1.1.1:/attach/res* /user/path/to/3/
sshpass -fpass1.txt scp -r root@1.1.1.1:/attach/res* /user/path/to/4/
sshpass -fpass1.txt scp -r root@2.2.2.2:/attach/res* /user/path/to/1/
sshpass -fpass1.txt scp -r root@2.2.2.2:/attach/res* /user/path/to/2/
sshpass -fpass1.txt scp -r root@2.2.2.2:/attach/res* /user/path/to/3/
sshpass -fpass1.txt scp -r root@2.2.2.2:/attach/res* /user/path/to/4/
sshpass -fpass1.txt scp -r root@3.3.3.3:/attach/res* /user/path/to/1/
sshpass -fpass1.txt scp -r root@3.3.3.3:/attach/res* /user/path/to/2/
sshpass -fpass1.txt scp -r root@3.3.3.3:/attach/res* /user/path/to/3/
sshpass -fpass1.txt scp -r root@3.3.3.3:/attach/res* /user/path/to/4/
sshpass -fpass1.txt scp -r root@4.4.4.4:/attach/res* /user/path/to/1/
sshpass -fpass1.txt scp -r root@4.4.4.4:/attach/res* /user/path/to/2/
sshpass -fpass1.txt scp -r root@4.4.4.4:/attach/res* /user/path/to/3/
sshpass -fpass1.txt scp -r root@4.4.4.4:/attach/res* /user/path/to/4/
sshpass -fpass2.txt scp -r root@5.5.5.5:/attach/res* /user/path/to/5/
sshpass -fpass2.txt scp -r root@5.5.5.5:/attach/res* /user/path/to/100/
sshpass -fpass2.txt scp -r root@5.5.5.5:/attach/res* /user/path/to/104/
sshpass -fpass2.txt scp -r root@5.5.5.5:/attach/res* /user/path/to/135/
sshpass -fpass2.txt scp -r root@6.6.6.6:/attach/res* /user/path/to/5/
sshpass -fpass2.txt scp -r root@6.6.6.6:/attach/res* /user/path/to/100/
sshpass -fpass2.txt scp -r root@6.6.6.6:/attach/res* /user/path/to/104/
sshpass -fpass2.txt scp -r root@6.6.6.6:/attach/res* /user/path/to/135/
sshpass -fpass2.txt scp -r root@6.6.6.6:/attach/res* /user/path/to/5/
sshpass -fpass2.txt scp -r root@6.6.6.6:/attach/res* /user/path/to/100/
sshpass -fpass2.txt scp -r root@6.6.6.6:/attach/res* /user/path/to/104/
sshpass -fpass2.txt scp -r root@6.6.6.6:/attach/res* /user/path/to/135/
sshpass -fpass2.txt scp -r root@7.7.7.7:/attach/res* /user/path/to/5/
sshpass -fpass2.txt scp -r root@7.7.7.7:/attach/res* /user/path/to/100/
sshpass -fpass2.txt scp -r root@7.7.7.7:/attach/res* /user/path/to/104/
sshpass -fpass2.txt scp -r root@7.7.7.7:/attach/res* /user/path/to/135/
Remove the echo
and it should now work.