Passing the name of spool file to sqlplus from a shell script - linux

I am trying to write a unix program, in which I need to connect to the SQL DB and fetch the data and store it into a file.
Currently I am using the following command:
output1=`sqlplus -s username#SID/password <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING ON ECHO OFF;
SPOOL EMP_NAMES.txt
select emp_name from employee order by emp_name;
Spool off;
This is working fine. But my requirement was that I want to pass the value of spool file such that everytime a new Spool file would be generated.
I basically want to append the date at the end of the file name like:
date=`date +'%d/%m/%Y_%H:%M:%S:%2N'`
output1=`sqlplus -s username#SID/password <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING ON ECHO OFF;
SPOOL EMP_NAMES_$date.txt
Kindly let me know as to how this can be done.

If you call your sqlplus with a heredoc, you can do this easily:
spool_file=: ... your date logic here ...
sql_ouput=: ... path for sqlplus output & errors ...
sqlplus -s username#SID/password << EOF &> "$sql_output"
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING ON ECHO OFF;
spool $spool_file
# SQL statements
spool off
EOF
if [[ $? != 0 ]]; then
: ... error handling ...
fi
It's better to capture stdout/stderr of sqlplus into a file rather than a shell variable.
I think it is possible to hide the password by removing it from the command line and adding it as the first line of heredoc (this will prevent password from showing in ps)
Take a look at this related post: Connect to sqlplus in a shell script and run SQL scripts

Related

sudoedit non interactive with password and editor input from script

(I'm a beginner in linux and shell)
I need to run a sudoedit -u from a non interactive bash script
I have the password in a variable and the input for the editor
My challenge is to give password AND editor inputs to sudoedit
For the context, I need to make a kind of automatic unit tests for sudoers commands.
So my actual strategy is to run the main script with a user A. In that script I need to switch to a second user B (su) to run a subscript that run the sudoedit command (sudoedit -u userC). And I need to get the $? of the sudoedit for my main script report.
I have tested many things without success. Is there a way to give both password and editor input ? (Or an other way to perform what I need to do ?)
I tried that :
#!/bin/bash
# master script
userB=userB
userBpassword=xxx
export userBpassword
su - userB bash child.sh >/dev/null 2>&1 < <(echo $userBpassword)
isSuccess=$? # I need the information of success or not for file edition
#!/bin/bash
sudoedit -u userC file.txt >/dev/null 2>&1 < <(echo ":wq") # I must inject userBpassword here but how ?
return $?
I must do that non interactive (just asking logins and passwords once at start)
Ideally I should have 0 if sudoedit is success or anithing else if there is a failure (no right or missing file)
Thanks in advance for reading and help

Adding logfile command puts script on the loop?

I'm trying to create logfile for output/terminal files of a script.
Why is script going for the loop?
Script is used for remotely changing passwords over ssh using imported .txt file with list of addresses. Script is working fine until I add line for logging at the end after DONE:
#!/bin/bash
echo "Enter the username for which you want to change the password"
read USER
sleep 2
echo "Enter the password that you would like to set for $USER"
read PASSWORD
sleep 2
echo "Enter the file name that contains a list of servers. Ex: ip.txt"
read FILE
sleep 2
for HOST in $(cat $FILE)
do
ssh root#$HOST "echo $'$PASSWORD\n$PASSWORD' | passwd $USER"
done
I tried adding following log creating commands:
/root/passwordchange.sh | tee -a /root/output.log
logsave -a /root/output.log /root/passwordchange.sh
/root/passwordchange.sh >> /root/output.log
Adding logging line is creating loop for entire program instead of closing it.
I need to sigkill script to end the process.
Output file is created just as normal with all the information provided.
It is my first question here on stack, thank you from advance for all answers

How can I send commands to msfconsole from shell?

Hi guys i need a shell script that can open for example msfconsole and then type commands in it, i made a very basic script but of course it stops when msfconsole starts... can you give me some example?
thanks!
this is the basic dumb script i made:
#!/bin/bash
service postgresql start && sleep 4 && msfconsole && sleep 15 && quit
As you can see i first start postgresql and then i start msfconsole and when i try to close it i can't because i'm not in the shell but im into metasploit.
If you want commands in your script to be read by msfconsole, you need to direct them to its stdin; otherwise, they're only read by the shell itself (which is waiting for msfconsole to exit before it proceeds).
One way to redirect a set of text to a given command is with a heredoc:
#!/bin/bash
service postgresql start || exit
msfconsole <<EOF
sleep 15
quit
EOF
I am not familiar with msfconsole but if you execute it then you get a prompt to enter commands then the following method I use to partition may ssd with gdisk, may work for you.
#!/bin/sh
(
# Delete current partition table
echo o # Delete partition table
echo Y # Confirm deletion
# Create Linux LVM partition
echo n # Create new partition
echo 3 # Partition number
echo # First Sector
echo # Last Sector
echo 8E00 # Set partition type: Linux LVM
# Write out partition table
echo w # Write out new partition table
echo Y # Confirm write
) | gdisk /dev/sdx
If you have space in the commands "" needed like: echo "ls -ltr"

Cron job command to not overwrite exising file when executed

I'm using the following command
mysqldump -hHOST -uUSERNAME -pPASSWORD DATABASE > file.sql
Would like a separate job created each time when cron job is executed and not overwrite existing file.
i'm using shell scrip for solve this problem on my server. This is the code if you wan to try. Create file .sh, Ex: backupdb.sh
#!/bin/bash
NOW=$(date +"%m%d%Y")
FILE="backupdb.$NOW.tgz"
SQL="backupdb.$NOW.sql"
DBNAME='live' #name of database
DUMP='/usr/bin/mysqldump'
USER='root' #user
HOST=10.0.0.8 #Ip database
$DUMP -h $HOST -u $USER -pYOURPASSWORD --routines --events $DBNAME > /home/backupdb/$SQL
gzip -9 /home/backupdb/$SQL
echo "BACKUP DATABASES FROM MYSQL MASTER SUCCESS $SQL"|mail -s "BACKUP DATABASES" your#email.com #you can chang this email with your personal email
and save.
Don't forget to change permission,
chmod +x backupdb.sh
Add this code in cron job
00 04 * * * sh /home/backupdb/backupdb.sh
Save
That's it.
Sorry if my English is not good

script stays in Oracle db

When I use this script :
sqlplus -s "/ as sysdba" << EOF
startup;
EOF
It starts db and returns to host. I want my script doesn't return to host. Where is my mistake?
Does it help if you add exit to your script? In other words, if you try something like:
sqlplus -s "/ as sysdba" << EOF
startup;
exit
EOF
EDIT: if you don't want SQL*Plus to exit after running startup, put the line
startup;
in a file named startup.sql, say. You can then run
sqlplus -s "/ as sysdba" #startup.sql
That should then start the database and leave you in SQL*Plus.

Resources