Error running grep on Remote Servers


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Error running grep on Remote Servers

Im running the below command

Code:
sshpass -p mypassword ssh -t user1@server2 /bin/bash -c 'echo "mypassword" | sudo -S -l; echo "$?#`grep -iE "user66|dbuser|tomcat|splunk|stash|jira|user2|docadmin" /etc/passwd`"; exit'

Below is the error I get: Output:

Quote:
User user1 may run the following commands on server2:
(ALL) ALL, !/bin/su
ksh: user66: not found
ksh: dbuser: not found
ksh: tomcat: not found
ksh: splunk: not found
ksh: stash: not found
ksh: jira: not found
ksh: ^J: not found [No such file or directory]
ksh: docadmin /etc/passwd: not found [No such file or directory]
I run this command across a set of 500+ servers.

I also tried using egrep but it still did not work.

Note: Strangely grep -iE "user66|dbuser|tomcat|splunk|stash|jira|user2|docadmin" /etc/passwd works fine with ksh shell when i manually fire it from the remote server2

I m looking for a solution by which I could enforce to either use bash shell on remote servers or have a grep / egrep / fgrep / ggrep or by specifying complete path to a particular grep command that works across different flavors or unix Linux servers.

Last edited by mohtashims; 01-18-2019 at 12:42 PM..
# 2  
Quote:
Originally Posted by mohtashims
Code:
sshpass -p mypassword ssh -t user1@server2
 /bin/bash -c 'echo "mypassword" | sudo -S -l; echo "$?#`grep -iE "user66|dbuser|tomcat|splunk|stash|jira|user2|docadmin" /etc/passwd`"; exit'

Note: Strangely grep -iE "user66|dbuser|tomcat|splunk|stash|jira|user2|docadmin" /etc/passwd works fine with ksh shell when i manually fire it from the remote server2
Sigh, here we go again: have you actually read what i wrote in response to your problem yesterday? Now you cram (again) fifteen commands into one commandline and wonder why you get confused - honestly, i would get confused too and i think i know a bit or two about shell programming.

Actually, strangely, you explicitly fire up a bash, which is not a ksh and that two different shells behave differently sometimes is not "strange", it is no wonder.

Second, you seem to think that quotations can be nested:

Code:
 /bin/bash -c '... "..." | ... ; ... "...`... "..." ...`"; ...'

Inside a single-quoted string a double-quote character is just a normal character (besides: and vice versa). That is:

Code:
"    '      "     and     '    "     '

are just two quoted strings, a double-quoted one and a single-quoted one. The single-quotes in the first and the double-quotes in the second are just one more character.

Third, you have now read probably a hundred times that YOU SHOULD NOT USE BACKTICKS! How often does someone have to point that out to you until you realize that some of us actually know what we are talking about? It is utterly frustrating to solve the same problem over and over again for you because nothing anybody here is telling you seems to have any lasting effect. By the way: also the backticks are just simple characters with no meaning at all inside a single-quoted string. Try this on the commandline:

Code:
echo '`date`'

and you will see a simple string `date` in the output, not the date itself.

Alas, i suppose you will ignore what i write again, so i spare you (and me) advice on how to do better what you want to do. You won't heed my advice anyway.

bakunin
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #578
Difficulty: Medium
Lists are ordered collections that are essentially static arrays.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for remote servers

Hi , I have written a small script : set -x #!/bin/ksh for i in `cat /tmp/list` ( list contains remove servers ) do ssh -t $i << EOF uname -a cd ~user echo "Enter the dir >" read dir path=`ll -ld /home/user/"$dir"` if ; then echo "Dir exists " read rm $path else echo "no such... (9 Replies)
Discussion started by: kpatel786
9 Replies

2. Shell Programming and Scripting

Running set of commands in remote servers in shell script

Hi Wishing to all. I am very new joined in an organization as a unix system administrator. I need a help in preparing a script for a report. i have a file contains all of the linux/ubuntu servers line by line around 140 servers. vi servers.txt nh01 nh02 nh03 bh01 bh04 - - :wq (3 Replies)
Discussion started by: kumaraswamy
3 Replies

3. Shell Programming and Scripting

Remote login and running a script on multiple servers

Hi all, I am baffled on this. Solaris Irix system.:confused: I have 4 servers all connected to one another, :b: I need to write a script line that would login on to server 1-3 ($HOST) start a script in the back ground and log off while the back ground script runs over a length of time.:eek: ... (10 Replies)
Discussion started by: weddy
10 Replies

4. Shell Programming and Scripting

Checking running process status using "grep" on multiple servers in load sharing system.

Suppose i have 3 different servers say x,y and z. Im running some process say ABC and 40 instances for the same is being created. In load sharing suppose on server x, 20 instances are running server y, 10 instances are running server z, 10 instances are running. While checking the... (1 Reply)
Discussion started by: ankitknit
1 Replies

5. AIX

Connect HMC to remote servers

I'm trying to connect a few servers in different remote locations to my HMC. I added static IPs to the Service Processor through ASM, and did all the necessary network configurations, then connected those servers to our switch. Now my questions are: - in our site, do I need to connect these... (4 Replies)
Discussion started by: Dardeer
4 Replies

6. UNIX for Dummies Questions & Answers

Running the same remote script on multiple servers

Experts, Im trying to remote into a server, run a script that resides on that server and capture the information displayed & store in a local file. I struggled with this yesterday & finally that script is working now. Now, here is a scope creep and the script that I wrote for 1 remote... (2 Replies)
Discussion started by: OMLEELA
2 Replies

7. UNIX for Dummies Questions & Answers

Is there a way to Grep processes running on Windows Servers...

Hi guys - newbie question: Is there a way for me to grep running processes from a windows machine? If its possible I could then try to script it. Currently I can see Windows file shares on my Linux server. (4 Replies)
Discussion started by: DallasT
4 Replies

8. Shell Programming and Scripting

Status of remote servers

hi, Say me the command to check the status of server. whether its up or down (11 Replies)
Discussion started by: satheeshkr_cse
11 Replies

9. Shell Programming and Scripting

Copy a file on remote servers

Hey Unix Gurus, I'm having trouble in copying a file on 5 different servers, first how can you do it locally (i.e without the need to ssh to the server you want to copy the file) and if you need to ssh how do u run a command within that server. Please see my code below(it doesn't work somehow).... (10 Replies)
Discussion started by: sexyTrojan
10 Replies

10. Shell Programming and Scripting

Connecting to remote servers

Please advise, The previous thread asked about the automated transfer of files between two servers. The question is - is there any way of encrypting the password within the script or is it a matter of setting the permissions settings on the script so that it can only be executed and possibly... (1 Reply)
Discussion started by: rdbooth
1 Replies

Featured Tech Videos