Skip to main content

Posts

Showing posts with the label Script

Bash slice string in Linux Shell

Strings are quite basic in any scripting or programming languages.If a journey to a thousand miles starts with a single step, in the programming world the journey starts with a string called “Hello World” and beyond “Hello World” pseudocode and algorithm will keep the journey going. Strings in scripting or programming are just group of characters.So, if string is a group of characters then it can be slice by character or a sub-string can be taken from the string.To slice a string in Bash, a syntax which is part of the Bash library can be used.Syntax is: ${string_to_be_sliced:slice_start_position:slice_end_position}Example string: slicethestring=“Hello Algorithm World”The string is stored in a variable: slicethestringTo get the sub-string “Algorithm World”.Code will be:echo ${slicethestring:6:23} #start the slice at position 6 till position 23Another way to slice the string from a specified start position till the end; is to tell Bash the start position and just omit the end position.O…

Read or get the list of contents on S3 bucket

S3 or Simple Storage Service bucket is quite handy to store files or any data on AWS cloud.As with any storage, online or offline organizing the data is quite an issue.When there are a lot of files of data on the storage; finding the data that you will need will be difficult. Especially, if the data or bucket is not organized properly. And even if the data has been organized by folders with proper time stamp but no specific data catalog to classify which one is which one it is quite a challenge to find or get the data on time.S3 is ideal for backup, since backup is often accessed only when it is needed. So, familiarization of the backup folder structure is necessary. In order to find or get easily the data, listing the contents of the S3 bucket is a good option when unable to find the files or data needed.Once the list is ready, opening the list via editor such as notepad and searching or finding thru the editor will provide an option to find things quickly and easily.
How to list the …

PowerShell switch case call function

Code below collect user input and uses switch case statement to check if the input matches, and if it matches a function is called that will execute commands.Here’s the code:#get or read from user input$computer_name=read-host ("Enter Computer Name:")switch ($computer_name ){#if the input is computer_1 then function func_comuputer1 is called computer_1{func_computer1}computer_2{func_computer2}}#function called if computer_1 is the inputfunctionfunc_computer1 {write-host"You entered Computer_1"#or replaced with other function like reboot / shutdown /or other commands#Restart-Computer -ComputerName computer_1}
#function called if computer_2 is the inputfunctionfunc_computer2 {write-host"You entered Computer_2"#or replaced with other function like reboot / shutdown /or other commands#Ex: Enter-Pssession computer_2}

Code below will keep asking for input and will only exit if a specific string matches to exit the script. Here’s the code:do {$computer_name

Linux find accessed and modified files

Finding accessed and modified files might be necessary at times to check or for audit purposes.
If files kept in a folder or directory has been accessed or modified but should not be the case then something dubious is going on. 
In Linux finding accessed and modified files can be done in a one liner command.
find /home -type f -amin -60 || -mmin -60 -print
Above command will find or show any files accessed within the last 60 minutes with the option "-amin" and it will show also the files modified within the last 60 minutes with the option "-mmin".
A shell script can be created and further processing can be done when files are detected.
The time can be adjusted if there's a need, but a more robust solution to check any accessed or modified files should be a file system watcher, but above command is quite helpful to check any activity that should not be occurring.

Cheers..till next time!================================Free Android Apps:
Click  links below to find out mor…

PowerShell launch an application and close it automatically

Code below will run an application and PowerShell will also close it after a few seconds.$process_id=Invoke-CimMethodWin32_Process-MethodName"Create"-Arguments @{CommandLine ='cmd /c "C:\dev\remind.bat"'} |Select-Objectprocessid|ft-HideTableHeaders|Out-StringStart-Sleep-Seconds2.5stop-process$process_idSample contents of remind.bat:
@echo off
echo "Shutdown the server. Do it now."
Notepad
==================Notepad is on the batch file so the command window will stay on the top of any running applications.Sample output of the above code:
Code below will run calculator and close after a few seconds specified on the code:Invoke-CimMethodWin32_Process-MethodName"Create"-Arguments @{CommandLine ='calc.exe'} |Select-Objectprocessid|ft-HideTableHeaders|Out-StringStart-Sleep-Seconds2.5$get_process_id=Get-Process-Namecalcu*|<

Create a new text file with content in Linux

Touch command in Linux will create an empty file in Linux command line. If no other parameters is specified whether a line will be inserted to the newly created file.
Cat command is for reading files via command line but it can also be used to create a file in Linux terminal window command line.
Cat command below will create a file called “test_comment.txt” and with the content or the line of “Hello World of Touch and Create File” on the file created.
Here’s the shell command:
cat  <( echo “Hello World of Touch and Create File”)  >  test_comment.txt
Note that there is no space between “<(“, if there is a space then the command will not work as expected.
Touch command to create a new file and also insert a new line or insert a string on the newly created file.
Here’s the shell command:
touch touch_file_comment.txt; echo “Hello World of Touch, insert this line.” >  touch_file_comment.txt

Basically, the command executes two commands separated by “;” semi-colon. The first part is to cr…

PowerShell restart remote server with confirmation

Restart a remote PC or server and ask user to confirm reboot before proceeding.

Here’s the code:
$server_q="D-Box-007"
$response_confirmation=[Microsoft.VisualBasic.Interaction]::MsgBox("Restart this server: "+$server_q,'YesNo',"Confirm to reboot?")

switch ( $response_confirmation )
{
Yes{ Restart-Computer-ComputerName$server_q} No{ Write-Host"Restart Cancelled"} }
Code above will prompt the user with “Yes” or “No”, yes will send the restart or reboot command to the remote server or computer, and no will cancel or exit the execution.

PowerShell code above also demonstrate how to use a switch case statement and use the response to perform an action, in regar

PowerShell check access or folder permissions

In an Active Directory domain, one of the common issues is folder permissions. Of course, permission must be restricted as much as possible. In order, confidential data or things that only a group of people will know, is not made available to everyone.

Employee salaries on a network share made available to everyone, will cause some employees to be disheartened. Such data, the access or folder permission should be checked properly.
One way to check a folder permission is to assume or login as a certain user that is not supposed to have access to such data and checked whether the data can be viewed or not.
Aside from restricting permissions for confidential data; data supposed to be accessed by everyone but some user’s complaint that they are unable to access then this will also cause some problem and may end up not being productive for the users.
One way to check is to go to the user’s workstation and verify whether the user is unable to access. This kind of issue is not hard to solve…