Shell Scripting

10K

Solutions

6K

Contributors

The term 'shell' refers to a general class of text-based command interpreters most often associated with the UNIX and Linux operating systems. Popular shells include Bourne, Debian Almquist (dash), Korn (ksh), Bourne Again (bash) and the C shell family (csh). Some view the DOS 'cmd' prompt as a minimal shell of sorts. It is also possible to install Cygwin on Windows and emulate a full Unix environment with complete shell capabilities. Terminal emulators, such as xterm, GNOME Terminal and OS X Terminal, can be used to access shell.

Share tech news, updates, or what's on your mind.

Sign up to Post

Experts,

I have the following code fragment :

Function Test-FileLocation($location, $FileName){
    write-host "Starting File Check for $("$location\$filename")"
        if ($("$location\$filename")){
            write-host "File Exists Proceeding with script"
            $filetest = "True"
        }else {
            write-host "File Does not Exist : FileTest Failed"
            Exit
        }
}

Open in new window


I would like to modify this where I can add Nested Parameter Sets.

For instance in this code I would use something like :

test-filelocation -fullpath "c:\test\somefile.txt"

or

in the same code I could use something like this

test-filelocation -location "c:\test" -filename "somefile.txt"
0
Starting with Angular 5
LVL 12
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Experts,

What is the easiest way to acomplish the following:

I am looking at turning the following working code into a reusable function.

Function invoke-varcheck2(){
        if ($services.count -gt "200"){
            write-host "Check was successful"
        }else {
            write-host "Variable was not created successfully"
        }
    }
    . invoke-varcheck2

Open in new window



Here was my attempt that was less than successful :

Function invoke-varcheck($varname, $count){
    write-host "Attempting to Test Variable $varname"
        if (($+"$(($varname).count)" -gt $count){ 
            write-host "Check was successful"
        }else {
            write-host "Variable was not Created Successfully"
        }

}
invoke-varcheck -varname services -count 200

Open in new window


Where did I go wrong with this?
1
Experts,

Powershell scripting issue :

Function get-spaceusagereport($loc){

set-location $loc
$colItems = Get-ChildItem $loc | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
$colresults = @()
    foreach ($i in $colItems) {
        try {
            write-host "Processing .... $i"
            $subFolderItems = @()
            $subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
            $object = ""|select Fullpath, SpaceUsed, rawspace
            $object.FullPath = $i.FullName
            $object.SpaceUsed = "{0:N2}" -f $($subfolderitems.sum / 1MB) + " MB"
            $object.rawspace = $($subfolderitems.sum / 1MB)
            $colresults += $object
        }
        catch {
            write-host "Could Not Access $i"
        }
    }

    #$object.Fullpath = $loc
    #$rootFolder = Get-ChildItem $($loc) -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
    #$object.spaceused = "{0:N2}" -f $($subfolderitems.sum / 1MB) + " MB"
    #$object.rawspace = $($subfolderitems.sum / 1MB)

    #$colresults += $object

write-host "The Top Space Using Folders are :"
$($colresults|sort-object rawspace -Descending|select -first 5)|select FullPath, SpaceUsed      
}

Open in new window


Most of this code functions, however, I am running into an issue retrieving the rootlevel folder $loc 's size in the $colresults object.

Any chance someone can assist with this :

- Thanks
1
Experts,

What am I missing :

if I run the following , I am able to get the output I am looking for..

 enter-pssession -computername Computername
 $diskinfo = [System.IO.DriveInfo]::GetDrives()|select Name, AvailableFreeSpace, TotalSize
            $diskinfo |add-member -MemberType NoteProperty -Name PercentFree -value $null
            $diskinfo |add-member -MemberType NoteProperty -name ComputerName -value $ENV:Computername

            $diskinfo | ForEach-Object { 
                [float]$_.AvailableFreeSpace = $($_.AvailableFreeSpace/1GB)
                [float]$_.Totalsize = $($_.totalsize/1GB)
                [float]$_.PercentFree = (($($_.AvailableFreeSpace) / $($_.TotalSize)) * 100)

                 }

Open in new window





If I run :


$s = new-pssession -ComputerName Computername -name DiskInfo

$scriptblock = {$diskinfo = [System.IO.DriveInfo]::GetDrives()|select Name, AvailableFreeSpace, TotalSize
            $diskinfo |add-member -MemberType NoteProperty -Name PercentFree -value $null
            $diskinfo |add-member -MemberType NoteProperty -name ComputerName -value $ENV:Computername

            $diskinfo | ForEach-Object { 
                [float]$_.AvailableFreeSpace = $($_.AvailableFreeSpace/1GB)
                [float]$_.Totalsize = $($_.totalsize/1GB)
                [float]$_.PercentFree = (($($_.AvailableFreeSpace) / $($_.TotalSize)) * 100)

                 }
 }

 invoke-command -Session $s -ScriptBlock $scriptblock

 import-pssession -session $s

Open in new window



I get a lightshow..


What I am attempting to do is the following :


The script will be run from Computer1 and collect information for several other computers

The information will then be stored on computer1 and exported to CSV.

Can someone please show me where I went wrong with this : I am sure I am missing something simple

Thanks,
0
Trying to capture this output to a file.  (manual SFTP)

sftp> cd /inbox
sftp> put testfile.txt
Uploading testfile.txt to /inbox/testfile.txt
testfile.txt                                                                                                       100%    5     0.1KB/s   00:00
sftp> bye


Using this command in my script, sftp -b /tmp/sftp_command_file $user@$node >> /tmp/sftp_error_file.   However, it does not write anything to the file.  

From script,
FILELIST=`ls -1 2> /dev/null`  
     for f in $FILELIST
      do
         echo "put $f inbox/$f" >>/tmp/sftp_command_file      
         echo "chmod 777 inbox/$f" >> /tmp/sftp_command_file
         echo "quit" >> /tmp/sftp_command_file
    done

sftp -b /tmp/sftp_command_file $user@$node >> /tmp/sftp_error_file

Thanks in advance for your help.
BJE
0
Hello All,
when running the below python script from the terminal to change an interface MAC, I get the following message: "siocsifhwaddr cannot assign requested address"

any ideas of what could be happening?

---------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/env python

import subprocess

interface = "eth0"
new_mac = "11:22:33:44:55:11"
print("[+] Changing MAC for " + interface + " to " + new_mac)

subprocess.call("ifconfig " + interface + " down", shell=True)
subprocess.call("ifconfig " + interface + " hw ether " + new_mac, shell=True)
subprocess.call("ifconfig " + interface + " up", shell=True)

-----------------------------------------------------------------------------------------------------------------------------------------
0
Using the below code in the bash script, i get the list of the available interface names.
How can i assign each found interface name to different variables, then use them to configure each interface name.


Thank you For your great help and Quick Prompts
(if you know a better way to find the existing interface names. please share ;)  )

ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d'

Open in new window

0
Hi..
I have a file similar to the below example:

/../../PVD
/../../PVD1
/../../PVD2
/../..PVD3

In a bash script How can I 'for' loop from the last item to process an action.
0
Dear Experts,

I hope you can help me with this manipulation of a file which needs to be re-designed so that it can be used in Graphviz.

Input file as below, chunk starts with "SCHEDULE" and ends with "END".
In between chunks there's a blank line.
In each of the chunks there is upper and lower half seperated by a ":".

In the upper part each line that start with "FOLLOWS" should come on 1 line with and preceeding "SCHEDULE", seperated with "->", double quotes are necessary...
                example: "FOLLOWS FTA_BES_ACC#SBP_BE_BEG_PRERQ.@"->"SCHEDULE FTA_BES_ACC#SBP_BE_BOOT_J2P0"

In the lower half each line should be preceeded by the "SCHEDULE" line (the first line of the chunk)
                example: "SCHEDULE FTA_BES_ACC#SBP_BE_BOOT_J2P0"->"FTA_BES_ACC#SBP_BE_BOOT_J2P0"

               
Input file:              
                               
               

                SCHEDULE FTA_BES_ACC#SBP_BE_BOOT_J2P0
                FOLLOWS FTA_BES_ACC#SBP_BE_BEG_PRERQ.@
                :
                FTA_BES_ACC#SBP_BE_BOOT_J2P0
                END

                SCHEDULE FTA_BES_ACC#SBP_BE_CHKV2SBP
                FOLLOWS FTA_BES_ACC#SBP_BE_BOOT_J2P0.@
                :
                FTA_BES_ACC#SBP_BE_CHKV2SBP_SYNC
                FTA_BES_ACC#SBP_BE_CHKV2SBP_SEND
                FTA_BES_ACC#SBP_BE_CHKV2SBP_LIST
                END

                SCHEDULE FTA_BES_ACC#SBP_BE_CHKNXCTRL
                FOLLOWS
0
When we get threat intels info for hashes to be added to Trendmicro
Officescan, the MD5 or SHA1 hash value has to be entered into an
IOC Editor (we use Mandiant's ie
 https://www.fireeye.com/content/dam/fireeye-www/services/freeware/ug-ioc-editor.pdf)
 to generate 2 values indicated by 1st value & 2nd value enclosed in "...'  below.

However, sometimes, the intel that comes in can be more than a hundred, so this makes
it very tedious to manually generate the values & populate into the IOC file for Officescan
to read in.  Is there an automated way / script to generate the 2 values for each hash &
auto-populate (using script etc) or an easier way for Officescan to read in just the hash
values?    Have logged a case with Trendmicro & was advised to do it manually which
does not help at all.

attached the full IOC file.


   Sample IOC file's content:
   ====================
<?xml version="1.0" encoding="us-ascii"?>
<ioc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="2146113a-1513-4be6-b07e-f43969847a6a" last-modified="2018-12-02T02:19:17" xmlns="http://schemas.mandiant.com/2010/ioc">
  <short_description>Default</short_description>
  <authored_date>2017-09-26T02:58:26</authored_date>
  <links />
  <definition>
    <Indicator operator="OR" id="a1c825b0-ae7f-4461-85dd-25a20720acac">          <== 1st value enclosed in "...";  once only for entire IOC file
      <IndicatorItem …
0
Fundamentals of JavaScript
LVL 12
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Hi ..
I booted from an bootable media and trying to chroot the root file system on the local drive to run a bash script (bscript.sh) in /usr/local/bin/ with 3 parameters which are files inside the /usr/local/bin/
i made a bash script as below:
mkdir /mnt/s1
mount /dev/sda1 /mnt/s1
chroot /mnt/s1 ./etc/local/bin/bscript.sh $(cat paramfile1) $(cat paramfile2) $(cat paramfile3)
But I get the error:
cat cannot find file paramfile1
cat cannot find file paramfile2
cat cannot find file paramfile3

/bin/bash: line 1 file not found

Open in new window


The bscript.sh runs without the parameters, since 'cat' cannot find the files that in the same directory as the bscript.sh (of course full path works with cat like:  cat /mnt/s1/....)
Could someone help to correct the above procedures.

Thank You for your Quick Prompts.
1
Hi ...

Trying running a python script that calls a bash script as sudo.  to get all the standard input/output using the below:
sas_cmd = ['sudo', 'apacheart']
proc = subprocess.Popen(sas_cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Open in new window

it is not calling the script at all ....
But, if i do the following:
proc = subprocess.Popen(['sudo','/usr/bin/apacheart'])

Open in new window

It calls the script.
Can someone please help to get the std/stout.
Thanks
0
I am attempting to silently run an uninstall script via a GPO when a normal domain user logs into the device.
My understanding is that anything run at startup uses the system account and therefore the script should work fine but that is not my experience. The files copy over fine and the script runs but not with admin (install) privileges.

Any assistance to resolve this is appreciated

(See attached screenshots)

CMD file being executed:

@echo off
if not exist "C:\Programdata\install_wim_tweak.exe" xcopy "\\maccrayhs\msiapps\uninstall_edge\*.*" "C:\Programdata\"
echo Uninstalling Microsoft Edge...
cd /d "%~dp0"
echo Uninstalling Microsoft Edge...
CLS
C:\Programdata\install_wim_tweak.exe /o /l
C:\Programdata\install_wim_tweak.exe /o /c Microsoft-Windows-Internet-Browser-Package /r
C:\Programdata\install_wim_tweak.exe /h /o /l

Open in new window

Screenshot-from-2018-11-16-09-31-08.png
Screenshot-from-2018-11-16-09-31-35.png
0
Hi...
I have a bash script with 8 positional parameters. each needs to be set from a file. So, I have 8 files with only one line each.
what is the best way to read each file and pass it to the bash script.
Thank you for your prompt help.
1
I'll need a Shell (Bash) script (rather an exact command) that outputs

a)  files' name in the Solaris system, one file per line in the UNIX systems
b) that were modified/created the last 1470 minutes
c) exclude FIFO files, symbolic links, sockets (ie *.sock)
d) names of files of between 1 byte to 20MB in size
e) files in /dev, /devices, /kernel, /cdrom, /platform, /proc, /net
f) files mounted on NFS

I have about 1million files so hoping the command/script of outputting
the file can complete in 30mins, so may need efficient coding.


I need to amend the following script to read (ie for AV to scan) the above output file:

#!/bin/bash
LOGFILE="/var/log/clamav/`hostname`-$(date +'%Y-%m-%d').log";
## suggest to change dirs below to root but exclude databases
DIRTOSCAN="/var /opt /home /etc /tmp /export";

for S in ${DIRTOSCAN}; do
DIRSIZE=$(du -sh "$S" |grep -v "/proc" |grep -v "/dev" |grep -v ...  /2>/dev/null | cut -f1);
## add to grep -v for any other file types to exclude

echo "Starting a daily scan of "$S" directory.
Amount of data to be scanned is "$DIRSIZE".";

clamscan -ri "$S" >> "$LOGFILE";
0
Dear Experts,

I am thinking to use SSIS to do the following procedure.

(1) copy many files to Linux machines. then
(2) execute a Linux program (command) to analysis those files. The program will output the result to the local Linux drive
(3) when (2) is done, then pull the result back to Windows machine

For (1) I am thinking of FTP task (by FTP or SFTP)

how about the task of (2) and (3) ?  Could you please help ... Thanks
0
Linux. I need to set up a script that I can run via cron in the middle of every night to copy / back up specific folders into another directory. I want all folders, files and permissions and attributes copied. The goal is to have a 24 hour old copy that I can restore in case a user screws up their files. Example:

/usr/home/thisuser/web_master would be copied / backed up to /usr/home/mainuser
/usr/home/thisotheruser/web_master would be copied / backed up to /usr/home/mainuser
/usr/home/andanotheruser/web_master would be copied / backed up to /usr/home/mainuser

Each night, the copy / back up would overwrite the one from 24 hours ago.

How do I set this script up, and what type of file extension should the script be?
0
I'll need to monitor several "privilege escalation related" Solaris 10 & RHEL6 files using
ACLs (Access Ctrl Lists) :

a) /etc/group, /etc/sudoers, /etc/cron.daily (or .weekly or any crons owned by root):
    ACL to send to syslog (so that we can pipe to SIEM) when permissions, ownership
    or contents of the above files are changed

b)visudo, sudo, usermod, useradd    command binary files :
   when these are being executed/run, ACL to send to syslog (who & when it's being
   executed)

Appreciate an exact  setacl (or the actual commands/settings in RHEL6 & Solaris 10
x86  samples
0
I am trying to get a specific pid and ONLY that pid, not others that might have that pid embedded in them, i.e.

345
1345
5345

I only want to get the 345.  If they would let me use Perl it would be easy, but I have to use ksh.

Any ideas?

Thanks!
David
0
OWASP: Forgery and Phishing
LVL 12
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Batch scripting

Experts,

I have run across an old dos batch script that utilizes something I have never seen.

The script is used to copy a file, but the reasons behind and the uses for the [ ] is what I am looking for.

copy h:filename.txt i:[Spreadsheet]\folder1\folder2

Open in new window

1
Hi Expert,

I have a SFTP job which is failing while transfer file for source to dest using "put" command.  As put command only works for single file.

Here my requirement is i want to transfer all the files available in "source/dir1/dir2" and want to merge them into single file then wanna SFTP.

Note:The script should also need to check how many files are there in the source directory before merge them!

thanks in advance.
0
Hi Expert,

How Bundle XML files together into a ZIP using shell script.

Your Comments will be really Appreciate.

Thanks,
0
Need to harden a Solaris 10 that is connecting to Internet  from DMZ.

Anyone has a Solaris 10 hardening script that once run will harden for
a) Level 2 Profile
b) "Scored"

The attached which I got from GitHub doesn't seem quite fit to what's needed
& with all the "printf ...", it's more of listing out than actually doing hardening.


From CIS benchmark:

Scoring Information
================
A scoring status indicates whether compliance with the given recommendation impacts the assessed target's benchmark score. The following scoring statuses are used in this benchmark:
Scored  <==
Failure to comply with "Scored" recommendations will decrease the final benchmark score. Compliance with "Scored" recommendations will increase the final benchmark score.
Not Scored
Failure to comply with "Not Scored" recommendations will not decrease the final benchmark score. Compliance with "Not Scored" recommendations will not increase the final benchmark score.



Profile
=====

 Level 1
Items in this profile intend to:
o be practical and prudent;
o provide a clear security benefit; and
o not inhibit the utility of the technology beyond acceptable means.
 Level 2  <==
This profile extends the "Level 1" profile. Items in this profile exhibit one or more of the following characteristics:
o are intended for environments or use cases where security is paramount
o acts as defense in depth measure
o may negatively inhibit the utility or performance of the …
0
This question is regarding modifying the below code which is supposed to take f string as the path and within the sn variable looks for all the files that exist there.

Two things I need to know is

1.) what is the correct syntax for me to loop through and view each file within 'sn'…  I know it has to do with for each but unable to come up with the correct syntax to make it work.
2.) ...and what should 'sn' be declared originally besides variant?

This coding below was found doing a search on youtube as the fastest method to get any files within a folder at this link:
https://www.youtube.com/watch?v=ddA2_SOaq14

Dim sSQL As String
Dim f As String
Dim sn As Variant
    
f = "C:\Temp\File Sorter\Samples"
sn = Split(CreateObject("wscript.shell").exec("cmd /c Dir """ & f & ".pdf" & """ /s /a /b").stdout.readall, vbCrLf)

Open in new window


I'm really looking for some code to try and paste to see if works.  I'm not a student looking for an answer but have other solutions I'm having to figure out and work on.  Don't have time to try this method but if you have time to solve and know it right away I appreciate the direct answer.   Thank you in advance.
0
Using the last SharePoint site in Office 365, and after some changes (see below), all files in SharePoint Sync as marked Read Only (Padlock icon)
All files are Read OnlyIndeed if you try to save the file after making a change, you are warned that this file is Read Only and to save using a new name or in a different location.

When editing a file in Word, all files have the following error message: "REQUIRED PROPERTIES To save to the server, correct the invalid or missing required properties"
Required PropertiesWhen you click on the "Edit Properties" button, Word is asking to add a Title and a File Type. If you fail to enter a File Type, you cannot proceed.
PropertiesSame "error" appears when checking the file online
Properties while online
ALL the files and folder in that Site are affected
There are 54K+ files & ~6,000 folders affected

Background
1. Files were previously uploaded without issues by dropping them in a SharePoint synced folder on a Windows 10 PC using OneDrive one demand
2. There was no issue initially with Read Only or Properties missing
3. We had to restart the above process because it had been stopped in the middle
4. It is only after this second upload that their Read Only & Properties settings were changed.

Google has not helped so far.
* Only one possible root cause (but no fix): Changing the Default View on the SharePoint Site web page that lists all the Documents in that Site has been shown to create …
0

Shell Scripting

10K

Solutions

6K

Contributors

The term 'shell' refers to a general class of text-based command interpreters most often associated with the UNIX and Linux operating systems. Popular shells include Bourne, Debian Almquist (dash), Korn (ksh), Bourne Again (bash) and the C shell family (csh). Some view the DOS 'cmd' prompt as a minimal shell of sorts. It is also possible to install Cygwin on Windows and emulate a full Unix environment with complete shell capabilities. Terminal emulators, such as xterm, GNOME Terminal and OS X Terminal, can be used to access shell.