Send command via ssh to another server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Send command via ssh to another server
# 1  
Old 10-23-2014
Send command via ssh to another server

Hello,

i`m tryeing to execute loop on remote server and i have problems with syntax

my command is

Code:
ssh root@server "for i in /vz/private/*; do b=`ls -la $i/usr/bin/crontab | awk '{print $1}'`; echo $b; done"


with this command i`m tryeing to get all permissions of file /usr/bin/crontab
this file can be found in any subdirectory of directoryes in this folder /vz/private/

when i execute that command in remote server it works fine, but when i try to send loop via ssh to that remote server then i`m getting empty values

---------- Post updated at 04:33 AM ---------- Previous update was at 04:25 AM ----------

dont need it any more, i got it working with command:

Code:
ssh root@server "for i in /vz/private/*; do ls -la $i/usr/bin/crontab | awk '{print $1}'; done"


Last edited by Don Cragun; 10-23-2014 at 07:00 AM.. Reason: Add CODE tags.
# 2  
Old 10-23-2014
A more sensible approach, rather than trying to "make it work" as one-liner:
1. put the working command into a script
2. chmod +x the script file
3. scp -p the script to the remote box
4. ssh remoteserver '/path/to/script'

You now have a general model to do anything on the other side. You can automate: put steps 1-4 in a local script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Command to check if the server is not reachable using ssh

I have list for servers say server1, server2, server3.....server20 I want to test the ssh connectivity of each server, if any one of the server is down then ssh connectivity fails and the script results something like echo "serever is not reachable" Now I m confused using which command shall I... (2 Replies)
Discussion started by: sam@sam
2 Replies

2. Shell Programming and Scripting

Execute command using ssh server 'cmd'

Hi The command below does not work as it require to take command in the breakers But If I do so the variable values get lost ssh testserver01 'dsmc q b "${ARCHIVE_DIR}*" -sub=yes -querysummary -inactive -fromd="${BACKUP_DATE}"' Thank you. (3 Replies)
Discussion started by: zam
3 Replies

3. Shell Programming and Scripting

Connect (SSH) to Windows server via Linux server through a script and passing command.. but failing

I am trying to connect to Windows server via Linux server through a script and run two commands " cd and ls " But its giving me error saying " could not start the program" followed by the command name i specify e g : "cd" i am trying in this manner " ssh username@servername "cd... (5 Replies)
Discussion started by: sunil seelam
5 Replies

4. UNIX for Dummies Questions & Answers

ssh command to execute shell script in another server

ssh -q <hostname> /opt/tcs/satish/tst.ksh ssh -q <anotherserver> /opt/tcs/satish/tst.ksh tst.ksh has "nohup <command> & " when i execute below script , its throwing error as nohup can not be found ssh -q <anotherserver> /opt/tcs/satish/tst.ksh > log & can someone let me... (5 Replies)
Discussion started by: only4satish
5 Replies

5. Shell Programming and Scripting

no command is not working to send a mail in linux redhot server

HI All, I am facing the problem with rmail ... Actually my server is linux environment Redhot server. To send a mail which command I have to use it . I want subject and I want to Include CC also And body of the mail also there . But If I use rmail command it is not taking any options to... (1 Reply)
Discussion started by: ksrivani
1 Replies

6. UNIX Desktop Questions & Answers

ssh command doesnot excute commands in the destination server

Hi All, I have the below code where Iam connecting from xzur111pap server to xzur0211pap server thru ssh to execute some commands. ssh xzur0211pap spaceleft=`df -k /home |tail -1 | awk '{print $5}'` spaceleft=${spaceleft%\%} if ]; then echo "ALERT : HUFS(/home $spaceleft)" exit 0... (3 Replies)
Discussion started by: gaddamja
3 Replies

7. Shell Programming and Scripting

could not send commands SSH session with Net::SSH::Expect

I am using Net::SSH::Expect to connect to the device(iLO) with SSH. After the $ssh->login() I'm able to view the prompt, but not able to send any coommands. With the putty I can connect to the device and execute the commands without any issues. Here is the sample script my $ssh =... (0 Replies)
Discussion started by: hansini
0 Replies

8. Fedora

ssh command to read server resources

what is the ssh command to read my server resources, like system operator, Ram installed, CPU etc....? (12 Replies)
Discussion started by: dan8354544
12 Replies

9. Shell Programming and Scripting

Retrive the value returned by a command excuted in a remote server using ssh

Hello Everybody, I'm facing a weird problem with the awk command. I try to retrieve in a variable the value returned by a simple ls command. ls /export/home/tmp |tail -1 return a good value (the name of the . But When I try to execute the same command in a remote server using ssh as... (2 Replies)
Discussion started by: Jabarod
2 Replies

10. Shell Programming and Scripting

script to send command periodically to remote server

Hi, I'm wondering if there's a way to send a command periodically to remote server through a script. Right now I have this: keepLooping=1 ssh user@domain while (( keepLooping == 1 )) do echo a sleep 3 done but what this does is ssh to the server, and only when the connection is... (2 Replies)
Discussion started by: sayeo
2 Replies
Login or Register to Ask a Question