ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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..
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.
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
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...
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
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.