I want to delete file from server by bash script using ftp - bash

I want to delete file by bash script using ftp
I use below code
$file = xyz/ab/file.txt
curl -v -u $user:$pass ftp://server.com/$file -Q "DELE $file"
but it's give these error
*Entry path is '/'
DELE xyz/ab/file.txt
* ftp_perform ends with SECONDARY: 0
< 550 Could not delete xyz/ab/file.txt: No such file or directory
* QUOT command failed with 550
How can I delete file with single line bash script command

How to delete file from ftp server with curl:
user="foo"
pass="bar"
dir="xyz/ab/" # with trailing slash
file="file.txt"
curl -v -u "$user:$pass" "ftp://server.com/$dir" -Q "-DELE $file"
or
curl -v -u "$user:$pass" 'ftp://server.com' -Q "-DELE /$dir$file"
or without leading /
curl -v -u "$user:$pass" 'ftp://server.com' -Q "-DELE $dir$file"

You could use the ftp command if you want to add additional commands.
user=foo
user=bar
ftp -n 127.0.0.1 <<EOF
quote USER $user
quote PASS $pass
delete xyz/ab/file.txt
exit
EOF
Deleting must be available on the ftp server, however. If I remember correctly, for vsftpd you must set anon_other_write_enable=YES in /etc/vsftpd.conf

Related

Why can't I use 'sudo su' within a shell script? How to make a shell script run with sudo automatically

I cannot figure out what's wrong with my bash script. When I run it in terminal, command by command run every command separately in terminal, it works.
#!/bin/bash
sudo su <<EOF
mkdir /home/ubuntu/backup/
cp "$(ls -t /usr/lib/unifi/data/backup/autobackup | head -1)" /home/ubuntu/backup/
curl --insecure --user root:password -T "$(ls -t /home/ubuntu/backup/ | head -1)" sftp://vps2.duckdns.org:/root/backup.unf
EOF
However, when I run the above bash script, give me plenty of erros
bash -v test.sh
#!/bin/bash
sudo su <<EOF
mkdir /home/ubuntu/backup/
cp "$(ls -t /usr/lib/unifi/data/backup/autobackup | head -1)" /home/ubuntu/backup/
curl --insecure --user root:password -T "$(ls -t /home/ubuntu/backup/ | head -1)" sftp://vps2.duckdns.org:/root/backup.unf
EOF
ls: cannot access '/usr/lib/unifi/data/backup/autobackup': Permission denied
ls: cannot access '/home/ubuntu/backup/': No such file or directory
cp: cannot stat '': No such file or directory
curl: (78) Could not open remote file for reading: SFTP server: No such file
Any help will be very much appreciated!
TIA
It's trying to execute the command substitutions in the original shell, which runs with the regular user's permissions. They need to be executed by su. Quote the EOF token to prevent expansions in the here-document.
sudo su <<'EOF'

Syntax error when calling variable in bash

Here is my code:
#!bin/bash
id=$(sshpass -p password ssh -tt username#ipaddress -p PORT "grep --include=\*.cr -rlw '/usr/local/bin/' -e '$1' | cut -c16-")
echo $id
sshpass -p password rsync -avHPe 'ssh -p PORT' username#ipaddress:/usr/local/bin/"$id" /usr/local/bin/
id echos correctly, but I get an rsync error when trying to call the variable.
If I manually populate and run rsync, the command works, so I'm not sure what is going on.
Rsync gives me the following output on error.
rsync: link_stat "/usr/local/bin/match.cr\#015" failed: No such file or directory (2)
It seems to be grabbing extra characters? Any help is appreciated :)
Looks like your file contains Windows specific "CR LF" characters. You need to convert these to Linux specific "LF" characters in your script. You can use a tool like dos2unix or Notepad++.

Sending mail using shell command in Ruby

I'm trying to send an email using shell command in my ruby script.
I use command
%x{echo "sometext" | mail -s "Account report #{file_tmp}" -a /home/linux/reports/#{file} #{address[0]}}
and I get
Send options without primary recipient specified. Usage: mail
-eiIUdEFntBDNHRVv~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users sh:
line 2: send_report#gmail.com: command not found
Why an email address is taken as second line of command and how to fix it?
Try stripping newlines on the file variable:
%x{echo "sometext" | mail -s "Account report #{file_tmp}" -a /home/linux/reports/#{file.strip} #{address[0]}}

bash script read server and not read password

I use curl to open with username and password router
curl http://192.168.1.1 --user admin:admin |grep -i "stats"
But when I made this code to use curl from bash script I have problem to read server and password from file `LINKS_FILE="server"
PASS="passwd"
for link in `cat "$LINKS_FILE"`
do
for pass in `cat "$PASS"`
do
res=$(curl -m 1 "http://${link}:8080" --user admin:${pass} )
if echo $res | grep -i "stats"; then `

SHELL script to make ftp connection and get xml files

I need a shell script that will login to a remote FTP server, get the list of files present in only root folder and identify only xml files and get those files to local system.
Login credentials can be mentioned in the script it self. This script must be run once a day only.
Please help me with a UNIX BASH SHELL script.
Thanks
script:
#!/bin/bash
SERVER=ftp://myserver
USER=user
PASS=password
EXT=xml
DESTDIR=/destinationdir
listOfFiles=$(curl $SERVER --user $USER:$PASS 2> /dev/null | awk '{ print $9 }' | grep -E "*.$EXT$")
for file in $listOfFiles
do
curl $SERVER/$file --user $USER:$PASS -o $DESTDIR/$file
done
for scheduled run every day check the crontab:
crontab -e
for edit your current jobs and add for example:
0 0 * * * bash /path/to/script
that will mean run the script every day at midnight.
If you can install ncftpget, this is a one-line operation:
ncftpget -u user -p password ftp.remote-host.com /my/local/dir '/*.xml'

Resources