Lftp a handy utility for all kinds of ftp, sftp, and other file transfer use-cases from the command-line on a *nix system. Give it a shot if there's ever a need..
Insights on Java, Big Data, Search, Cloud, Algorithms, Data Science, Machine Learning...
Showing posts with label Shell. Show all posts
Showing posts with label Shell. Show all posts
Thursday, June 18, 2015
Wednesday, January 15, 2014
Mocks for Unit testing Shell Scripts
1. Look at shunit for a sense of what kind of unit testing can be performed for Shell scripts.
2. For mocking up specific steps/ programs in the script, make use of alias.
Within a shell script testScript.sh this would be something as follows:
shopt -s expand_aliases
2. For mocking up specific steps/ programs in the script, make use of alias.
Expand alias in the script:
shopt -s expand_aliases
Mock up inbuilt program value via alias:
alias find='abc.txt efg.txt; #'
shopt -s expand_aliases
Mock up inbuilt program value via alias:
alias find='abc.txt efg.txt; #'
Within a shell script testScript.sh this would be something as follows:
shopt -s expand_aliases
alias find='abc.txt efg.txt; #'
./runFindFileScript.sh
assertEquals "2 files found" 2 $countFilesFound
Friday, May 3, 2013
Php Script To Display Process, Vmstat, Disk Usage, Syslog Of A Linux Server Via A Browser
A Php script that executes some standard shell programs for monitoring resource utilization & processes on a given Linux box. The script directs the output to a web-browser.
Apache web-server should be installed on the server. To run copy the script to the DocumentRoot (/var/www/html). Appropriate execute rights (-rw-x) need to be given to the apache user (which runs this script, but is not the owner) to execute this Php file & to be able to access /var/log/syslog.
Save this file as: showHealth.php in the /var/www/html folder:
Apache web-server should be installed on the server. To run copy the script to the DocumentRoot (/var/www/html). Appropriate execute rights (-rw-x) need to be given to the apache user (which runs this script, but is not the owner) to execute this Php file & to be able to access /var/log/syslog.
Save this file as: showHealth.php in the /var/www/html folder:
<html>
<body>
<script type="text/javascript">
<!--
function toggle(id){
DIV1=document.getElementById(id);
if (DIV1.style.display=="none") DIV1.style.display="block";
else DIV1.style.display="none";
}
//-->
</script>
<div id="outermost">
<div id="mcdetails">
Showing stats from:
<!-- ifconfig eth0 -->
<?php
echo shell_exec("ip addr show | grep 'inet' | tail -1");
?>
<br/><br/>Server Date is:
<?php
echo shell_exec("date");
?>
<br/><br/>
</div>
<a href="#" onclick="toggle('vmstat');">-------Vmstat----------</a><br/><br/>
<div id="vmstat" style="display:none">
<?php
$output = shell_exec("vmstat| sed 's/$/<br\/>/g'| sed 's/ /./g'");
echo $output;
?>
</div>
<br/><a href="#" onclick="toggle('df');">-------df -m----------</a><br/><br/>
<div id="df" style="display:none">
<?php
$output = shell_exec("df -m | sed 's/$/<br\/>/g'| sed 's/ /./g'");
echo $output;
?>
</div>
<br/><a href="#" onclick="toggle('ps');">-------ps aux----------</a><br/><br/>
<div id="ps" style="display:none">
<?php
$output = shell_exec("ps aux | sed 's/$/<br\/><br\/>/g'| sed 's/ /./g'");
//$output = shell_exec('ls -l');
echo $output;
?>
</div>
<br/><a href="#" onclick="toggle('syslogdiv');">-------syslog----------</a><br/><br/>
<!-- sudo chmod 615 /var/log/syslog -->
<div id="syslogdiv" style="display:none">
<?php
$output = shell_exec("tail -200 /var/log/syslog | sed 's/$/<br\/>/g' ");
//$output = shell_exec('ls -l');
echo $output;
?>
</div>
</div>
</body>
</html>
<body>
<script type="text/javascript">
<!--
function toggle(id){
DIV1=document.getElementById(id);
if (DIV1.style.display=="none") DIV1.style.display="block";
else DIV1.style.display="none";
}
//-->
</script>
<div id="outermost">
<div id="mcdetails">
Showing stats from:
<!-- ifconfig eth0 -->
<?php
echo shell_exec("ip addr show | grep 'inet' | tail -1");
?>
<br/><br/>Server Date is:
<?php
echo shell_exec("date");
?>
<br/><br/>
</div>
<a href="#" onclick="toggle('vmstat');">-------Vmstat----------</a><br/><br/>
<div id="vmstat" style="display:none">
<?php
$output = shell_exec("vmstat| sed 's/$/<br\/>/g'| sed 's/ /./g'");
echo $output;
?>
</div>
<br/><a href="#" onclick="toggle('df');">-------df -m----------</a><br/><br/>
<div id="df" style="display:none">
<?php
$output = shell_exec("df -m | sed 's/$/<br\/>/g'| sed 's/ /./g'");
echo $output;
?>
</div>
<br/><a href="#" onclick="toggle('ps');">-------ps aux----------</a><br/><br/>
<div id="ps" style="display:none">
<?php
$output = shell_exec("ps aux | sed 's/$/<br\/><br\/>/g'| sed 's/ /./g'");
//$output = shell_exec('ls -l');
echo $output;
?>
</div>
<br/><a href="#" onclick="toggle('syslogdiv');">-------syslog----------</a><br/><br/>
<!-- sudo chmod 615 /var/log/syslog -->
<div id="syslogdiv" style="display:none">
<?php
$output = shell_exec("tail -200 /var/log/syslog | sed 's/$/<br\/>/g' ");
//$output = shell_exec('ls -l');
echo $output;
?>
</div>
</div>
</body>
</html>
Labels:
Apache,
Health Check,
Linux,
mod_php,
Monitoring,
Php,
Scripting,
Shell,
Unix
Saturday, April 20, 2013
Linux/Unix Shell Function For Date Addition and Subtraction
Here is a small shell script to do date addition and subtraction. This works on the bash shell with GNU Date.
#!/bin/bash
function getPreviousDateTime(){
d1=`date -d "$1" +'%Y-%m-%d %H:%M:%S'`;
secs1=`date -d"$d1" +%s`;
secs2=`expr $secs1 - $2`;
# change to %H:%M:%S if you want the nos. left padded
result=`date -d@"$secs2" +'%Y-%m-%d %-H:%-M:%-S'`;
# return formatted string
echo $result;
}
function getNextDateTime(){
d1=`date -d "$1" +'%Y-%m-%d %H:%M:%S'`;
secs1=`date -d"$d1" +%s`;
secs2=`expr $secs1 + $2`;
result=`date -d@"$secs2" +'%Y-%m-%d %H:%M:%S'`;
# return formatted string
echo $result;
}
# Tests
echo `getPreviousDateTime "2012-04-29 18:00:31" 30`
echo `getPreviousDateTime "2012-04-29 00:00:00" 60`
echo `getPreviousDateTime "2012-04-29 02:00:00" 60`
echo `getNextDateTime "2012-04-29 19:00:00" 60`;
echo `getNextDateTime "2012-04-30 23:59:00" 60`;
echo `getNextDateTime "2012-02-28 23:59:00" 60`;
echo `getNextDateTime "2012-02-28 2:59:00" 60`;
#!/bin/bash
function getPreviousDateTime(){
d1=`date -d "$1" +'%Y-%m-%d %H:%M:%S'`;
secs1=`date -d"$d1" +%s`;
secs2=`expr $secs1 - $2`;
# change to %H:%M:%S if you want the nos. left padded
result=`date -d@"$secs2" +'%Y-%m-%d %-H:%-M:%-S'`;
# return formatted string
echo $result;
}
function getNextDateTime(){
d1=`date -d "$1" +'%Y-%m-%d %H:%M:%S'`;
secs1=`date -d"$d1" +%s`;
secs2=`expr $secs1 + $2`;
result=`date -d@"$secs2" +'%Y-%m-%d %H:%M:%S'`;
# return formatted string
echo $result;
}
# Tests
echo `getPreviousDateTime "2012-04-29 18:00:31" 30`
echo `getPreviousDateTime "2012-04-29 00:00:00" 60`
echo `getPreviousDateTime "2012-04-29 02:00:00" 60`
echo `getNextDateTime "2012-04-29 19:00:00" 60`;
echo `getNextDateTime "2012-04-30 23:59:00" 60`;
echo `getNextDateTime "2012-02-28 23:59:00" 60`;
echo `getNextDateTime "2012-02-28 2:59:00" 60`;
Subscribe to:
Posts (Atom)