0

I want to create a script wherein I can grep a particular word from a specific path of remote server and display that whole grep sentence in the output.

Currently, I have to take access to the target server, then to go in the particular path and then use the grep command.

 #!/usr/bin/ksh

clientID="00000" 

print -n "\nEnter the  ID (Enter ybr for ybr_ybrfndt): $1" 
read clientID
print "$clientID"

for HOST in $(cat qa_servers.txt ) ; 
do 
    ssh $HOST "uname -a" 
    cd /apps/WebSphere/NA70_TBA/config/cells/CellV70_TBA
    grep $clientID resources.xml; 
done
Siva
  • 9,077
  • 1
    welcome to U&L, we are not a scripting services, what have you tried so far ? can you provide (by editing your question) some sample (environment , expected output) ? – Archemar Jan 29 '19 at 09:51
  • #!/usr/bin/ksh

    for server in "${qa_servers.txt[@]}"; { ssh cacher@$server

    cd /apps/WebSphere/NA70_TBA/config/cells/CellV70_TBA untar archive

    clientID="00000"

    print -n "\nEnter the client ID: $1" read clientID print "$clientID"

    grep '$clientID' resources.xml }

    – user334028 Jan 29 '19 at 09:57
  • Put this information in the question, not in a comment. – Panki Jan 29 '19 at 10:04
  • Your code is nonsensical. The for loop does not have a do and you are using variables (some of which are arrays) that you never set. – Kusalananda Jan 29 '19 at 10:21
  • how about this #!/usr/bin/ksh

    clientID="00000"

    print -n "\nEnter the ID (Enter ybr for ybr_ybrfndt): $1" read clientID print "$clientID"

    for HOST in $(cat qa_servers.txt ) ; do ssh $HOST "uname -a" cd /apps/WebSphere/NA70_TBA/config/cells/CellV70_TBA grep $clientID resources.xml; done

    – user334028 Jan 29 '19 at 10:41
  • But with this code I am unable to enter into the target server. I just want to get into the target server and want to get the output of below two commands from target server – user334028 Jan 29 '19 at 10:44

1 Answers1

1

This script should work for your use case.

#!/usr/bin/ksh 

print -n "\nEnter the ID (Enter ybr for ybr_ybrfndt):"
read clientID

while IFS= read -r host
do
    ssh -n $host "uname -a; grep $clientID /apps/WebSphere/NA70_TBA/config/cells/CellV70_TBA/resources.xml;"
done < qa_servers.txt
Haxiel
  • 8,361
  • yes it's working...thanks...but what if i want the same for multiple servers – user334028 Jan 29 '19 at 11:37
  • @user334028 There's a while loop here that reads from the 'qa_servers.txt' file, one line at a time. So the ssh command will be executed for each line in that file. Do you mean something else by 'multiple servers'? – Haxiel Jan 29 '19 at 11:39
  • I just meant that if in the text file i enter the next server name in next line. Will it work ? – user334028 Jan 29 '19 at 11:59
  • @user334028 Yes, it will work. There are no destructive commands involved here, so feel free to experiment. – Haxiel Jan 29 '19 at 12:06
  • nope....it's not working for more than one server – user334028 Jan 29 '19 at 12:10
  • @user334028 You'll need to show me your 'qa_servers.txt` file before I can comment on that. – Haxiel Jan 29 '19 at 13:52
  • the script has stopped working now. have you edited something in it ?? – user334028 Jan 29 '19 at 14:21
  • @user334028 Nope. You can see the edit timestamps on my answer - the last edit was 3 hours ago. – Haxiel Jan 29 '19 at 14:23
  • okie....but it's getting terminated after reading the client id – user334028 Jan 29 '19 at 14:31
  • @user334028 There was a problem with SSH reading stdin earlier, which is why the multiple servers list did not work in your case. I've fixed it now by adding the -n parameter. You can find more details on that problem here: https://unix.stackexchange.com/q/107800/173368 – Haxiel Jan 29 '19 at 14:32
  • #!/usr/bin/ksh

    print -n "\nEnter the ID (Enter ybr for ybr_ybrfndt):" read clientID

    The script is working only till here

    – user334028 Jan 29 '19 at 14:56
  • @user334028 Do keep in mind that I cannot actually see your system, so any advice I can give would be limited. One thing to check is that the redirected file (qa_servers.txt) is not empty. Another thing you can do is to the run the script with ksh -x so that you see the debugging output. – Haxiel Jan 29 '19 at 17:00
  • Thanks. I am able to run the script as expected now. Thanks a ton

    Can you help me with just a small doubt. Is it possible to put a while loop in else statement. If yes can you tell me the syntax for it ??

    – user334028 Jan 30 '19 at 14:46
  • @user334028 Yes, you should be able to use a while loop inside an if-else block. There's nothing special about the syntax here, it's identical to a typical if-else. Also, if my answer has resolved your problem, you may mark it as accepted by clicking the 'tick' mark next to it. – Haxiel Feb 01 '19 at 13:02