Unexpected end of file in a shell script, cant find the mistake - linux

HEy there i wrote this little shell script for my pi to upload a picture, but everytime i run the script i get "Unexpected end of file" I does not even show me the first echo.
Thanks for your help :)
raspistill -o snapshot2.jpg
HOST=XXXXX #This is the FTP servers host or IP address.
USER= XXXX #This is the FTP user that has access to the server.
PASS=XXXXX #This is the password for the FTP user.
NOW=$(date +"%c")
echo test
if [ -f work ];
then
echo >> ftp.log "$NOW Script failure"
echo ein prozess arbeitet noch
else
echo beginne upload
touch work
ftp -inv $HOST << EOF
user $USER $PASS
cd /bilder2/
put snapshot2.jpg
bye
echo >> ftp.log "$NOW Upload Success"
rm work
echo erfolgreicher upload
fi
EOF

fi should be placed after EOF, my guess would be that your script should look like:
raspistill -o snapshot2.jpg
HOST=XXXXX #This is the FTP servers host or IP address.
USER= XXXX #This is the FTP user that has access to the server.
PASS=XXXXX #This is the password for the FTP user.
NOW=$(date +"%c")
echo test
if [ -f work ];
then
echo >> ftp.log "$NOW Script failure"
echo ein prozess arbeitet noch
else
echo beginne upload
touch work
ftp -inv $HOST << EOF
user $USER $PASS
cd /bilder2/
put snapshot2.jpg
bye
EOF
echo >> ftp.log "$NOW Upload Success"
rm work
echo erfolgreicher upload
fi

Related

Calling multiple shell scripts within a script on different virtual machines

I am trying to create shell scripts which will setup Zookeeper Server in one VM, and its corresponding Zookeeper Clients in different VM's so i written a shell script as below
#!/bin/bash
ZOOKEEPER_SERVER_IP="1.2.3.4"
while read ipaddress zookeepertype number
do
echo -e "Setting up the Zookeepers \n"
echo $ipaddress
if [ "${zookeepertype}" = 'zookeeperserver' ]; then
echo "Setup Zookeeper Server"
#ZOOKEEPER_SERVER_IP = $ipaddress
#echo $ZOOKEEPER_SERVER_IP
#echo $ipaddress
sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/ZooKeeper_Server_Script.sh ubuntu#$ipaddress:/home/ubuntu/
ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress /home/ubuntu/ZooKeeper_Server_Script.sh
echo "This script is about to run ZooKeeper_Server_Script."
echo "The server script has completed.";
#sleep 30
exit 1
fi
echo -e $ZOOKEEPER_SERVER_IP
if [ $zookeepertype = "zookeeperclient" ] ; then
echo "Setup Zookeeper Client"
echo $ipaddress
sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/ZooKeeper_Client_Script.sh ubuntu#$ipaddress:/home/ubuntu/
ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress
#mkdir /home/ubuntu/keyfiles
#exit
#sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/abc/network/test/keyfiles/* ubuntu#$ipaddress:/home/ubuntu/keyfiles
#sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/abc/test/simple/abc.json ubuntu#$ipaddress:/home/ubuntu/
#ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress
#chmod 777 ZooKeeper_Client_Script.sh
#echo "This script is about to run ZooKeeper_Client_Script."
#sh ./ZooKeeper_Client_Script.sh $ZOOKEEPER_SERVER_IP
echo "The client script has completed."
#exit
fi
#Separating Runhosts File
done < setupZkinput.txt
the input file is
1.2.3.4 zookeeperserver 1
5.6.7.8 zookeeperclient 2
9.10.11.12 zookeeperclient 3
The issue that i am facing is
1) Only the server setup is being done , i.e the script is exiting after the first line
2)Not able to assign the server ip dynamically , in the line ZOOKEEPER_SERVER_IP = $ipaddress
Thanks
What is the default permission you are setting up after copying a file to the server?
remember it required execute permission in order to execute script.

Server to server file transfer port?

I use a bash script(https://github.com/johnnywoof/FTP-Bash-Backup) to Backup my Debian www directory to a ftp server. everything went well but transfer blocked by firewall. The script using separate port every time so i can't unblock by Firewall. is there any way to specify a port ?
The Bash script
# FTP server settings
USERNAME=""
PASSWORD=""
SERVER=""
PORT=21
BACKUPDIR="/"
ndays=7
LOCAL_DIRECTORY="/home"
TEMP_BACKUP_STORE="/tmp"
ENCRYPT_BACKUP=false
AES_PASSWORD_FILE=""
timestamp=$(date --iso)
backup_remote_file_name="$timestamp.tar.gz"
backup_file="$TEMP_BACKUP_STORE/$backup_remote_file_name"
MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`
echo "Removing files older than $MM $DD"
listing=`ftp -i -n $SERVER $PORT <<EOMYF
user $USERNAME $PASSWORD
binary
cd $BACKUPDIR
ls
quit
EOMYF`
lista=( $listing )
for ((FNO=0; FNO<${#lista[#]}; FNO+=9));do
# month (element 5), day (element 6) and filename (element 8)
#echo Date ${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]} File: ${lista[`expr $FNO+8`]}
if [ ${lista[`expr $FNO+5`]}=$MM ];
then
if [[ ${lista[`expr $FNO+6`]} -lt $DD ]];
then
echo "Removing ${lista[`expr $FNO+8`]}"
ftp -i -n $SERVER $PORT <<EOMYF2
user $USERNAME $PASSWORD
binary
cd $BACKUPDIR
delete ${lista[`expr $FNO+8`]}
quit
EOMYF2
fi
fi
done
echo "Creating backup..."
tar -czf $backup_file $LOCAL_DIRECTORY
if [ "$ENCRYPT_BACKUP" == "true" ]
then
echo "Encrypting backup using OpenSSL..."
output_encrypted_file="$backup_file.enc"
openssl enc -aes-256-cbc -salt -in $backup_file -out $output_encrypted_file -pass file:$AES_PASSWORD_FILE
rm $backup_file
backup_file=$output_encrypted_file
fi
echo "Uploading backup $backup_file ..."
ftp -n -i $SERVER $PORT <<EOF
user $USERNAME $PASSWORD
cd $BACKUPDIR
put $backup_file $backup_remote_file_name
quit
EOF
echo "Deleting temporary files..."
rm $backup_file
echo "Backup complete."
Switch to passive mode:
Insert passive in a new line after user command.
See: Active FTP vs. Passive FTP, a Definitive Explanation

Transfer files using lftp in bash script

I have server A test-lx, and server B test2-lx, I want to transfer files from server A to server B.
While transfering the files i'll need to create a driectory only if it's not exist, how can i check if a directory exist during the lftp conenction? How can i out several files in one command instead of doing this in 2 lines.
Is there an option to use find -maxdepth 1 -name DirName
Here is my code:
lftp -u drop-up,1Q2w3e4R ftp://ta1bbn01:21 << EOF
cd $desFolder
mkdir test
cd test
put $srcFil
put $srcFile
bye
EOF
Simple way with ftp:
#!/bin/bash
ftp -inv ip << EOF
user username password
cd /home/xxx/xxx/what/you/want/
put what_you_want_to_upload
bye
EOF
With lftp:
#!/bin/bash
lftp -u username,password ip << EOF
cd /home/xxx/xxx/what/you/want/
put what_you_want_to_upload
bye
EOF
From lftp manual:
-u <user>[,<pass>] use the user/password for authentication
You can use mkdir for create a directory. And you can use put command several time like this:
put what_you_want_to_upload
put what_you_want_to_upload2
put what_you_want_to_upload3
And you can close connection with bye
You can check folder is exist or not like this:
#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls /home/test1/test1231")
if [ "$checkfolder" == "" ];
then
echo "folder does not exist"
else
echo "folder exist"
fi
From lftp manual:
-c <cmd> execute the commands and exit
And you can open another connection for put some files.
I don't know how to check folder is exist or not with one connection, but I can do that like this. Maybe you can find better solution:
#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls /home/test1/test2")
if [ "$checkfolder" == "" ];
then
lftp -u user,pass ip << EOF
mkdir test2
cd test2
put testfile.txt
bye
EOF
else
echo "The directory already exists - exiting"
fi
I used the same basic coding outline as phe however I found that using ls /foldername will output "folder does not exist" if the folder is empty. To solve this I use
#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls | grep /test1231")
if [ "$checkfolder" == "" ];
then
echo "folder does not exist"
else
echo "folder exists"
fi
Please note this only works if the folder is in the root directory. For sub directories in a folder the following should work.
#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; find | grep home/test1/test1231")
if [ "$checkfolder" == "" ];
then
echo "folder does not exist"
else
echo "folder exists"
fi
First prepare credentials record into ~/.netrc file as:
machine site-url-here
login user-login-here
password user-password-here
so you don't have to expose your password on the command line to use this in script.
Then call:
lftp -e "lftp-command-here" ftps://user-login-here#site-url-here/initial-folder-here/`
In my case I run mget -c * lftp command for getting all logs from java spring boot application running linux app instance at azure infrastructure.
Of course you can put your commands separated by semicolon there.

shell script to copy files from Server A to Server B

I am writing a shell script to copy different extension files from server A to server B but it is not working. I have used this but not working.
#!/bin/sh
echo "copying from Log Folder"
cd /hosting/a/apache-tomcat-7.0.39/logs
echo "Started the transferring files from PROD to FTP server"
echo "transferring Started"
HOST=Xyz.a.com
USER=log
PASS=log
ftp -n $HOST << EOF
echo "connection success"
user $USER $PASS
echo "login success"
put File1_log.txt
echo "transferred success"
echo "renamed success"
if [ $? -eq 0 ]
then
echo "Success.log">>success.log
else
echo "Error.log">>Error.log
bye
EOF
This part is not working properly.
Assuming you have SSH access to the server, use rsync:
rsync File1_log.txt $HOST:

shell bash script - upload a big tar file, retry?

I use the following shell script code to upload a big .tar file. Sometimes it happens that the server can´t resolve the domain to the ip or the other server isn´t available. So I wan´t it to retry some times if it didn´t work. How can I do this? I couldn´t find something for this on the internet.
ftp -inv << EOF
open $FTP_SERVER
user $FTP_USER $FTP_PASS
cd $FTP_VERZEICHNIS
mkdir ultimate_$DATE
cd ultimate_$DATE
mput *.tar
quit
EOF
Edit:
Sorry I have no real experience with shell, how would this look like ?
FTP_SUCCESS_MSG="226 Transfer complete"
while [fgrep "$FTP_SUCCESS_MSG" $FTPLOG]
do
FTPLOG=/temp/ftplogfile
ftp -inv <<! > $FTPLOG
open $FTP_SERVER
user $FTP_USER $FTP_PASS
cd $FTP_VERZEICHNIS
mkdir ultimate_$DATE
cd ultimate_$DATE
mput *.tar
close
quit
!
fi
exit 0
sleep 10s
else
echo "Upload completed"
done
Adapting from your code and the one in Getting exit status code from 'ftp' command in linux shell I made this:
#!/bin/bash
FTP_SUCCESS_MSG="226 Transfer complete"
FTPLOG=/temp/ftplogfile
i=0
while [ $i -le 5 ]; do
ftp -inv <<! > $FTPLOG
open $FTP_SERVER
user $FTP_USER $FTP_PASS
cd $FTP_VERZEICHNIS
mkdir ultimate_$DATE
cd ultimate_$DATE
mput *.tar
close
quit
!
if fgrep "$FTP_SUCCESS_MSG" $FTPLOG ;then
i=10 #stupid way of saying: exit the "while"
else
sleep 5
i=expr $i + 1 # ((i++)) not working
fi
done

Resources