First script in a long time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting First script in a long time
# 1  
Old 12-11-2017
First script in a long time

I was wondering if I could get some feedback on my script to grab time from our MDM... I blocked out all of the important stuff. I really appreciate any guidance, since I am long out of practice.

Code:
#!/bin/bash
serial=$1
# get last seen value of ipad

lastseen=$(curl -s -X "GET" "https://myairwatchmdm.com/api/mdm/devices/serialnumber/$serial" -H "Authorization: Basic bartsimpson=" -H "aw-tenant-code: marge" | xmllint --format -|grep LastSeen | awk -F "T|<" '{print $3}' | awk -F: '{printf "%1.0f",($1*60)+$2+($3%60)}')

# get UTC time
utc=$(UTC date -j %s)

#get time difference
timediff="$((utc-lastseen))"

# Ipad exit successful 

if [[ "$timediff" -eq 0 ]] || [[ "$timediff" -lt 3600 ]];
echo "$timediff"
then exit 0; 

# Ipad warning threshhold 
    
elif [[ "$timediff" -gt 3600 && "$timediff" -lt 3701 ]];
echo  "$lastseen tablet is reaching warning threshold"
then exit 1; 

# Ipad not seen for more then sixty minutes

elif [[ "$timediff" -gt 3600 ]]; 
echo  "$lastseen tablet has not been seen in more then an hour"
then exit 2; 

# Ipad serial number invalid

elif [[ "$lastseen" = '^[a-ZA-Z]*$' ]] || [[ -z "$lastseen" ]];
echo "Serial Number Invaild"
then exit 3; 
fi

# 2  
Old 12-11-2017
This is not doing what you require:
Code:
elif [[ "$lastseen" = '^[a-ZA-Z]*$' ]] || [[ -z "$lastseen" ]];

See Parsing null or empty output: post #4

This validity check also looks out of place here as part of an elif statement, because in the if statements you are comparing timediff, not lastseen.

If anything it should probably occur as a separate if statement right after
Code:
 lastseen=$( ... )

But lastseen is a timestamp, not a serial number, so the error message seems not as exact as it might be?
--
Code:
if [[ "$timediff" -eq 0 ]] || [[ "$timediff" -lt 3600 ]];
echo "$timediff"
then exit 0;

Should be:
Code:
if [[ "$timediff" -eq 0 ]] || [[ "$timediff" -lt 3600 ]]
then 
  echo "$timediff"
  exit 0

or
Code:
if [[ $timediff -eq 0 || $timediff -lt 3600 ]]
then 
  echo "$timediff"
  exit 0

or
Code:
if (( timediff == 0 || timediff < 3600 ))
then

--
It is good to have proper indentation

--
Your script should test the validity of $1

Last edited by Scrutinizer; 12-12-2017 at 03:55 PM..
# 3  
Old 12-12-2017
On top of what Scrutinizer already said,
- I'd be surprised if UTC date worked.
- the grep | awk | awk organ could be squeezed into a smaller one.
- "$timediff" -lt 3600 includes == 0.
- in your decision tree, you're missing the $timediff == 3600 case.
# 4  
Old 12-12-2017
$lastseen variable

what I was trying to do with the $last seen was to account for any output other then a number or null output. I guess my logic is incorrect.

Is there a way I can combine the whole curl command and part the last seen time and look for any error or null output?


Code:
elif [[ "$lastseen" = '^[a-ZA-Z]*$' ]] || [[ -z "$lastseen" ]];
echo "Serial Number Invaild"
then exit 3; 
fi

Thanks for the feedback
# 5  
Old 12-12-2017
Post a (few?) unprocessed line(s) from that output.

Last edited by RudiC; 12-12-2017 at 12:42 PM..
# 6  
Old 12-12-2017
XML output

The xml looks like this (I blocked out the sensitive bits) I am using grep to get the last seen value.

Code:
LocationGroupName><UserName>myipad</UserName><UserEmailAddress>myipad@myorg.com</UserEmailAddress><Ownership>C</Ownership><PlatformId title="Apple">2</PlatformId><Platform>Apple</Platform><ModelId title="iPad Mini with Retina (16 GB Space Gray)">2</ModelId><Model>iPad Mini with Retina (16 GB Space Gray)</Model><OperatingSystem>11.1.0</OperatingSystem><PhoneNumber /><LastSeen>2017-12-12T16:49:33.863</LastSeen><EnrollmentStatus>Enrolled</EnrollmentStatus><ComplianceStatus>Compliant</ComplianceStatus><CompromisedStatus>false</CompromisedStatus><LastEnrolledOn>2017-11-07T20:21:19.36</LastEnrolledOn><LastComplianceCheckOn>2017-12-12T16:21:29.97</LastComplianceCheckOn><LastCompromisedCheckOn>0001-01-01T00:00:00</LastCompromisedCheckOn><IsSupervised>true</IsSupervised><IsRemoteManagementEnabled>False</IsRemoteManagementEnabled><DataEncryptionYN>N</DataEncryptionYN><AcLineStatus>0</AcLineStatus><VirtualMemory>0</VirtualMemory><OEMInfo>ME276LL</OEMInfo><DeviceCapacity>12.052879333496094</DeviceCapacity><AvailableDeviceCapacity>10.491180419921875</AvailableDeviceCapacity><LastSystemSampleTime>2017-12-12T16:49:35</LastSystemSampleTime><IsDeviceDNDEnabled>false</IsDeviceDNDEnabled><IsDeviceLocatorEnabled>fal

# 7  
Old 12-12-2017
Not sure what you need the NULL test for. Try (provided the date command on your system (the version of either you dodn't mention, btw) allows for it):
Code:
lastseen=$(curl . . .  | awk 'match ($0, "<LastSeen>[^<]*") {print substr($0, RSTART+10, RLENGTH-10)}')
if [[ "$lastseen" =~ [0-9]{4}-[0-9]{2}-[0-9]{2}T([0-9]{2}:){2}[0-9]{2}. ]]
  then  timediff=$(( $(date +%s) - $(date +%s -d$lastseen)))
  .
  .
  .
  fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Killing the process if running for long time in script

I am running a script which will read the data from fail line by line and call the Java program by providing the arguments from the each line. The Java code is working fast for few records and for some records its getting hanged not providing response for morethan one hour. Currently am... (4 Replies)
Discussion started by: dineshaila
4 Replies

2. Shell Programming and Scripting

Expect script idles for a long time

the following code works sometimes. other times, it behaves mysteriously. when the script sshs to a box, it is suppose to automatically begin running the command it is told to run. but in this case, after this script logs into a host, it just sits there at the prompt and does not run the... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Kill long running script, if it crosses the threshold time

Hi, I need a script to kill the process if it running for long time. Inputs for the scripts: 1.test.sh (will be running fron cron scheduler) 2.1 hr (ie threshold_time - if the test.sh is running for more than 1 hr test.sh has to kill) Thanks, Divya (1 Reply)
Discussion started by: Divya Nochiyil
1 Replies

4. Shell Programming and Scripting

How long ago since time - Using perl

echo "1337124526" | perl -pe 's/(\d+)/easttime($1)/e' the above gives a date and time. how can i subtract the date and time given by this command, from the current present date? can this be a one liner or as close to a one-liner as possible? (1 Reply)
Discussion started by: SkySmart
1 Replies

5. UNIX for Dummies Questions & Answers

Job is taking long time

Hi , We have 20 jobs are scheduled. In that one of our job is taking long time ,it's not completing. If we are not terminating it's running infinity time actually the job completion time is 5 minutes. The job is deleting some records from the table and two insert statements and one select... (7 Replies)
Discussion started by: ajaykumarkona
7 Replies

6. Shell Programming and Scripting

help - exec time too long

Dear everyone... thanks to this forum I am able to do everyday more and more complex scripts...but now I come up with problem with optimisation.. problem 1 - optimise: here is my code: while read number do nawk -F "|" -v... (8 Replies)
Discussion started by: abdulaziz
8 Replies

7. Shell Programming and Scripting

<AIX>Problem in purge script, taking very very long time to complete 18.30hrs

Hi, I have here a script which is used to purge older files/directories based on defined purge period. The script consists of 45 find commands, where each command will need to traverse through more than a million directories. Therefore a single find command executes around 22-25 mins... (7 Replies)
Discussion started by: sravicha
7 Replies

8. Programming

Debug env for long time use

Hi, I'm pritty new to C, but a recent bug in a program i've been using has forced me to debug it. But I am unable to find a debugger that can act as a layer between the OS and the program to see whats going on.. The problem is that this piece of software makes a connection through localhost... (2 Replies)
Discussion started by: nephilimbe
2 Replies

9. AIX

rcp gets hung for long time

Every evening I run a script in AIX production box, which executes below command: rcp prod_bkup.tar prodapp@IP:/data/appl/prod This will rcp a backup of around 11 GB from production to another machine (runs every evening so overwrites previous one). Just to keep the backup safe. Since 2-3 days,... (0 Replies)
Discussion started by: panchpan
0 Replies

10. Shell Programming and Scripting

shell script takes long time to complete

Hi all, I wrote this shell script to validate filed numbers for input file. But it take forever to complete validation on a file. The average speed is like 9mins/MB. Can anyone tell me how to improve the performance of a shell script? Thanks (12 Replies)
Discussion started by: ozzman
12 Replies
Login or Register to Ask a Question