Input to while loop in remote machine using ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Input to while loop in remote machine using ssh
# 1  
Old 12-30-2014
Input to while loop in remote machine using ssh

Hello I am writing a script in a local machine, i am using ssh, here i am not able to using back ticks & input file to while loop. here is my script
Code:
ssh -q server1
a=`ps -ef | grep ccms | awk {print $1}` 
b=`ps -ef | grep mss | awk {print $1}` 
# above lines are not working, so i redirected them to a file to use a while loop
ps -ef | grep ccms | awk {print $1} > /tmp/file.txt
ps -ef | grep mss | awk {print $1} >> /tmp/file.txt

while  read i
do 
echo $i
done</tmp/file.txt
# this is not printing the pid's, but if i use cat i am able to get both values, but i want one value at a time.

Please suggest
# 2  
Old 12-30-2014
Quote:
Originally Posted by nanz143
Hello I am writing a script in a local machine, i am using ssh, here i am not able to using back ticks & input file to while loop. here is my script
Code:
ssh -q server1
a=`ps -ef | grep ccms | awk {print $1}` 
b=`ps -ef | grep mss | awk {print $1}` 
# above lines are not working, so i redirected them to a file to use a while loop
ps -ef | grep ccms | awk {print $1} > /tmp/file.txt
ps -ef | grep mss | awk {print $1} >> /tmp/file.txt
 
while  read i
do 
echo $i
done</tmp/file.txt
# this is not printing the pid's, but if i use cat i am able to get both values, but i want one value at a time.

Please suggest
Hello nanz143,

Could you please change following in your code, hope this helps.
Code:
ps -ef | grep ccms | awk '{print $1}' > /tmp/file.txt
ps -ef | grep mss | awk '{print $1}' >> /tmp/file.txt

You have missed ' while doing print operation. Kindly let me know if this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 12-30-2014
sorry i missed it here.. but i mentioned it in the script.. i am able to redirect the output to /tmp/file.txt, but i have an issue in while loop echo is not printing the values in file.txt ...
# 4  
Old 12-30-2014
Hello nanz143,

I think you should do like below to do so.We can make a script to gather the processes information during ssh.
Let's say we have a scrpit in our local machine which has following.
Code:
cat local_script.sh
ps -ef | grep `whoami` > /tmp/test
while read line
do
        echo $line
done < "/tmp/test"

Now we have main script which is performing ssh to other server then we can call script named local_script.sh
in it as follows.
Code:
cat main_script.sh
ssh -q user_name@server_name 'bash -s' < local_script.sh

As if you will write first ssh command to login to another server then how it will read the next commands in same main script if it is in an another shell/server, so to avoid that we can try the same, hope this will help.


Thanks,
R. Singh

Last edited by RavinderSingh13; 12-30-2014 at 10:32 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 12-30-2014
thanks Singh.. i was able to get some thing like.. when you are in local server and writing a script to work in remote server we need to mention / to the variable in while loop " /i " got it this way.. .

but still wanted to know why i am not able to assign a command to a variable

a=`ps -ef | grep ccms | awk {print $1}` b=`ps -ef | grep mss | awk {print $1}`
can you help me on this please
# 6  
Old 12-30-2014
This depends on what you want to achieve, which still escapes me. Do you want the variables defined on your local machine? Or on the remote host? Where should that while loop be executed, locally or remotely?
If you want to define and use the variables locally, assign them like a=$(ssh server1...) (which might not the most effective way to do things like defining multiple vars).
If you need them remotely, try e.g.
Code:
ssh  server1 <<-EOF
        set -vx
        ps -ef | grep bash | awk '{print $1}'
        a="`ps -ef | grep bash | awk '{print $1}'`"
        echo "\$a"
        EOF

. Make sure the $-sign is escaped if used like this. Or use a script as RavinderSingh suggested.

Usually its more fruitful to describe the target to be achieved in a larger context than to just ask for a single command's correction.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 12-30-2014
yes I want it to run on remote machine... will check and get back to you...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Executing a script in remote machine through ssh

How to execute a script in remote machine through ssh I have a script test.sh which does some backup activity in remote machine. Wanted to keep backup also in remote machine. ssh -l username <remote machine> "commands to be exceuted as ; separted" but how to put the script in the place of... (5 Replies)
Discussion started by: sanvel
5 Replies

2. Red Hat

iptables applied in local machine, can't ssh remote machine after chain changed to DROP

I want to SSH to 192.168.1.15 Server from my machine, my ip was 192.168.1.99 Source Destination was UP, with IP 192.168.1.15. This is LAN Network there are 30 Machine's Connected to the network and working fine, I'm Playing around the local machine's because I need to apply the same rules in... (2 Replies)
Discussion started by: babinlonston
2 Replies

3. UNIX for Dummies Questions & Answers

how to use ssh to run shell script on a remote machine?

how to use ssh to run shell script on a remote machine? ssh user@remote sh ./script.unx i ran the above command ./script.unx HAS NOHUP COMMAND IN ITS BODY, I AM GETTING ERROR AS NOHUP NOT FOUND... i tried to run that script from remote server, its working fine do ineed to set... (6 Replies)
Discussion started by: only4satish
6 Replies

4. Shell Programming and Scripting

Cannot create variables via ssh on remote machine

Hello to all Background info: Local machine : Linux, /bin/bash Remote machine (for the user used for ssh) : SunOs, /bin/ksh (so we have different OS, different Shells) My problem : From the local host i execute $ var=bla $ result=$(ssh -q user@remote-machine " > echo \"this is... (12 Replies)
Discussion started by: black_fender
12 Replies

5. Linux

Create VNC Session on remote machine on which ssh access is denied

Hi Folks, I want to create VNC session on the Remote RHEL machine on which ssh access is denied. Is there any way so that I can create VNC session without ssh access. Let me know all possible ways! (1 Reply)
Discussion started by: gydave
1 Replies

6. Shell Programming and Scripting

executing command in a remote machine through ssh - shell script

Hi All, i have two machines like x and y . my requirement is i should connect to machine Y from x through ssh connection . and do some operation such as copy and move and delete files in Y machine . i tried with this code but it is doing in machine x only . and i need to exit from Y when... (1 Reply)
Discussion started by: rateeshkumar
1 Replies

7. UNIX for Dummies Questions & Answers

ssh to remote machine with 2nd user and password

Hi all, Really hope someone can help me, i have been trying lots of things and just cant seem to nail it - and for something that seems straight forward.... Anyway, scenario is I need to log onto a second machine (remote server) from main workstation. Once logged in I need to run a batch... (2 Replies)
Discussion started by: Hopper_no1
2 Replies

8. Red Hat

Unable to SSH into machine - ssh_exchange_identification: Connection closed by remote host

For a few days now I have been experiencing issues when trying to SSH into 1 of my machine. I get the following output when running 'ssh -vvv': server1:/home/mymadq> ssh -l root -vvv server2 OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003 debug1: Reading configuration data /etc/ssh/ssh_config... (3 Replies)
Discussion started by: jaapar
3 Replies

9. Shell Programming and Scripting

ssh connection from remote machine in solaris

Hi! I have two solaris 10 machines(say 10.1.1.1,10.1.1.2). i have installed rsync on 10.1.1.2, 10.1.1.1::: Sun Microsystems Inc. SunOS 5.10 Generic January 2005 -bash-3.00$ ssh 10.1.1.2 "echo $PATH" Password:... (4 Replies)
Discussion started by: dddkiran
4 Replies

10. Shell Programming and Scripting

executng program on remote machine using ssh

I am trying to search and remove files from a list of servers. I want to find every occurence of this file on each machine and then remove it. If I execute the find command on the remote machine I would like to be able to pipe the output to xargs and remove the file. Does anyone know hat would be... (1 Reply)
Discussion started by: sewood
1 Replies
Login or Register to Ask a Question