A small guide to: "CMSMS remote control with bash scripting"

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
User avatar
blast2007
Power Poster
Power Poster
Posts: 508
Joined: Wed Aug 01, 2007 5:36 pm

A small guide to: "CMSMS remote control with bash scripting"

Post by blast2007 »

Thanks to precious hints received from calguy1000, here you can find a smarty way to control your CMSMS site without opening a browser, manually logging-in and (best thing) with scheduled automatic bash scripts (GNU/Linux)

For security reason I suggest you to logon on CMSMS only via SSL. This prevents password sniffing.

Another safety measure is to protect bash script from unwanted reading. So set appropriate file permissions for the scripts.

Copy these scripts in a file (ie cmsms-rc.sh) and make it executable. (chmod +x cmsms-rc.sh)

The common part is login script that must be present in all control scripts.

Code: Select all

#!/bin/bash
username="yourusername"
password="yourpasswordhere"
sitename="http://www.yourdomainhere.com"
_cookies="./cookie"
keyname="sp_"
admin_dir="admin"

# ************** Do the login ******************************
_postdata="username=${username}&password=${password}&loginsubmit=Submit"

wget -o log -O outfile.1 --save-cookies ${_cookies} --keep-session-cookies --post-data=$_postdata ${sitename}/$admin_dir/login.php
_x=`grep -c 'password incorrect' outfile.1`

if [ $_x -gt 0 ]; then
  echo "FATAL: Authentication error";
  exit 1
fi

# Get the session key
_line=`cat $_cookies | grep ${keyname}`
if [ ${line:-notset} != notset ]; then
  _sessionkey=`cat $_cookies | grep ${keyname} | tr '\t' , | cut -d, -f7`
else
  _sessionkey=`cat log | grep ^Location | cut -d= -f2 | cut -d" " -f1`
fi
Then you can operate on what part of CMSMS you need.

Clear cache

Code: Select all

# Clear cache
_postdata="clearcache=clear&submit=Submit"
wget -o log -O outfile.1 --load-cookies ${_cookies} --post-data=$_postdata ${sitename}/$admin_dir/siteprefs.php?${keyname}=${_sessionkey}
Set site offline: (Enable Site Down)

Code: Select all

# Set site offline
_postdata="active_tab=sitedown&editsiteprefs=true&enablesitedownmessage=0&enablesitedownmessage=1&sitedownmessage=%3Cp%3ESite+is+currently+down+for+maintenance.%3C%2Fp%3E&sitedownexcludes=&submit=Submit"
wget -o log -O outfile.1 --load-cookies ${_cookies} --post-data=$_postdata ${sitename}/$admin_dir/siteprefs.php?${keyname}=${_sessionkey}
Set site online: (Disable Site Down)

Code: Select all

# Set site online
_postdata="active_tab=sitedown&editsiteprefs=true&enablesitedownmessage=0&sitedownmessage=%3Cp%3ESite+is+currently+down+for+maintenance.%3C%2Fp%3E&sitedownexcludes=&submit=Submit"
wget -o log -O outfile.1 --load-cookies ${_cookies} --post-data=$_postdata ${sitename}/$admin_dir/siteprefs.php?${keyname}=${_sessionkey}
And now the best action you can do...automatic backup of your site with Mysql Dump and FileBackup

Mysqldump

Code: Select all

# ************** Mysqldump ***************************
# Create Mysqldump download to your local PC
admin_file="moduleinterface.php"
date=`date '+%Y-%m-%d_%H-%M-%S'`
out_file="backup-$date.sql"

# Launch Mysqldump
querystring="mact=MysqlDump%2Cm1_%2Cdumpdatabase%2C0&m1_filename=$out_file&m1_backup=Backup+Database&m1_path=%2Fusr%2Fbin&m1_options=--opt+--verbose+--compatible%3Dmysql40+--default-character-set%3Dlatin1"
wget -a log -O outfile1.htm --load-cookies ${_cookies} ${sitename}/$admin_dir/$admin_file?${keyname}=${_sessionkey}\&$querystring
# Retrieve file
wget -a log -O $out_file --load-cookies ${_cookies} ${sitename}/$admin_dir/backups/$out_file &
wait $!
# Delete backup from server
querystring="mact=MysqlDump%2Cm1_%2Cdeletedataset%2C0&m1_filename=%2Fvar%2Fwww%2Fadmin%2Fbackups%2F$out_file&m1_delete=Delete"
wget -a log -O outfile1.htm --load-cookies ${_cookies} ${sitename}/$admin_dir/$admin_file?${keyname}=${_sessionkey}\&$querystring
Filebackup:

Code: Select all

# ************** Filebackup ***************************
# Do a backup with Filebackup
admin_file="moduleinterface.php"
date=`date '+%Y-%m-%d_%H-%M-%S'`
out_file="filebackup-$date.tgz"

# Launch file backup
#querystring="mact=FileBackup%2Cm1_%2Cdumpdatabase%2C0&m1_filename=$out_file&m1_backup=Backup+Files&m1_path=%2Fbin&m1_bck_options=-cpzvf"
#wget -a log -O outfile1.htm --load-cookies ${_cookies} ${sitename}/$admin_dir/$admin_file?${keyname}=${_sessionkey}\&$querystring
# Retrieve file
#wget -a log -O $out_file --load-cookies ${_cookies} ${sitename}/$admin_dir/backups/$out_file &
#wait $!
# Delete backup from server
#querystring="mact=FileBackup%2Cm1_%2Cdeletedataset%2C0&m1_filename=%2Fvar%2Fwww%2Fadmin%2Fbackups%2F$out_file&m1_delete=Delete"
wget -a log -O outfile1.htm --load-cookies ${_cookies} ${sitename}/$admin_dir/$admin_file?${keyname}=${_sessionkey}\&$querystring
These scripts can be invoked from cron, so you can schedule your job.

Almost any actions triggered in CMSMS can be replicated via scripting. If you install "Live HTTP Headers" plugin for Firefox you can read all url/get/post value when you trigger an action in you browser.

These scripts are only raw examples that can be expandend according your own needs. Moreover I haven't put any control to response value, so I don't check if there are errors or other response code from CMSMS. If you want you can improve scripts and post on the forum.

Hope this helps
regards
blast
manmower
Forum Members
Forum Members
Posts: 15
Joined: Wed May 13, 2009 12:41 pm

Re: A small guide to: "CMSMS remote control with bash scripting"

Post by manmower »

Do you know if there are also ways to do this in a windows environment? Or is there a way to create a login script in php? I also need scheduled tasks... but I without a login script I'm not able to access the functions I need.
User avatar
blast2007
Power Poster
Power Poster
Posts: 508
Joined: Wed Aug 01, 2007 5:36 pm

Re: A small guide to: "CMSMS remote control with bash scripting"

Post by blast2007 »

I haven't enough infos on bash and windows system. You can try with win-bash on sf.

regards
blast
Pierre M.

Re: A small guide to: "CMSMS remote control with bash scripting"

Post by Pierre M. »

Hello,

@windozers : you can run cygwin or busybox, can't you ?

@all : wget automation is a nice way to write non regression tests for CMSms :
-build a strong library of functional tests (add a user, modify permissions, edit page, attach CSS to template, and so),
-upgrade CMSms or change a setting,
-rerun all the tests to see result of changes.

Suggestion : name the admin bot user "botadmin" to trace its activity in the CMSms log.

Notice : a functional test must check a result against an expected result (like the "do login" above).

I'm dreaming of the Community submitting a full non regression testing suite to CMSms into the QA board.

Pierre M.
JeremyBASS

Re: A small guide to: "CMSMS remote control with bash scripting"

Post by JeremyBASS »

I think I'd be really cool to have an install bash script.. I have one that grabs the latest and untars it... simple... but it'd be cool to have one run the install script, install "your" fav mods and set up common users and setting like pretty urls.... I know this can be done.. be cool... just a thought...  ;D
Pierre M.

Re: A small guide to: "CMSMS remote control with bash scripting"

Post by Pierre M. »

Hello,
JeremyBASS wrote: I think I'd be really cool to have an install bash script.. I have one that grabs the latest and untars it... simple... but it'd be cool to have one run the install script, install "your" fav mods and set up common users and setting like pretty urls.... I know this can be done.. be cool... just a thought...  ;D
I disagree : I think this is off topic. Installing a given version with fav mods and commun users and settings is only a restore away (db restore and untar). No buggy scripting needed.

Remote control with wget/curl scripting is a way to automate things *users* would do : true use case clicstreams.

Pierre M.
User avatar
chilsta
Forum Members
Forum Members
Posts: 52
Joined: Thu Oct 20, 2005 8:22 pm
Location: Hove, UK

Re: A small guide to: "CMSMS remote control with bash scripting"

Post by chilsta »

Hi- This is looking really interesting,

I'm not too comfortable with bash scripting, so can you please confirm: do I copy & paste the relevant script(s) that I want to run into the same file, underneath the initial authenticating script, or are they somehow run separately? If so, how?

If the first option, is the idea that you set each file up as you need it to run, then either manually execute it or use cron as required?

-C-
User avatar
blast2007
Power Poster
Power Poster
Posts: 508
Joined: Wed Aug 01, 2007 5:36 pm

Re: A small guide to: "CMSMS remote control with bash scripting"

Post by blast2007 »

chilsta wrote: I'm not too comfortable with bash scripting, so can you please confirm: do I copy & paste the relevant script(s) that I want to run into the same file, underneath the initial authenticating script, or are they somehow run separately? If so, how?

If the first option, is the idea that you set each file up as you need it to run, then either manually execute it or use cron as required?

-C-
Basically you can use login.sh script (the first part) then all other scripts afterwards (e.g. clearcache.sh, siteoffline.sh,...).
The first script get a session cookie when sucessfully logged in. The other scripts use the cookie as authentication and make actions.

All can be added to crontab for repetitive tasks.

regards
blast
Post Reply

Return to “Tips and Tricks”