I'm trying to write a script that will check what is the last modified/created file on the server, then copy it to my homedirectory on this server and then copy this file from node to my local machine.
I would like to point out, that I am learning how to do this, so I don't have skill at all.
Here is my script, don't look at "fr" option as it's for testing things localy.
#!/bin/bash
set -xv
dcma=some_server_name1
dcfr=some_server_name2
echo "FR czy MA?"
read dc
if [ "${dc,,}" == "fr" ]
then
echo "Wybrales Frankfurt!"
cd test/ ; out=$( ls -1tr | tail -n 1 ) ; cp $out ../
elif [ "${dc,,}" == "ma" ]
then
echo "Wybrales Boston!"
cmd=$( ssh -t $dcma ' cd /opt/automation/repository/hosts/ ; out=$( ls -1tr | tail -n 1 ) ; sudo cp $out ~/ ; ls -1tr ~/ | tail -n 1 ' )
cmdv=$( echo "$cmd" )
echo "Ostatnie wybrane hosty na serwerze: $cmdv"
scp -r some_server_name1:~/"$cmdv" .
fi
Script is failing on SCP command, as it's says that there is no such file or directory mentioned by $cmdv variable.
Here is output with set -xv.
user-VirtualBox:~/hosty-skrypt$ ./hosty-v8
dcma=some_server_name1
+ dcma=some_server_name1
dcfr=some_server_name2
+ dcfr=some_server_name2
echo "FR czy MA?"
+ echo 'FR czy MA?'
FR czy MA?
read dc
+ read dc
ma
if [ "${dc,,}" == "fr" ]
then
echo "Wybrales Frankfurt!"
cd test/ ; out=$( ls -1tr | tail -n 1 ) ; cp $out ../
elif [ "${dc,,}" == "ma" ]
then
echo "Wybrales Boston!"
cmd=$( ssh -t $dcma ' cd /opt/automation/repository/hosts/ ; out=$( ls -1tr | tail -n 1 ) ; sudo cp $out ~/ ; ls -1tr ~/ | tail -n 1 ' )
cmdv=$( echo "$cmd" )
echo "Ostatnie wybrane hosty na serwerze: $cmdv"
scp -r some_server_name1:~/"$cmdv" .
fi
+ '[' ma == fr ']'
+ '[' ma == ma ']'
+ echo 'Wybrales Boston!'
Wybrales Boston!
ssh -t $dcma ' cd /opt/automation/repository/hosts/ ; out=$( ls -1tr | tail -n 1 ) ; sudo cp $out ~/ ; ls -1tr ~/ | tail -n 1 '
++ ssh -t some_server_name1 ' cd /opt/automation/repository/hosts/ ; out=$( ls -1tr | tail -n 1 ) ; sudo cp $out ~/ ; ls -1tr ~/ | tail -n 1 '
some_username@some_server_name1's password:
Connection to some_server_name1 closed.
+ cmd=$'hosts.merged.1558094140821\r'
echo "$cmd"
++ echo $'hosts.merged.1558094140821\r'
+ cmdv=$'hosts.merged.1558094140821\r'
' echo 'Ostatnie wybrane hosty na serwerze: hosts.merged.1558094140821
Ostatnie wybrane hosty na serwerze: hosts.merged.1558094140821
' .cp -r 'some_server_name1:~/hosts.merged.1558094140821
some_username@some_server_name1's password:
: No such file or directoryhosts.merged.1558094140821
Any idea how to solve this?
Also I can't place this script on target machines as it's against company policy.