script a shell script that test that the new user can indeed upload and download files using passive mode of operation in proftpd - bash

I need a shell script that test that the new user can indeed upload and download files using passive mode of operation in proftpd.I had installed proftpd on /tmp and configuration file is /etc/proftpd.conf.
please guys help me out here
#! /bin/bash
#decompress the file
mkdir /opt/$1
cd /opt/$1
wget http://ftp.swin.edu.au/proftpd/distrib/source/proftpd-1.3.3e.tar.gz
tar -xzf proftpd-1.3.3e.tar.gz
#configure it
cd proftpd-1.3.3e
./configure --sysconfdir=/opt
#Make the file
make
#make install the extracted file
make install
#To add ftp user,group
useradd $2
groupadd $2
mkdir /home/$3
passwd $2
echo "User $3 ">> /opt/proftpd.conf
echo "Group $3 ">> /opt/proftpd.conf
echo "DefaultRoot ~ " >> /opt/proftpd.conf
echo "PassivePorts 30000 35000 ">> /opt/proftpd.conf
exit

echo -n "Enter FTP Username : "
read USER
echo -n "Enter password : "
read -s PASSWD
HOST='192.168.1.100'
ftp -n -v $HOST << EOT
ascii
user $USER $PASSWD
prompt
put file.txt
get file.txt
ls -la
bye
EOT

Related

Using Vim commands in a Bash Script

so im trying to create a bash script that runs on MAC command line to a remote server and uses some mv commands to move some files around but i also need it to open up a file and add a line to the top of the file and save it in the middle of the script heres what i have so far:
(this is adjusting permissions so i can edit the file)
chef-client -r xxxxxxredactedxxxxxxredacted
cd /xxx/postgresql/xx/main/
Sudo chmod -R 775 filenamehere
sudo chown -R postgres:postgres filenamehere
read -p 'Enter the IP: ' ip
echo "Enter the file name :"
read -r filename
echo "Type this below: host all all "$ip"/24 trust : "
read -r line
cd /etc/postgresql/12/main/
printf '1i\n%s\n.\nwq\n' "$line" | ed "$filename". <-- **this is the problem line**
^ this command gives me permission denied because of access, for some reason i can edit it with vim but not this command
its worth noting these commands arent ran through my pc so my ability to move files is somewhat limited, its ran through SSM ing into an IP of a test enviroment through my command line
Normally I manually VIM into the file and add a line to the top
Don't know if you're using echo to output the prompts because you didn't know about the -p read option or you wanted the new lines.
You could use command grouping to add a line at the top of your file.
read -p "Have you copied a file to the data shipper? (yes/no)"
if [ "$REPLY" == "yes" ]; then
read -p "Enter a variable: " VARIABLE
read -p "Enter a file name: " FILE
cd /var/xxxredacted////
cd /tmp/
sudo mv "$FILE" /varxxxredactedxxxxxxxx/drop
cd /var/redactedxxxxredactedxx/drop
sudo unzip "$FILE"
fi
read -p "Enter the file name:\n" FILENAME
read -p "Enter the line to be added:\n" LINE
{ echo $LINE; cat "$FILENAME"; } > "${FILENAME}.new"
mv "$FILENAME"{.new,}
sed could be used too, if the line had to go to a specific line :
# If \n is omnited, $LINE will just be inserted on
# line 1 instead of appending a new line
sed -i "${LINENB}s/$LINE\n/" $FILENAME

Store output of command in sftp to variable and list

My aim is to create a shell script such that it logins and filter the list of files available and select a file to get. Here I need to run commands like in bash.
My sample code is:
sshpass -p password sftp user#10.10.10.10 <<EOF
cd /home/
var=$(ls -rt)
echo $var
echo "select a folder"
read folder
cd $folder
filen=&(ls -rt)
echo $filen
echo "select a file"
read name
get $name
bye
EOF
The above approach will not work. Remember that the 'here document' (<<EOF ... EOF) is evaluate as input to the sftp session. Prompts will be displayed, and user input will be requested BEFORE any output (ls in this case) will be available from sftp.
Consider using lftp, which has more flexible construct. In particular, it will let you use variables, create command dynamically, etc.
lftp sftp://user#host <<EOF
cd /home
ls
echo "Select Folder"
shell 'read folder ; echo "cd $folder" >> temp-cmd'
source temp-cmd
ls
echo "Select Folder"
shell 'read file ; echo "get $file" >> temp-cmd'
source temp-cmd
EOF
In theory, you can create similar constructs with pipes and sftp (may be a co-process ?), but this is much harder.
Of course, the other alternative is to create different sftp sessions for listing, but this will be expensive/inefficient.
After some research and experimentation, found a way to create batch/interactive sessions with sftp. Posting as separate answer, as I still believe the easier way to go is with lftp (see other answer). Might be used on system without lftp
The initial exec create FD#3 - pointing to the original stdout - probably user terminal. Anything send to stdout will be executed by the sftp in the pipeline.
The pipe is required to allow both process to run concurrently. Using here doc will result in sequential execution. The sleep statement are required to allow SFTP to complete data retrieval from remote host.
exec 3>&1
(
echo "cd /home/"
echo "ls"
sleep 3 # Allow time for sftp
echo "select a folder" >&3
read folder
echo "cd $folder"
echo "ls"
sleep 3 # Allow time for sftp
echo "select a file" >&3
read name
echo "get $name"
echo "bye"
) | sshpass -p password sftp user#10.10.10.10
I would suggest you to create a file with pattern of the files you want downloaded and then you can get files downloaded in one single line:
sftp_connection_string <<< $"ls -lrt"|grep -v '^sftp'|grep -f pattern_file|awk '{print $9}'|sed -e 's/^/get -P /g'|sftp_connection_string
if there are multiple definite folders to be looked into, then:
**Script version**
for fldr in folder1 folder2 folder3;do
sftp_connection_string <<< $"ls -lrt ${fldr}/"|grep -v '^sftp'|grep -f pattern_file|awk '{print $9}'|sed -e "s/^/get -P ${fldr}/g"|sftp_connection_string
done
One-liner
for fldr in folder1 folder2 folder3;do sftp_connection_string <<< $"ls -lrt ${fldr}/"|grep -v '^sftp'|grep -f pattern_file|awk '{print $9}'|sed -e "s/^/get -P ${fldr}\//g"|sftp_connection_string;done
let me know if it works.

Bash error: "b.sh: line 52: syntax error: unexpected end of file logout"

So, I wanted this to download a file from my computer, transfer it to my iPhone, install the file there, and respring my phone. For some reason the error you saw in the title pops up. Would be nice if someone could help, thanks.
#!/bin/bash
clear
cd downloads
if [ ! -d ".tmpdl" ]; then
mkdir ".tmpdl"
fi
cd .tmpdl
echo "Enter .deb link:"
read dllink
echo "Enter name of tweak (simple):"
read name
echo "Enter IP of device:"
read IP
echo "Enter your SSH password. If you don't know what this is enter 'alpine' without the apostrophes."
read pass
echo "Sending test file..."
touch test.txt
sshpass -p "$pass" scp test.txt root#"$IP":/var/mobile/Documents
echo "If the transfer was successful (no permission denied error) enter yes or no if it wasn't."
read reply1
if [ $reply1 == no ]; then
echo "Error, wrong credentials, try again."
exit
fi
echo "Okay, attempting to download file..."
curl -o "$name".deb "$dllink"
if [ ! -d "tmp" ]; then
sshpass -p "$pass" ssh root#"$IP" << EOF
cd /
cd var/mobile/Documents
mkdir tmp
EOF
fi
sshpass -p "$pass" scp "$name".deb root#192.168.1.104:/var/mobile/Documents/tmp
echo "Sent file, deleting it from here..."
cd ..
rm -rf .tmpdl
sshpass -p "$pass" ssh root#"$IP" << EOF
cd /
cd var/mobile/Documents
mkdir tmp
mv "$name".deb tmp/
cd tmp
dpkg -i "$name".deb
echo "Clearing caches..."
cd ..
rm -rf tmp
echo "Done installing, respringing now..."
killall backboardd
EOF
These lines:
if [ ! -d "tmp" ]; then
sshpass -p "$pass" ssh root#"$IP" << EOF
cd /
cd var/mobile/Documents
mkdir tmp
EOF
fi
have the problem. The EOF is not at the beginning of the line (indented by one space), so it is not the end of the here-doc, and hence the here-doc is not terminated until the end of the file (where there happens to be a second EOF that's supposed to be terminating a second here-doc), and that generates the error message because the if is not terminated with a fi. Unindent EOF and you should be in business.

How can I use the root password provided by the user in my bash installation script

I'm trying to install a cronjob to run a bash shell script on a relative's machine. They will run the install and I can't access it remotely yet (that's what my script is for - but that's not the issue here). I use kdialog to request their root password and then want to use that to sudo various commands. My code below is failing by a) revealing the root p/w on the terminal and b) failing to pipe it to the various sudos. Help?
#!/bin/bash
kdialog --password "Please enter your root password to install theCronScript.sh and set up cron"
# Sanity checks =========================================╕
if test -z "$BASH" ; then
printf "$SCRIPT:$LINENO: please run this script with the BASH shell\n">&2
exit 192
fi
#========================================================╛
# Global variables=======================================╕
PW="$?"
THISDIR="$(pwd)"
GETIPFILE='theCronScript.sh'
CRONPERIOD='/15 * * * * '
TARGETCRONDIR='/etc/cron.hourly'
#========================================================╛
echo "hi"
# txt file exists check =================================╕
echo "Checking:"
if [ ! -f "$THISDIR/$GETIPFILE" ]; then #there's no file to install
kdialog --msgbox "I cannot find $GETIPFILE to upload\nPlease check attachments in recent e-mails from Greg and download $GETIPFILE to $THISDIR"
exit
else
if [ -f "$TARGETCRONDIR/$GETIPFILE" ]; then #the target already exists
kdialog --title "Replace or Keep" --warningyesno "A similar file already exists.\n Do you want to replace it (recommended)?\n(The original file will be saved with a different name _OLD)"
if [ $? = 0 ]; then # rename, then replace the existing file
#echo $PW is probably unneccessary beyond the first use but just in case...
RNGETIPFILE=$GETIPFILE'_OLD'
echo $PW | sudo -S mv $TARGETCRONDIR/$GETIPFILE $TARGETCRONDIR/$RNGETIPFILE #rename original file
echo $PW | sudo -S cp $THISDIR/$GETIPFILE $TARGETCRONDIR/$GETIPFILE #copy new version in
echo $PW | sudo -S chmod +x $TARGETCRONDIR/$GETIPFILE #
echo $PW | sudo -S crontab -l > mycron #write out current crontab
echo $PW | sudo -S echo $CRONPERIOD $TARGETCRONDIR >> mycron #echo new cron into cron file
echo $PW | sudo -S crontab mycron #install new cron file
rm mycron
$PW="" #clear password variable once it's no longer required
else # Don't replace, exit
exit
fi
else # Nothing to replace. Just copy it in
echo $PW | sudo -S "cp $THISDIR/$GETIPFILE $TARGETCRONDIR/$GETIPFILE" #copy new version in
echo $PW | sudo -S chmod +x $TARGETCRONDIR/$GETIPFILE # make sure it's executable
echo $PW | sudo -S crontab -l > mycron #write out current crontab
echo $PW | sudo -S echo $CRONPERIOD $TARGETCRONDIR >> mycron #echo new cron into cron file
echo $PW | sudo -S crontab mycron #install new cron file
rm mycron
$PW="" #clear password variable once it's no longer required
fi
fi
exit 0
#========================================================╛
One option is to ask for the password directly in the sudo command via an external GUI. From the sudo manpage:
-A, --askpass
Normally, if sudo requires a password, it will read it from the user's terminal. If the -A (askpass) option is specified, a (possibly graphical)
helper program is executed to read the user's password and output the password to the standard output. If the SUDO_ASKPASS environment variable is
set, it specifies the path to the helper program. Otherwise, if sudo.conf(5) contains a line specifying the askpass program, that value will be
used. For example:
# Path to askpass helper program
Path askpass /usr/X11R6/bin/ssh-askpass
If no askpass program is available, sudo will exit with an error.
if sudo is caching credentials, it will only ask for this password once. One way I use to get this cached would be, having no side effects other than caching the password:
export SUDO_ASKPASS=/usr/bin/ssh-askpass
sudo --askpass true
Depending on what distro you're running, ssh-askpass may be somewhere else. There is an example on StackExchange on how you might use kdialog to get the password for the sudo askpass. For reference, here is the script:
$ cat myaskpass.sh
#!/bin/bash
kdialog --password "Please enter your password: "
exit 0
And how you would use it:
export SUDO_ASKPASS=/path/to/myaskpass.sh
sudo --askpass true

FTP upload failed

I have a bash script that backs up my iOS files over FTP and I'm getting a few problems, I'm just wondering if anyone could help me out?
Here's my script:
#!/bin/bash
mkdir zipfolder
cp /var/mobile/Library/SMS/sms.db /var/root/zipfolder/
cp /var/mobile/Library/Notes/notes.sqlite /var/root/zipfolder/
cp /var/mobile/Library/Safari/Bookmarks.db /var/root/zipfolder/
cp /var/mobile/Library/Safari/History.plist /var/root/zipfolder/
cd var/root
zip -r zippyy.zip zipfolder
HOST=HOSTNAME
USER=USERNAME
PASS=PASSWORD
ftp -inv $HOST << EOF
user $USER $PASS
cd sms
LIST=$(ls | grep zippyy*.zip)
FILECOUNT=0
for FILE in $LIST
do
if [ -f $FILE ];
then
FILECOUNT+=1
done
FILECOUNT+=1
NEXTDB="zippyy$FILECOUNT.db"
mv zippyy.zip $NEXTDB
ftp -inv $HOST << EOF
put $NEXTDB
bye
EOF
rm -f zippyy.zip
rmdir zipfolder
I get the following errors:
?Invalid command
?Invalid command
We only support non-print format, sorry.
?Invalid command
?Invalid command
?Invalid command
?Invalid command
?Invalid command
?Invalid command
?Invalid command
?Invalid command
?Invalid command
(local-file) (remote-file)
rmdir: failed to remove 'zipfolder': Not a directory
Answer #3 for formatting
Try something like this (totally untested!)
#!/bin/bash
ROOTFOLDER="/var/root"
ZIPNAME="zipfolder"
ZIPFOLDER=$ROOTFOLDER/$ZIPNAME
LIBFOLDER="/var/mobile/Library"
ZIPFILE="zippyy.zip"
mkdir -p $ZIPFOLDER
cp $LIBFOLDER/SMS/sms.db $ZIPFOLDER/
cp $LIBFOLDER/Notes/notes.sqlite $ZIPFOLDER/
cp $LIBFOLDER/Safari/Bookmarks.db $ZIPFOLDER/
cp $LIBFOLDER/Safari/History.plist $ZIPFOLDER/
cd $ROOTFOLDER
zip -r $ZIPFILE $ZIPNAME
HOST=HOSTNAME
USER=USERNAME
PASS=PASSWORD
ftp -inv $HOST << EOF
user $USER $PASS
cd sms
dir . remote_dir.txt
bye
EOF
FILECOUNT=$(grep zippyy remote_dir.txt | wc -l)
NEXTDB="zippyy${FILECOUNT}.db"
mv $ZIPFILE $NEXTDB
ftp -inv $HOST << EOF
user $USER $PASS
put $NEXTDB
bye
EOF
Why are you using cp -i in a script? The -i switch makes the copy "interactive" and so is expecting input from the user, which it wont get because of the script.
Also, can you format your script using the "Code sample" format rather than bullet points! ;-)
New answer for formatting...
It's not entirely clear to me what you're trying to do. It looks like you're trying to find out how many existing backups there are on the ftp server and rename the new backup to go at the end of the list.
You cant execute code on an ftp server (massive security hole!) so the best way to do accomplish this would probably be to get the remote directory listing and process it locally. Try using something like:
ftp -inv $HOST << EOF
user $USER $PASS
cd sms
dir . remote_dir.txt
bye
EOF
{process remote_dir.txt now to get new backup name}
ftp -inv $HOST << EOF
user $USER $PASS
put $NEXTDB
bye
EOF

Resources