LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2013, 09:54 PM   #1
AshwiniIngale
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Rep: Reputation: Disabled
How to execute multiple commands on remote server


hi All,

I am trying to connect to other machine using ssh and want to execute multiple commands remotly. I am using below script for he same. But instead of executing it on other server, the commands are being executed on server I am executing it from. Coud you please help me to know where m doing wrong.

#!/bin/bash
set -x
file="/app/rdg/release/thirdparty/EM/monitor/process_monitor/sample.txt"
while read line
do
server=`echo "$line" | awk -F'|' '{ print $1 }'`
uname=`ssh -n aghadge2@$server "uname"`
reg_expr=`echo "$line" | awk -F'|' '{ print $9 }'`
reset_interval=`echo "$line" | awk -F'|' '{ print $4 }'`
ssh -t $server << EOF
pid=`ps -ef | grep '$reg_expr' | grep -v "grep"| awk '{ print $2 }'`
P_Run_Time=`perl -e '$mtime=(stat("/proc/$pid"))[9]; print $mtime;'`
Cur_Time=`perl -e '$cur_time=time(); print $cur_time'`
Run_Time=`expr "$Cur_Time"-"$P_Run_Time" | bc`
EOF
echo "$P_Run_Time" >> Ash123
echo "$Cur_Time" >> Ash123
echo "$Run_Time" >> Ash123
done <"$file"
 
Old 01-16-2013, 12:35 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
if you already started perl, you could implement those lines in one single perl script. stdout will be redirected to the current host, so something like:
ssh -t $server "perl -e 'some script to print data'" >> Ash123
should work.
 
Old 01-17-2013, 03:08 AM   #3
AshwiniIngale
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
Thanks pan64.. But unfortunatley its not working.. I am getting error as
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.
And the output file have nothing..

Plz help!!
 
Old 01-17-2013, 04:03 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by AshwiniIngale View Post
Thanks pan64.. But unfortunatley its not working.. I am getting error as
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.
And the output file have nothing..

Plz help!!
Can you post the exact code you wrote? And use the [code] [/code] tags to preserve formatting.
Also, pay attention to what variables exist in the local shell and which exist in the remote shell. You can't just run

Code:
foo=bar
on the remote machine and then use $foo on the local machine and expect to get anything reasonable.
Also, pay attention to where your variable names are interpreted. For example, anything in a heredoc that has a $ in it gets substituted by bash before it can even reach ssh. You have to escape those $. Single quotes won't protect them in heredoc.

Last edited by millgates; 01-17-2013 at 04:04 AM.
 
Old 01-17-2013, 05:02 AM   #5
AshwiniIngale
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
Thanks Millgates. Since m a newbee for unix m pretty much unaware of many thumb rules here. Below is the exact code I executed.


#!/bin/bash
set -x
file="/app/rdg/release/thirdparty/EM/monitor/process_monitor/sample.txt"
while read line
do
server=`echo "$line" | awk -F'|' '{ print $1 }'`
uname=`ssh -n $server "uname"`
reg_expr=`echo "$line" | awk -F'|' '{ print $9 }'`
reset_interval=`echo "$line" | awk -F'|' '{ print $4 }'`
pid=`ps -ef | grep '$reg_expr' | grep -v "grep"| awk '{ print $2 }'`
ssh -t $server "perl -e '$mtime=(stat("/proc/$pid"))[9]; print $mtime;'" >> Ash123
###P_Run_Time=ssh -n "perl -e '$mtime=(stat("/proc/$pid"))[9]; print $mtime;'"
###$cur_time=time(); print $cur_time - $mtime;'`
###echo "$P_Run_Time" >> Ash123
done <"$file"
 
Old 01-17-2013, 05:14 AM   #6
AshwiniIngale
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
I will post the error code in brief..

Last edited by AshwiniIngale; 01-17-2013 at 05:17 AM.
 
Old 01-20-2013, 11:23 PM   #7
AshwiniIngale
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
The error code is as below:
+ file=/app/rdg/release/thirdparty/EM/monitor/process_monitor/sample.txt
+ read line
++ echo 'lnl07a-0106||250|2|mux|/app/rdg/release/scripts/Mux|IRS Trades Multiple xer|mux -p "IRS Trade Publisher"|Mux -S /app/rdg/local/sockets/Mux_IRS Trade Pub lisher -m'
++ awk '-F|' '{ print $1 }'
+ server=lnl07a-0106
++ ssh -n lnl07a-0106 uname
UNAUTHORISED ACCESS TO THIS SYSTEM IS STRICTLY PROHIBITED

All data and information held on or in or generated by this
system is proprietary and confidential. Any unauthorised
access to, or use or disclosure of, such information is
strictly prohibited, and may result in legal action against
you, including but not limited to criminal or civil
prosecution, to the full extent permitted by applicable
law.

All use of this system is subject to monitoring, retention
and disclosure to the extent permitted or required by
applicable law without further notice to you, and accessing
the system constitutes your consent to such monitoring,
retention and disclosure.
+ uname=Linux
++ echo 'lnl07a-0106||250|2|mux|/app/rdg/release/scripts/Mux|IRS Trades Multiple xer|mux -p "IRS Trade Publisher"|Mux -S /app/rdg/local/sockets/Mux_IRS Trade Pub lisher -m'
++ awk '-F|' '{ print $9 }'
+ reg_expr='Mux -S /app/rdg/local/sockets/Mux_IRS Trade Publisher -m'
++ echo 'lnl07a-0106||250|2|mux|/app/rdg/release/scripts/Mux|IRS Trades Multiple xer|mux -p "IRS Trade Publisher"|Mux -S /app/rdg/local/sockets/Mux_IRS Trade Pub lisher -m'
++ awk '-F|' '{ print $4 }'
+ reset_interval=2
++ ps -ef
++ grep '$reg_expr'
++ grep -v grep
++ awk '{ print $2 }'
+ pid=
+ ssh rdgadmin@lnl07a-0106 'perl -e '\''=(stat(/proc/))[9]; print ;'\'''
UNAUTHORISED ACCESS TO THIS SYSTEM IS STRICTLY PROHIBITED

All data and information held on or in or generated by this
system is proprietary and confidential. Any unauthorised
access to, or use or disclosure of, such information is
strictly prohibited, and may result in legal action against
you, including but not limited to criminal or civil
prosecution, to the full extent permitted by applicable
law.

All use of this system is subject to monitoring, retention
and disclosure to the extent permitted or required by
applicable law without further notice to you, and accessing
the system constitutes your consent to such monitoring,
retention and disclosure.
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.
+ read line
 
Old 01-21-2013, 12:55 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
instead of pid=`ps -ef | grep '$reg_expr' | grep -v "grep"| awk '{ print $2 }'` you can use:
pid=`pgrep -f '$reg_expr'`
also instead of perl -e '$mtime=(stat("/proc/$pid"))[9]; print $mtime;' you can simply write:
perl -e 'print (stat("/proc/$pid"))[9];'
the problem with your ssh is the following:
ssh -t $server "perl -e '$mtime=(stat("/proc/$pid"))[9]; print $mtime;'" >> Ash123
you need to protect the " signs inside the command, so use \", but better would be to construct the command before executing it:
Code:
# you need to find the pid on the server, not on the current host
pid=`ssh $server "pgrep -f '$reg_expr'"`

CMD="perl -e 'print (stat(\""/proc/$pid"\"))[9];"
echo $CMD  # just to check
ssh -t $server "$CMD"
actually it was not tested, so probably you need some adjustment...
 
Old 01-21-2013, 04:36 AM   #9
AshwiniIngale
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
Hi Pan64,

Thanks for the help! But now with above corrections I can see I am getting two pid's assigned for the variable and hence the follwed by execution isnt working. Could you please again have a look of the error here...
+ pid='3908
13409'
+ ABC='perl -e '\''print (stat("/proc/3908
13409"))[9];'\'''
+ echo 'perl -e '\''print (stat("/proc/3908
13409"))[9];'\'''
perl -e 'print (stat("/proc/3908
13409"))[9];'
+ ssh -t lnl07a-0106 'perl -e '\''print (stat("/proc/3908
13409"))[9];'\'''
Pseudo-terminal will not be allocated because stdin is not a terminal.
UNAUTHORISED ACCESS TO THIS SYSTEM IS STRICTLY PROHIBITED

All data and information held on or in or generated by this
system is proprietary and confidential. Any unauthorised
access to, or use or disclosure of, such information is
strictly prohibited, and may result in legal action against
you, including but not limited to criminal or civil
prosecution, to the full extent permitted by applicable
law.

All use of this system is subject to monitoring, retention
and disclosure to the extent permitted or required by
applicable law without further notice to you, and accessing
the system constitutes your consent to such monitoring,
retention and disclosure.
Unmatched '.
Unmatched ".
+ read line
 
Old 01-21-2013, 04:48 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you can try:
Code:
pids=(`pgrep -f '$reg_expr'`)
pid=${pids[0]} # or pid=${pids[1]}
or probably you need to check why do you have two processes
 
Old 01-22-2013, 12:40 AM   #11
AshwiniIngale
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
Thanks a lot Pan64.. All your corerctions have helped me to understand unix in better way, but unfortunately m still messing up with few quotes in code..
Their is an cimpilation eeror in code.. which is aborting the operation.. The ssh'ing to server is interpreted as:

+ ssh -t lnl07a-0106 'perl -e '\''print (stat("/proc/13409"))[9];'\'''

which is giving an error:
syntax error at -e line 1, near ")["
Execution of -e aborted due to compilation errors.
+ read line

and now the code looks like:
#!/bin/bash
set -x
file="/app/rdg/release/thirdparty/EM/monitor/process_monitor/sample.txt"
while read line
do
server=`echo "$line" | awk -F'|' '{ print $1 }'`
uname=`ssh -n $server "uname"`
reg_expr=`echo "$line" | awk -F'|' '{ print $9 }'`
reset_interval=`echo "$line" | awk -F'|' '{ print $4 }'`
pid=`ssh $server "ps -ef | grep '$reg_expr' | grep -v grep"| awk '{ print $2}'`
ABC="perl -e 'print (stat(\"/proc/$pid\"))[9];'"
echo "$ABC"
ssh -t $server "$ABC"
done <"$file"
 
Old 01-22-2013, 04:02 AM   #12
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Try another pair of parentheses:

Code:
perl -e '\''print ((stat("/proc/13409"))[9]);'\''
 
Old 01-22-2013, 09:32 PM   #13
AshwiniIngale
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
what I tried is as below:

ABC="perl -e '\''print (stat("/proc/$pid"))[9];'\'"

And still it wont work.. plzz plz help..
 
Old 01-24-2013, 07:07 PM   #14
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
I generally suggest that, when you need to execute "multiple commands," you should put those into a script on that server, then execute the script.
 
1 members found this post helpful.
Old 01-26-2013, 12:54 PM   #15
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
#!/bin/bash
set -x
file="/app/rdg/release/thirdparty/EM/monitor/process_monitor/sample.txt"
while read line
do
server=`echo "$line" | awk -F'|' '{ print $1 }'`
uname=`ssh -n $server "uname"`
reg_expr=`echo "$line" | awk -F'|' '{ print $9 }'`
reset_interval=`echo "$line" | awk -F'|' '{ print $4 }'`
pid=`ssh $server "ps -ef | grep '$reg_expr' | grep -v grep"| awk '{ print $2}'`
ABC="perl -e 'print (stat(\"/proc/$pid\"))[9];'"
echo "$ABC"
ssh -t $server "$ABC"
done <"$file"
At what point in the above code do you expect some (unknown) shell on a remote host to begin executing the commands? Why?
As sundialsvcs suggests, put the series of commands into one script that can be specified as a single commandline argument to the ssh client.
Again, please post source code in [CODE] [/CODE] tags so we can read it like you wrote it and so special characters don't get mangled into smileys and other non-text.
--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Expect To Execute Remote Commands samarudge Linux - Software 3 09-22-2011 04:10 AM
Execute script on local server as normal user to run commands on remote server ALInux Linux - Software 1 01-01-2010 06:30 AM
Execute commands on remote system madhi Linux - Software 3 06-20-2008 10:31 AM
Execute commands or scripts on a remote server gurl4sh25 Linux - Software 3 06-27-2007 09:04 PM
execute multiple ssh remote commands tom221 Linux - Newbie 2 01-28-2005 01:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration