Monday, 24 September 2018

Ansible user with pre-existing keys

This is the main task file:

---
- name: Create a login user with group
  user:
   name: '{{item.name}}'
   groups: '{{item.group}}'
   append: yes
   state: present
  when: item.group is defined
  with_items: '{{sshusers}}'

- name: Create a login user w/o group
  user:
   name: '{{item.name}}'
   state: present
  when: item.group is not defined
  with_items: '{{sshusers}}'

- name: Setting sudo permissions
  lineinfile:
   path: /etc/sudoers
   state: present
   regexp: '^%sudo'
   line: '%sudo ALL=(ALL) NOPASSWD: ALL'
   backrefs: yes

- name: Blocking root password access
  lineinfile:
   path: /etc/ssh/sshd_config
   state: present
   regexp: '^PermitRootLogin'
   line: 'PermitRootLogin without-password'
   backrefs: yes
  notify: reload ssh

- name: Creates directory
  file:
   path: /home/{{item.name}}/.ssh
   state: directory
   owner: '{{item.name}}'
   group: '{{item.name}}'
   mode: 0700
   recurse: yes
  with_items: '{{sshusers}}'

- name: ensure file exists
  copy:
   content: ""
   dest: /home/{{item.name}}/.ssh/authorized_keys
   force: no
   group: '{{item.name}}'
   owner: '{{item.name}}'
   mode: 0600
  with_items: '{{sshusers}}'

- name: copy SSH keys
  authorized_key:
   user: '{{item.name}}'
   key: "{{item.key}}"
   state: present
   exclusive: yes
  when: item.key is defined
  with_items: '{{sshusers}}'


The var file will look like this:

sshusers:
  - name: em_naveed
    group: sudo
    key: ssh-rsa AAAABxxxxxxxxxxxxxxxxx in clear text
  - name: em_hugo
    key: ssh-rsa AAAABxxxxxxxxxxxxxxxx in clear text


The handler file will look like this:

---
- name: reload ssh
  service:
   name: ssh
   state: reloaded

Wednesday, 12 September 2018

AWS ES Access Settings Example

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "1",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "es:*",
      "Resource": "arn:aws:es:ap-southeast-2:389920326251:domain/sharedservices/*",
      "Condition": {
        "IpAddress": {
          "aws:SourceIp": [
            "10.130.100.206/32",
            "54.66.242.28/32"
          ]
        }
      }
    },
    {
      "Sid": "2",
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "AIDAIMZ4NDQ4XT3DZRZQC",
          "arn:aws:iam::389920326251:user/scotts"
        ]
      },
      "Action": "*",
      "Resource": "arn:aws:es:ap-southeast-2:389920326251:domain/sharedservices/*"
    },
    {
      "Sid": "3",
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::389920326251:role/Logstash-SharedServices-APSE2",
          "arn:aws:iam::389920326251:role/Scripts-TicketekProduction-APSE2"
        ]
      },
      "Action": "es:*",
      "Resource": "arn:aws:es:ap-southeast-2:389920326251:domain/sharedservices/*"
    }
  ]
}

Monday, 3 September 2018

Powershell Software installation automation

New-Item -ItemType directory -Path C:\Install\API

$secpasswd = ConvertTo-SecureString "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("xxxxxxxxx", $secpasswd)
#$env:computername current computer name
$collectorName = $env:computername

$response = iwr "https://api.au.sumologic.com/api/v1/collectors" -Credential $mycreds

$content = ConvertFrom-Json $response.Content
$collector = $content.collectors | Where {$_.name -eq $collectorName} | Select -First 1
Write-Host "Collector: $collector"

$response2 = iwr "https://api.au.sumologic.com/api/v1/collectors/$($collector.id)/sources" -Credential $mycreds
$content2 = ConvertFrom-Json $response2.Content

$sourceName='ProdA BasketsAPI'

$source = $content2.sources | Where {$_.name -eq $sourceName} | Select -First 1
Write-Host "Source: $source"

$gist = iwr "https://api.au.sumologic.com/api/v1/collectors/$($collector.id)/sources/$($source.id)?download=true" -Credential $mycreds | Set-Content C:\Docs\Sumo\testingapi.json




New-Item -ItemType directory -Path C:\Install\Sumo
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12'
Invoke-WebRequest 'https://collectors.sumologic.com/rest/download/win64' -outfile 'C:\Install\Sumo\SumoCollector.exe'
C:\Install\Sumo\SumoCollector.exe -console -q "-VskipRegistration=true" "-Vsumo.accessid=xxxx" "-Vsumo.accesskey=xxxxxxxxxxxxxxxxxxxxx" "-Vsources=C:\\Install\\API\\" + testingapi + ".json"
timeout 150
net start sumo-collector
timeout 150
net start sumo-collector

Brute Calling API


New-Item -ItemType directory -Path C:\Install\API
$acctname = 'xxxxxxxxxxxxxxxx'
$password = 'xxxxxxxxxxxxxxxx'
$auth = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${acctname}:${password}"))}
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12'
$gist = Invoke-RestMethod -Method Get -Uri https://api.au.sumologic.com/api/v1/collectors/103320654/sources/122955606?download=true -Headers $auth
$gist | ConvertTo-Json -depth 100 | Set-Content C:\Install\API\testingapi.json

Monday, 13 August 2018

Time specified Windows server logs

$types = (Get-WinEvent -ListLog  *).LogName
$start = Get-Date 10/08/2017
$end = Get-Date 11/08/2017
ForEach ( $type in $types )
{
Get-WinEvent -FilterHashtable @{logname=$type;StartTime=$start;EndTime=$end} | Export-clixml "c:\Backup\$type.xml"
}

Output only in CSV of XML

--------------------------------------------------------------------------------------------------------------------------

Non Time based:

$types = wevtutil el
ForEach ($type in $types)
{
wevtutil epl $type C:\Backup\$type.evtx
}

Output is in evt or evtx

--------------------------------------------------------------------------------------------------------------------------

Monday, 30 July 2018

HTTP APIs calling and injecting Json data

#!/bin/bash
set -xvv

grouplist=$(aws autoscaling describe-auto-scaling-groups --query AutoScalingGroups[].[AutoScalingGroupName] --output text)
while read list; do
  var=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name "$list" --query AutoScalingGroups[].Instances[].[InstanceId] --output text)
  if [ -z "$var" ]; then continue; else
    echo $list
    aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name "$list" --query AutoScalingGroups[].Instances[].[InstanceId] --output text
    echo ""
  fi
done <<<"$grouplist" >asop
asopa=$(perl -00 -lpe 's/\n/,/g;' asop | egrep -i 'Hotshow A')
echo $asopa | tr -s ',' '\n' >final.txt

asc=64
while read ids; do
  let asc=$asc+1
  char=$(awk 'BEGIN{printf "%c", '$asc'}')
  midtemp=$(cat jsonmid)
  count+=$(printf '%s' "$midtemp" | sed 's/i-0123456/'${ids}'/g; s/.*refId.*/"refId":"'$char'"/; /.*refId.*/ s/$/,/')
done < <(tail -n +2 final.txt)
merger=$(echo $count | sed '$ s/.$//')

value1=$(curl -H "Authorization:Bearer eyJrIjoieDZNazBVcFROZ2N1OFlJR1pvZFJiYm8wZzFqWXI3MGEiLCJuIjoiT2ZmaWNlIiwiaWQiOjF9" http://localhost/api/dashboards/id/11/versions?limit=1)
temp=$(echo $value1 | tr -s ',' '\n' | tail -4 | head -1)
jsonbot=$(sed 's/.*version.*/'$temp'/g' jsonbottom)
jsontemptop1=$(sed 's/.*version".*/'$temp'/g; /.*version".*/ s/$/,/' jsontop)
temp1=$(head -1 final.txt)
jsontemptop2=$(printf '%s' "$jsontemptop1" | sed 's/.*title.*/"title":"'"$temp1"'"/; /.*title.*/ s/$/,/')
echo $jsontemptop2$merger$jsonbot >datainject.json

curl -H "Authorization: Bearer eyJrIjoieDZNazBVcFROZ2N1OFlJR1pvZFJiYm8wZzFqWXI3MGEiLCJuIjoiT2ZmaWNlIiwiaWQiOjF9" -H "Accept: application/json" -H "Content-type: application/json" -X POST -d @datainject.json http://localhost/api/dashboards/db

rm -fr datainject* asop*

--------------------------------------------------------------------------------------------------------------------------

jsontop:

{
"meta": {
"type": "db",
"canSave": true,
"canEdit": true,
"canAdmin": true,
"canStar": true,
"slug": "apiboard",
"url": "/d/ZfrvEIKmz/Apiboard",
"expires": "0001-01-01T00:00:00Z",
"created": "2018-07-22T22:02:53Z",
"updated": "2018-07-23T12:53:11Z",
"updatedBy": "admin",
"createdBy": "admin",
"version":37,
"hasAcl": false,
"isFolder": false,
"folderId": 0,
"folderTitle": "General",
"folderUrl": "",
"provisioned": false
},
"dashboard": {
"annotations": {
"list": [{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}]
},
"description": "Data has been loaded via API Inserted",
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"id": 11,
"links": [],
"panels": [{
"gridPos": {
"h": 1,
"w": 24,
"x": 0,
"y": 0
},
"id": 4,
"title":"Hotshow A",
"type": "row"
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "AWS",
"fill": 1,
"gridPos": {
"h": 10,
"w": 24,
"x": 0,
"y": 1
},
"id": 2,
"legend": {
"alignAsTable": true,
"avg": true,
"current": true,
"max": true,
"min": true,
"rightSide": true,
"show": true,
"sort": "current",
"sortDesc": true,
"total": false,
"values": true
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
--------------------------------------------------------------------------------------------------------------------------

jsonmid:

{
"alias": "{{InstanceId}}",
"dimensions": {
"InstanceId": "i-0123456"
},
"highResolution": false,
"metricName": "CPUUtilization",
"namespace": "AWS/EC2",
"period": "",
"refId": "A",
"region": "ap-southeast-2",
"statistics": [
"Average"
]
},
--------------------------------------------------------------------------------------------------------------------------

jsonbottom:

],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "CPU Utilization",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [{
"format": "percent",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}
],
"refresh": "5s",
"schemaVersion": 16,
"style": "dark",
"tags": [
"final"
],
"templating": {
"list": []
},
"time": {
"from": "now-2d",
"to": "now"
},
"timepicker": {
"refresh_intervals": [
"5s",
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d"
],
"time_options": [
"5m",
"15m",
"1h",
"6h",
"12h",
"24h",
"2d",
"7d",
"30d"
]
},
"timezone": "browser",
"uid": "iZr08GFik",
"title": "ApiBoard",
"version":37
}
}
--------------------------------------------------------------------------------------------------------------------------

Thursday, 17 May 2018

WinScp - Directory Syncing between linux and windows

Import-Module WinScp
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Scp
$sessionOptions.HostName = "nash.duckdns.org"
$sessionOptions.UserName = "xyz"
$sessionOptions.Password = "abc"

$sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = $true
$sessionOptions.SshPrivateKeyPath = "C:\Users\NashAIO\Downloads\Nisa.ppk"

$session = New-Object WinSCP.Session

$session.Open($sessionOptions)

$SourcePath = "C:\Source\"
$DestinationPath = "/home/ubuntu/html/"

$session.SynchronizeDirectories( [WinSCP.SynchronizationMode]::Remote, "$SourcePath", "$DestinationPath", $False)

$session.Dispose()

Some Powershell Stuff

Script1

$cred = Get-Credential

$Server = "192.168.109.135"

Invoke-Command -ComputerName $server -ScriptBlock {

Set-ExecutionPolicy Unrestricted -Force
Sleep 5

Install-PackageProvider -Name NuGet -Force

Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServer" -All
Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerManagementTools" -All
Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerRole" -All
#Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools

Import-Module ServerManager -Force
Import-Module WebAdministration -Force

Stop-Website "Default Web Site"

Md $Env:systemdrive\Website1
Md $Env:systemdrive\MyApp
Md $Env:systemdrive\Backup

New-Website -Name "Website1" -Port 80 -IPAddress "*" -HostHeader "" -PhysicalPath "C:\Website1"

New-WebVirtualDirectory -Site "Website1" -Name "MyApp" -PhysicalPath "$Env:systemdrive\MyApp"
ConvertTo-WebApplication -PSPath "IIS:\Sites\Website1\MyApp"

Restart-WebAppPool DefaultAppPool

} -Credential $cred

New-PSDrive -Name J -PSProvider FileSystem -Root \\$Server\C$ -Credential $cred

Copy-Item -Path "J:\Website1" -Destination "C:\ServerCopy" -Recurse -Force
Copy-Item -Path "J:\MyApp" -Destination "C:\ServerCopy" -Recurse -Force

Get-PSDrive J | Remove-PSDrive

--------------------------------------------------------------------------------------------------------------------------

Script2

$cred = Get-Credential

$server = "192.168.109.135"

New-PSDrive -Name J -PSProvider FileSystem -Root \\$server\C$ -Credential $cred

Copy-Item -Path "J:\Website1" -Destination "J:\Backup" -Recurse -Force
Copy-Item -Path "J:\MyApp" -Destination "J:\Backup" -Recurse -Force

do {

$Source=  Read-Host "Input The Full file Path "
$Destination = Read-Host "Input The Full file Path "

get-childitem $Source -Recurse | foreach {
    $SrcFile = $_.FullName
    $SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5

    $DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination

    if (Test-Path $DestFile) {
        $DestHash = Get-FileHash -Path $DestFile -Algorithm MD5
        if ($SrcHash.hash -ne $DestHash.hash) {
            Copy-Item $SrcFile -Destination $DestFile -Force
        }
    }
else {
        Copy-Item $SrcFile -Destination $DestFile -Force
    }
}
$response = read-host "Repeat?"
}
while ($response -eq "Y")

Get-PSDrive J | Remove-PSDrive

Invoke-Command -ComputerName $server -ScriptBlock {
Restart-WebAppPool DefaultAppPool
} -Credential $cred

--------------------------------------------------------------------------------------------------------------------------