Running a C Program in Bash Script - c

When I run a C program in this bash script it returns the error.
ssh -n -f *.*.*.* "cd /home/sth/remote && echo "$1" && det=$(./ossec-rootcheck)">/home/sthh/res
Error:
./ossec-rootcheck: No such file or directory
I want to ssh to a remote machine and then run a program on it. I know that this file is located in that path because when I edit it as you see, it works.
ssh -n -f *.*.*.* "cd /home/sth/remote && echo "$1" && ./ossec-rootcheck">/home/sthh/res
and as it echo $1 I can see that it does cd /home/sth/remote right. But I want the return value of that program to be stored in a variable,for example det.

ssh -n -f *.*.*.* "cd /home/sth/remote; echo "$1"; ./ossec-rootcheck || do_your_work">/home/sthh/res
You don't have to store it in a variable.
|| executes do_your_work if the exit status of ossec-rootcheck != 0
If you want to store the numeric exit status in a variable, or echo it, you can do (with proper escaping):
./ossec-rootcheck; ecode=$?; echo $ecode

To get the return code or exit code of the remote code:
ssh -n -f *.*.*.* "cd /***/***/remote && echo \"$1\"; ./ossec-rootcheck; echo \$?">/home/ossl7/res
To capture errors as well:
ssh -n -f *.*.*.* "exec 2>&1; cd /***/***/remote && echo \"$1\"; ./ossec-rootcheck; echo \$?">/home/ossl7/res
Also, you probably need to omit && echo \"$1\" when you find it to be working already. And you could just use single quotes for that:
ssh -n -f *.*.*.* 'cd /***/***/remote; ./ossec-rootcheck; echo $?' >/home/ossl7/res
Or
ssh -n -f *.*.*.* 'exec 2>&1; cd /***/***/remote; ./ossec-rootcheck; echo $?' >/home/ossl7/res

Related

Can PSCP retrieve file from multiple servers using an array of ip and looping?

I get more than one remote source not supported error on pscp if my script is written like this (no issues with plink) :
I want to retrieve files from multiple UNIX servers to local windows
Could someone help me to verify my code?
#Server Information:
$Server_IP=#("root#192.168.13.10","root#192.168.13.11")
$PPK_Path="C:\Users\me\Desktop\private-key.ppk"
#Local machine related information
$Dest_Path=#("C:\Users\me\Desktop\savehere01\","C:\Users\me\Desktop\savehere02\")
#Commands //Change with cautious
For ($i=0; $i -le 2; $i++) {
#Prompt computer to start plink.exe to insert private key and enable ssh
Echo "n" | plink -ssh -i $PPK_Path $Server_IP[$i]
#Prompt Powershell to run scp
pscp -r $Server_IP[$i]:/cf/conf/backup/* $Dest_Path[$i]
}
However, if i run my script as below, i am able to retrieve files from multiple servers to one single local host.
Echo "y" | plink -ssh -i C:\Users\me\Desktop\private-key.ppk root#192.168.13.32
pscp -pw testing -r root#192.168.13.10:/cf/conf/backup/* C:\Users\me\Desktop\savehere\
Echo "y" | plink -ssh -i C:\Users\me\Desktop\private-key.ppk root#192.168.13.11
pscp -pw testing -r root#192.168.13.11:/cf/conf/backup/* C:\Users\me\Desktop\savehere02\
EDIT
foreach ($IP in $Server_IP){
#Prompt computer to start plink.exe to insert private key and enable ssh
Echo "y" | plink -ssh -i $PPK_Path $IP
#Prompt Powershell to run pscp
pscp -pw testing -r $IP":"/cf/conf/backup/* C:\Users\me\Desktop\savehere\
}
I found some erros in your code:
1)Put this line
$Server_Username[$i]+"#"+$Server_IP[$i]
instead
${Server_Username[$i]}#${Server_IP[$i]}
2)You have only 2 elements in array
your loop must be
($i=0; $i -lt 2; $i++)
3)You can use just this construction
$Dest_Path[$i]
instead
${Dest_Path[$i]}
Additional:
#Commands //Change with cautious
For ($i=0; $i -lt 2; $i++) {
#Prompt computer to start plink.exe to insert private key and enable ssh
Echo "n" | plink -ssh -i $PPK_Path $Server_IP[$i]
#Prompt Powershell to run scp
pscp -r $Server_IP[$i]:/cf/conf/backup/* $Dest_Path[$i]
}
Try this fix

Using sqlcmd in a batch script to run multiple scripts with output files

I need to run about 50 scripts in a folder using sqlcmd from a batch file. Each script's query results need to be sent to its own output file. I have a working batch file that just runs each from a separate line:
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%SCRIPTFOLDER%\master_departments.sql" -s "|" -o "%OUTPUTFOLDER%\master_departments.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%SCRIPTFOLDER%\master_companies.sql" -s "|" -o "%OUTPUTFOLDER%\master_companies.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P %PASSWORD% -i "%SCRIPTFOLDER%\bill_history.sql" -s "|" -o "%OUTPUTFOLDER%\bill_history.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P %PASSWORD% -i "%SCRIPTFOLDER%\episodes.sql" -s "|" -o "%OUTPUTFOLDER%\episodes.csv" -W
Is there any way to run this in some kind of loop? I've seen examples that run a loop of all SQL scripts in a folder, but nothing that I've seen does it with an output file set.
Per #LotPings' suggestion I used the below code:
set INSTANCE=<someinstance>
set DATABASE=<somedb>
set USERNAME=<someuser>
set PASSWORD=<somepassword>
set "SCRIPTFOLDER=D:\<pathToScripts>\"
set "OUTPUTFOLDER=D:\<pathForOutput>\"
#Echo off
For /F "tokens=*" %%S in ('Dir /B "%SCRIPTFOLDER%*.sql" '
) do echo sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
#pause
I ran that in a batch file and when it paused, the last line said, "The system cannot find the file specified."
Thinking it was perhaps the backslashes in my paths, I removed them and put a slash before the .sql in the for line, but I got the same results.
Removing the backslash altogether resulted in a "File not found" message when I ran it like that.
In case your output file name matches the script name (without extension)
and your parameters are the same for all scripts
#Echo off
For /F "tokens=*" %%S in ('Dir /B "%SCRIPTFOLDER%*.sql" '
) do echo sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
The echo in front of sqlcmd prevents execution and allows to review the output. If all looks OK, remove the echo.
The for variable behaviour can be changed with ~ modifiers, see For /? or visit ss64.com/nt/for.html / syntax-args
To pass a folder to the batch you can input via set /P or hand over via command line arguments.

Using a batch file to call sequential SQL Agent jobs, but how do I end the process if one of the jobs fails?

I'm using a batch file to call sequential SQL Agent jobs. That part works fine, but my current challenge is that I need to stop the process when one of the SQL Agent jobs fails.
Business Requirement:
Step 1: Call SQLAgentJob1 to run on DBServer. If SQLAgentJob1 fails, this batch script should exit (not call other steps). If SQLAgentJob1 succeeds, call SQLAgentJob2.
Step 2: Call SQLAgentJob2 to run on DBServer. If SQLAgentJob2 fails, this batch script should exit (not call other steps). If SQLAgentJob2 succeeds, call the RunMe.cmd.
Step 3: Call RunMe.cmd to run on WebServer
Step 4: Call SQLAgentJob3 to run on DBServer. If SQLAgentJob3 fails, this batch script should exit. If SQLAgentJob3 succeeds, this batch script should exit.
Current Status
I have used the batch file to successfully call each of the SQL Agent jobs and the command line task, but I'm stuck on trying to STOP the process if any one of the SQLAgentJobs fail. Right now, even if SQLAgentJob1 fails, the batch file still calls SQLAgentJob2.
Here is the scripting as it currently stands:
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -Q "sp_start_job 'SQLAgentJob1'"
ECHO starting the SQL job
sqlcmd -S DBServer -E -d MSDB -Q "sp_start_job 'SQLAgentJob2'"
ECHO OFF
ECHO.
set CMDDIR = D:\webapps
pushd CMDDIR
call RunMe.cmd
popd
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -Q "sp_start_job 'SQLAgentJob3'"
Also, very challengingly, even when the SQL Agent Job fails, the %errorlevel% that is returned as part of the batch file for that job is 0 (not 1).
Any ideas/suggestions? Ideal world I'd like to continue using the batch scripting (in part because SQL isn't installed on my webserver, and I need to call the .cmd from my webserver and also run the SQL Agent jobs), although if it's impossible or too difficult, I can change to something else.
Thanks in advance for any help and forgive me if I've used any terminology incorrectly.
I found with a simple search Return value of SQLCMD.
The problem should be (not tested) easily solved using -b parameter on sqlcmd.
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -b -Q "sp_start_job 'SQLAgentJob1'"
if errolevel 1 exit /b
ECHO starting the SQL job
sqlcmd -S DBServer -E -d MSDB -b -Q "sp_start_job 'SQLAgentJob2'"
if errorlevel 1 goto :EOF
ECHO OFF
ECHO.
set CMDDIR = D:\webapps
pushd CMDDIR
call RunMe.cmd
popd
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -Q "sp_start_job 'SQLAgentJob3'"
Or you use -V X option additionally to -b to exit the batch file if an error of level X specified on usage of -V occurs.
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -V 11 -b -Q "sp_start_job 'SQLAgentJob1'"
if errolevel 1 exit /b
ECHO starting the SQL job
sqlcmd -S DBServer -E -d MSDB -V 11 -b -Q "sp_start_job 'SQLAgentJob2'"
if errolevel 1 goto :EOF
ECHO OFF
ECHO.
set CMDDIR = D:\webapps
pushd CMDDIR
call RunMe.cmd
popd
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -Q "sp_start_job 'SQLAgentJob3'"
Please let us know what works best and I will edit my answer accordingly.

Using wgets to connect to sites in a txt file using shell scripting

I am trying to create a script that uses wgets to access every url from a list in a file. However when doing this instead of accessing website.com it will try to connect to website.com/r/n. I know this has to do with text formatting in the text editor but I'm unsure of how to get my program to ignore this. This is the code I have:
#!/bin/bash
for i in `cat $1`
do
wget --spider $i
if wget --spider $i 2>&1 | grep --quiet "200 OK" ; then
echo $i >> connected.txt
else
echo $i >> unsuccesful.txt
fi
rm wget-log
done
Add this to the top of your script
dos2unix "$1"
IHTH

How do I sort a directory full of pictures into sub-directories based on the image name?

I want to take all of the files in /media/mdrive/dump/:
1COD-234355.jpg
MAK-LXT218.jpg
ZIR-CON145.jpg
And create and sort them into the following directories:
/media/mdrive/dump/1/1COD-234355.jpg
/media/mdrive/dump/M/MAK-LXT218.jpg
/media/mdrive/dump/Z/ZIR-CON145.jpg
How would I do that?
This script takes a directory as the first argument and performs what you need:
#!/bin/bash
DIR="$1"
if [ -z "$DIR" ]; then
echo >&2 "Syntax: $0 <directory>"
exit 1
fi
if [ ! -d "$DIR" ]; then
echo >&2 "\"$DIR\" is not a directory"
exit 1
fi
cd "$DIR"
for file in *.jpg *.JPG; do
first=${file::1}
mkdir -p $first && mv $file $first/;
done
head -c xx will return the first xx characters of its input (here, the filename). mkdir -p will skip directory creation if it already exists.
to make two directories you could try something like
dir "/media/mdrive/dump/1/" :: CD would also work here
mkdir folder 1
mkdir folder 2
from here I think you can continue with your IF statements and so forth.
all you need to do is set the dir commands with the Direct path takes the guess work out.
then to check each just do:
start explorer.exe "the folder's path here"
it should open the folder to view the files

Resources