Until string from remote command equals value run remote command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Until string from remote command equals value run remote command
# 1  
Old 02-23-2013
set variable value based on if string existing in output

I solved my issue by using the following code

Code:
#!/bin/bash
function GET_STATUS {
     #values Active Passive Failed
     ssh  -a localhost '/home/user/fakecommand.sh'
}

STATE="unknown"
until [[ "$STATE" = "Failed" ]]
do
echo $STATE
sleep 5


STATUS=`GET_STATUS`
echo $STATUS | grep Active && STATE="Active"
echo $STATUS | grep Passive && STATE="Passive"
echo $STATUS | grep Failed && STATE="Failed"
done







bash script: I want to wait until the output from function contains certain values

Note: this function ssh's into a remote server runs a command and outputs data to standard out



Code:
function GET_STATUS {    
     ssh  -a localhost '/home/user/fakecommand.sh'
}

Unfortunately the output from the remote command changes length in both line numbers and sometimes it has extra white space..

It looks something like this....



Running command.....


Code:
variable length:stuff i dont need1                stuff i dont need2                        status                          stuff i dont need3
variable length:stuff that i dont need 1                         stuff i dont need2                        Active                          stuff i dont need3

I want to perform actions based on the value of the status column
There are only 3 values..... Active,Passive,Failed

I have written the following code, but i think it could be much better.

Code:
GET_STATUS  | grep "Active" && STATE="Active" # I Set the variable first so that i dont have to wait for the sleep if it is already in Active
        until [[ "$STATE" = "Active" ]]
                do
                echo "Running GET_STATUS" && sleep 1
                GET_STATUS  | grep "Active" && STATE="Active"
done

Can anyone think of a way i can set the the variable $STATE
Any heap would be greatly appreciated


Thanks

Last edited by $scipt_Kid; 02-23-2013 at 09:59 AM.. Reason: I found a solution
# 2  
Old 02-23-2013
Please use code tags as required by forum rules!

Would be helpful if you posted a) your remote script b) an execution log thereof c) some real output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remote command in PuTTY

I have an issue with all of my AIX servers where a user can execute a remote command (bash in this case) using PuTTY and bypass all of the application security that we setup in the users .profile. How do I secure this without breaking the rest of the users? (8 Replies)
Discussion started by: d_brodie
8 Replies

2. Shell Programming and Scripting

Execute ssh command with additional terminal command to any remote user not working script

Hello i am having an issue with bash script and this is the code now=$(cat hosts1.txt | awk '{print $2;}') while read n ;do ssh root@$now 'useradd test1; echo -e "test1\ntest1" | passwd test1 && echo "test1 ALL=(ALL:ALL) ALL" >> /etc/sudoers' When i execute only part with cat, it... (8 Replies)
Discussion started by: tomislav91
8 Replies

3. Shell Programming and Scripting

Run remote command and send email

I am currently try to write a simple script with the following condition, but so far not having luck as the exit code would always be 0. Run remote command to read a file and grep for test word. if test word is in the file, send email. if not, do nothing also possible to ssh to multiple... (3 Replies)
Discussion started by: jaapar
3 Replies

4. Shell Programming and Scripting

Run awk command on remote host

I have below command to check for error logs from last 24 hours from the file : /var/log/messages/ The command is working fine on the local host. sudo awk -F - -vDT="$(date --date="24 hours ago" "+%b %_d %H:%M:%S")" ' DT < $1' /var/log/messages | egrep -i "error|fail" I want to run the... (8 Replies)
Discussion started by: rahul2662
8 Replies

5. UNIX for Beginners Questions & Answers

How to run command on remote server?

Hi I am trying to write a script which when I run from server A it execute few command on another server say B and show me the output. below is the script but it is not showing me the o/p of B machine but instead showing me A machine o/p every time. #!/bin/bash for i in `cat... (14 Replies)
Discussion started by: scriptor
14 Replies

6. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

7. Shell Programming and Scripting

Need script to run the command in remote server

hi, I need script to perform below task. 1. Read the IP address 2. copy the script from origin server to destination. 3. get root access on destination server 4. run the script on destination server 5. return to the origin server Code: #!/bin/bash echo "Enter Server IP... (5 Replies)
Discussion started by: bapu1981
5 Replies

8. Shell Programming and Scripting

Run command on remote sever from script

I have two redhat linux server. i have created one script which contain some command that run on Local server as well as remote server.I am using this command to connect to remote server ssh user1@192.x.x.x 'command' but when i am running the script in local server it connecting to the server... (1 Reply)
Discussion started by: ranvijaidba
1 Replies

9. Shell Programming and Scripting

ls command in Remote Server

Unix Box: IBM AIX Shell : K-Shell When i logged into a remote server through FTP, and tried to find only required month file by typing `ls -ltr *200805`, the output is "ls remotefile localfile". I coudnt understand this. What is want is get into the remote server and get the count of required... (6 Replies)
Discussion started by: vasuarjula
6 Replies

10. Shell Programming and Scripting

Run remote EOF command

I have the command cat - << _EOF | ed /home/fred/test >/dev/null 2>&1 2 s#u_pwd=*:#u_pwd=${Password}:# 3,$ s/u_succhg#*:/u_succhg#${Date}:/ w q _EOF I works great on the local system but I need to ssh to a remote system and run this command without... (1 Reply)
Discussion started by: insania
1 Replies
Login or Register to Ask a Question