#!/bin/bash## curl parameter explained:# -s, silent mode# -D, write the response http header to file or stdout by using "-D -"# -X, HTTP GET,POST,PUT,DELETE method# --data, http POST data contents# -c, save the cookies to file path# -b, use the cookes from file path# -o, output response to file path## Credentials for the NUS SVMS website loginusername='your_user_name'password='your_password'# This string will be used to locate the content/lines we want to extract from the html responsesearch_string="Search Ticket Result"# Get cookies from website by send POST API request to the login web pagecurl -s -D - -X POST --data "userid=$username&passwd=$password&submit=Login" -c /tmp/cookies.txt \ https://svms.nus.edu.sg/login -o /dev/null# Use obtained cookies to access the ticket information API web page by GET method # Save the response html content to a local file for parsing datacurl -s -D - -X GET --data 'status=unresolved' -b /tmp/cookies.txt \ https://svms.nus.edu.sg/ticket/search-ticket?status=unresolved -o /tmp/output# The start Line number of the html response file, can ignore the previous linesstart_line=$(grep -n "$search_string" /tmp/output | cut -d ":" -f 1)# Parse the ticket number count from the html response fileticket_no=$(grep -n "$search_string" /tmp/output | cut -d ":" -f 2 | cut -d "-" -f 2 | cut -d " " -f 2)# First skipped lines from start line to the ticket id location linefirst_skip=29# Subsequent skipped lines from ticket id to ticket idskip=31# First ticket id line numberline=$((start_line+first_skip))#echo $line# Loop through all the html content and based on each line number of the ticket id# Send the API POST request to update the ticket information# If the update is successful, display "successful" information on the consolefor (( i=0;i<$ticket_no;i++))do ticket_id=$(awk -v var="$line" 'NR==var {print $0}' /tmp/output | cut -d ">" -f 2 | cut -d "<" -f 1) curl -s -D - -X POST --data "scanticket_pkey=$ticket_id&status=justified&remarks=No+Impact&submit=Update" \ -b /tmp/cookies.txt --referer https://svms.nus.edu.sg/ticket/edit-ticket-form?scanticket_pkey=$ticket_id \ https://svms.nus.edu.sg/ticket/update-ticket | if grep -q "has been updated successfully"; \ then echo "updated Ticket ID: ${ticket_id} successfully"; fi line=$((line+skip))done
Không có nhận xét nào:
Đăng nhận xét