Exit codes of smbclient - linux

i've a problem with the commandline command "smbclient" of samba on arm.
I wrote a script to download files from a Windows Share.
Here the smb-part of this script.
smbclient //CNAME/SNAME -I0.0.0.0 -N -c "case_sensitive; cd folder; prompt; mget file"
echo $?
My problem ar the exit codes.
If the file is downloaded completely, the exit code is 0 (OK)
If the file cannot be downloaded, the exit code is 1 (OK)
If the testmaschine loses the connection to the share due downloading a file, the exit code is 0 (NOT GOOD), but error ("Lost connection...etc.") is written to console. (OK)
I tried it with two different versions.
samba-3.0.32
samba-3.6.19
Both the same.
Does someone know a good workaround (or smbclient-argument) to let my script know, that the download failed?
PS. I checked the smbclient sources. It looks like they forgot to set the exitcode. Because everytime there is another error the set the Errormessage and do an (e.g. exit(1)). But for timeouts, they only set the Errormessage.
Thank you in advance!

What would be best is to use the -E argument to smbclient and redirect 2>/errorlog from the command line. You can then check this file to see if any errors occurred.
Warning, the first line is always the Domain=......... so you may need to strip that line out.
Something like this:
smbclient Hostname -A authfile -E 1>log 2>errorlog <<-EOF
get foo
EOF
In the errorlog you should find something like below, your log file will be empty
Domain=[Hostname] OS=[Windows Server 2008 R2 Standard 7601 Service
Pack 1] Server=[Windows Server 2008 R2 Standard 6.1]
NT_STATUS_OBJECT_NAME_NOT_FOUND opening remote file \foo

Related

Retrieve underlying file of tee command

References
Fullcode of what will be discussed here:
https://github.com/djon2003/com.cyberinternauts.linux.backup
activateLogs question that solved how to log to file and screen: https://stackoverflow.com/a/70792273/214898
Limitation
Just a small reminder from the last question: this script is executed on limited environment, on a QNAP (NAS).
Background
I have a function that activate logging which now has three modes: SCREEN, DISK, BOTH. With some help (from the question of the link above), I achieve to make work the BOTH option. DISK & BOTH use a file descriptor numbered 3. The first is pointing to a file and the second to stdout.
On exit of my script (using trap), it detects if there were logged errors and send them via email.
Code
function sendErrorMailOnExit()
{
## If errors happened, then send email
local isFileDescriptor3Exist=$(command 2>/dev/null >&3 && echo "Y")
if [ "$isFileDescriptor3Exist" = "Y" ]; then
local logFile=$(readlink /proc/self/fd/3 | sed s/.log$/.err/)
local logFileSize=$(stat -c %s "$logFile")
if [ $logFileSize -gt 0 ]; then
addLog "N" "Sending error email"
local logFileName=$(basename "$logFile")
local logFileContent=$(cat "$logFile")
sendMail "Y" "QNAP - Backup error" "Error happened on backup. See log file $logFileName\n\nLog error file content:\n$logFileContent"
fi
fi
}
trap sendErrorMailOnExit EXIT
Problem
As you can see, this works well because the file descriptor #3 is using a file. But now, using the BOTH option, the file descriptor #3 is pointing to stdout and the file is written via tee. Hence my question, how could I get the location of the file of tee.
Why not only using a variable coming from my function activateLogs would you say? Because, this function relaunches the script to be able to get all the logs not caught before the function is called. Thus why using this method to retrieve the error file location.
Possible solutions, but not the best (I hope)
One way would be to pass the file location through a script
parameter, but I would prefer not do that if that can be avoid.
Another, would be to create a "fake" file descriptor #4 (probably my best solution up to now) that would always point to the file.
Does anyone have an idea?
I finally opted for the creation of a "fake" file descriptor #4 that does not nothing except pointing to the current log file.

Gearman: troubles with first use

I tried to start with Gearman. After downloading and setting it, gearman_version() works. But, when I start server and try to init worker like so:
php myFileName.php &
I see the code:
And when I init the client, I see code too. What am I doing wrong?
i don`t know why, but the examples from youtube was not correct in my case. The first scripts, which worked I get from http://php.net/manual/ru/gearman.examples-reverse-bg.php
You likely have short open tags disabled on your install. Notice, running a php file that doesn't actually contain any PHP will just echo the contents of the file.
>$ echo 'hello' > text.php
>$ php text.php
hello
>$
You can verify the setting for your install with the following
>$ php -i | grep "short_open_tag"
short_open_tag => On => On
If tags are On you're all set.

shell script to check the sybase iq status

I am writing a script to check whether sybase is running on my server. If it is not running, i want to start the service. If it is running, i want to stop the sybase iq.
Please help me doing the same.
The logic i have written is :
if(sybaseiq = active)
then
stop_iq
else
start_iq ".cfg" ".db"
Below is the code which I found on internet.But i am not able to understand what they are doing there. Please answer me with explanation.
isql -U${USERNAME} -P${PASSWORD} -S${SQL_SERVER} -w1000 << ! > ${LOG_FILE}
exit
!
if [[ $? != 0 ]]
then
msg="`date` ${SQL_SERVER} problem. ${SQL_SERVER} on ${HOST} is down or cannot be accessed"
cat ${LOG_FILE}|/usr/bin/mailx -s "${msg}" ${SUPPORT}
}
exit 1
fi
Thanks a lot in advance
The script is fairly straight forward
First the script logs into the server via isql, redirecting the output to a log file. If it's able to connect, it issues all the commands between the exclamation points, which is an exit in this case.
Next the if statement checks the error status of the last command run $?. 0 indicates no error, anything else indicates an error. So if the error is not 0, then create a message, then send that message, along with the log file to someone.
You will have to set the values for $USERNAME, $PASSWORD, $SQL_SERVER, $LOG_FILE, $HOST and $SUPPORT somewhere in your script.
If you are not familiar with shell scripts, I would recommend you read up a bit. It's quite easy to get into, but they are quite powerful for managing *nix systems.

Running TreeTagger

I'm attempting to run TreeTagger using the French parameter file but I am getting a permission denied error with the french-utf8.par file
[bash]:echo cmd/tree-tagger-french-utf8 | lib/french-utf8.par
[bash]:lib/french-utf8.par: Permission denied
This is quite similar to this question here (TreeTagger installation successful but cannot open .par file), but I'm able to run the tagger like this:
[bash]: echo 'Bonjour' | cmd/tree-tagger-french-utf8
reading parameters ...
tagging ...
Bonjour NOM bonjour
finished.
I've tried changing to echo bin/tree-tagger, but I get the same error. Any ideas on what I am doing wrong?
echo cmd/tree-tagger-french-utf8 will print the string cmd/tree-tagger-french-utf8 and you're piping that to a par file. That cannot work, you need to pipe a file to a command, like in your second example but using cat filename (instead of echo 'Bonjour') if you want to feed a file to the TreeTagger.
Thats strange, I know this may seem obvious but have you tried running that command as sudo?
The "Permission Denied" hints that there is some permission problem.

KornShell (ksh) redirection

I have a script which redirects std out/std err as below:
SCRIPTS=/test/scripts
LOG=/test/log
echo $SCRIPTS
echo $LOG
$SCRIPTS/dmm_algo_ofac_daily_sched.ksh >> $LOG/test12.log 2>&1
This script is not able to expand $SCRIPTS and $LOG
If I replace it as below:
/test/scripts/daily_sched.ksh >> /test/log/test12.log 2>&1
It complains as below:
: bad file unit numberd/test.ksh: line 33: 1
Also I am not able to invoke the script from the directory where it is saved. If I do
./test.ksh it gives me error saying file not found. I am able to execute it via ksh /test/sched/test.ksh though.
Can someone help me with these. Thanks in advance.
I'm almost certain that the problem is because of DOS/Windows line endings
The error message you are getting is overwriting itself because of a carriage return. You can fix your file using dos2unix.
Add magic #!/bin/ksh to the first line to invoke directly without naming the interpreter on the command line.
I'll conjecture wildly that your root cause(s) has (have) nothing to do with redirection.
Is the script you've exhibited /test/sched/test.ksh or /test/scripts/test.ksh? Are you certain?

Resources