run remote .SH file over plink

I need to run a remote .sh file, and write the output into a txt file. this is the command i'm trying to execute unsuccessfully, what should i to do run my remote file and get its output locally?

>mytxt.txt set /p a=- RESULT :<NUL
plink -P 22 -l user -pw password machine.extension "cd /HOME/FOLDER/;ls;sh myfile.sh" >> mytxt.txt 2>&1
echo. >> mytxt.txt

Open in new window


thank you in advance!
LVL 1
celticianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omarfaridCommented:
If you run the command without the output/error redirection, do you get output on your screen? If not then there is nothing to redirect.

Do you get any thing at all in mytxt.txt ?
0
arnoldCommented:
To omarfarid's point you need to include a redirect within the command
plink -P 22 -l user -pw password machine.extension "cd /HOME/FOLDER/;ls;sh myfile.sh 2\>\&1" >> mytxt.txt

Open in new window

0
celticianAuthor Commented:
Omar: Yes i do get an output in my screen if i log in via putty, i want that ouput in my text file. now i only get the LS

Arnold: I tried with the command changed you suggested and it prints in my txt file that at '(' character is missing.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

arnoldCommented:
It sounds as though the error isrelated to the shell script.
Try putting the entire group of commands in parenthesis.
plink ... "(Cd;ls;sh script.sh) 2\>\&1" >> my txt.sh
Not sure why you do not incorporate the cd/ls within the shell script

I.e. sh command.sh

#!/bin/sh

cd /home/someplace
ls

Do the rest of the stuff within the script
0
celticianAuthor Commented:
Well i actually dont need the ls, and i can't edit the script as i dont have permissions for that, if i did i would.

I tried to add the ( ), and this is the result i get written out to my text file after putting the commands between them:

:ksh: syntax error at line 1 : `2>&1' unexpected

In the previous case as i mentioned, i had this other error:

:ksh: /HOME/FOLDER/:  not found
myfile.sh: syntax error at line 6: `(' unexpected

this is the full script again, i see i forgot to add some info in the previous post:

>mytxt.txt set /p a=- RESULT :<NUL
plink -P 22 -l USERNAME -pw PASSWORD machine.extension "(cd /HOME/FOLDER/;sh myscript.sh) 2\>\&1" >> mytxt.txt 2>&1
echo. >> mytxt.txt

Open in new window

0
arnoldCommented:
the script has an error: myfile.sh: syntax error at line 6: `(' unexpected

can you run the script when you are connected and in the shell via ssh?

/home/folder needs to be replaced with the correct location where it should be.
you used it as a place holder presumably not to disclose your username, but when you tried it, you forgot to change it back to what it needs to be.
0
celticianAuthor Commented:
Yes, i can run the script via putty, it shows a text result, i just want that text out to my .txt file. It seems like plink is executing the out put??
In my file /home/folder is of course with the right folder :-) i checked it all tens of times.

This is the output text shown in putty when executing the sh file:


->Test result:
[OK]  Counted       7 process   (file_a_b_c)
[OK]  Counted        0 errors in (/app/abcd/tmp/log/2013123456_K7_CO.log)

It's just that short text that i need in my file.
0
arnoldCommented:
replace the double quotes with single quotes.
>mytxt.txt set /p a=- RESULT :<NUL
plink -P 22 -l USERNAME -pw PASSWORD machine.extension '(cd /HOME/FOLDER/;sh myscript.sh) 2\>\&1' >> mytxt.txt 2>&1
echo. >> mytxt.txt

Open in new window

0
celticianAuthor Commented:
Still no luck, im getting this error when replacing double with single quotes:

"1'' is not recognized as an internal or external command,
operable program or batch file.
 
:(
0
arnoldCommented:
There is somethng that is being lost.
The single quote you use is not the Curley single (execute bit)
Can you limit the command to the shell script only?
0
celticianAuthor Commented:
ok, you mean not writing it to a text right?
0
arnoldCommented:
Only run plink ... '/full/path/to/myscript.sh 2\>\&1' >> my text.log

You can run without writing to make sure it works.
0
celticianAuthor Commented:
This question is not abandoned, the reason i have not answered yet is that i couldnt log into that machine so far at my work, tomorrow i should be able to, or anyday this week :)
0
celticianAuthor Commented:
Arnold, if i run plink  'full/path/to/my/scripts.sh" >>my_text.log

it keeps returning this error:

"Unable to open connection:
Host does not exist"

I really dont know what to do.
0
celticianAuthor Commented:
If i run

plink -P 22 -l user-pw password machine.extension "sh myscript.sh" >> my_text.log 2>&1
pause

i get in the log file:

myscript.sh: syntax error at line 6: `(' unexpected


It seems to me its like interpreting the text within the output of the script and ot considering it just a text, but commands.
0
arnoldCommented:
Try instead of using double quotes (") a single quote (') surrounding the command to be executed on the other side.
You should also use full path /bin/sh /path/to/myscript.sh
0
celticianAuthor Commented:
I tried it all, single and double quotes, relative and absolut path. With single quotes the file is not being found, with double quotes its being found, but it returns the error above:

myscript.sh: syntax error at line 6: `(' unexpected

I really don't know what else to try.
0
arnoldCommented:
when you are on the system and run the script, what is the command you give exactly?
./myscript.sh
sh myscript.sh?
what is the first line in the myscript.sh?
which user are you logged in with when you run the script locally?
0
celticianAuthor Commented:
This is the full code within myscript.sh :

#!/bin/bash
#
#set -x


function do_test_process() {
  local S_PROC="abcd"
  local LIM_PROCS=76
  local USER="user"
  local N_PROCS=$( ps -ef|grep ${USER}|grep ${S_PROC}|grep -v grep|wc -l )
  local RES="[NOK]"
 
  if [ ${N_PROCS} -eq ${LIM_PROCS} ];then RES="[OK]";fi

  printf "%-5s asdb%-8s process   (%s)\n" "${RES}" "${N_PROCS}" "${S_PROC}"
}

function do_test_log() {
  local R="/app/folder/folder/log"
  local F="${R}/"`ls -rt ${R}|tail -1`
  local LIM_ERR=3
  local N_ERR=$( grep WAITSEM ${F}|wc -l );
  local RES="[OK] "
  
  if [ ${N_ERR} -ge ${LIM_ERR} ];then  RES="[NOK]";fi

   printf "%-5s asdfb %-8s asdf in (%s)\n" "${RES}"  "${N_ERR}" "${F}"
}

echo "->Result:"
do_test_process
do_test_log
date

Open in new window


I am logged with the same user over plink or putty,
0
arnoldCommented:
See whether the use of '/bin/bash /path/to/your/myscript.sh'

My current guess is that your script when run via plink is run by sh

When you login run the script as sh ./myscript.sh that will mimic your line 6 error.

The initial suggestion deals with explicitly using bash as the shell under which the script will run.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
celticianAuthor Commented:
I won't have access to that machine till next sunday, so ill consider this answer the good one,  if thisd oesn't work i think ill give it up :(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.