Simons Roliga Kod

From Datateknik
(Difference between revisions)
Jump to: navigation, search
(Bash)
(Mediawiki)
 
(22 intermediate revisions by one user not shown)
Line 6: Line 6:
 
  </div>
 
  </div>
 
[https://www.mediawiki.org/wiki/Manual:Collapsible_elements Mer info här]
 
[https://www.mediawiki.org/wiki/Manual:Collapsible_elements Mer info här]
 +
<s>Testar</s>
 +
 +
== Powershell ==
 +
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
'''Konverterar powerpoint filer till pdf från prompt eller via skript (spara som .ps1 fil och kör där pptx filerna ligger) [https://gist.github.com/mp4096/1a2279ec7b3dfec659f58e378ddd9aee Källa]<br>
 +
!OBS! Ta bort nedersta raden, den gör bara namnbyte på filerna enligt mitt eget tycke!
 +
<div class="mw-collapsible-content">
 +
# Batch convert all .ppt/.pptx files encountered in folder and all its subfolders
 +
# The produced PDF files are stored in the invocation folder
 +
#
 +
# Adapted from http://stackoverflow.com/questions/16534292/basic-powershell-batch-convert-word-docx-to-pdf
 +
# Thanks to MFT, takabanana, ComFreek
 +
#
 +
# If PowerShell exits with an error, check if unsigned scripts are allowed in your system.
 +
# You can allow them by calling PowerShell as an Administrator and typing
 +
# ```
 +
# Set-ExecutionPolicy Unrestricted
 +
# ```
 +
# Get invocation path
 +
$curr_path = Split-Path -parent $MyInvocation.MyCommand.Path
 +
# Create a PowerPoint object
 +
$ppt_app = New-Object -ComObject PowerPoint.Application
 +
# Get all objects of type .ppt? in $curr_path and its subfolders
 +
Get-ChildItem -Path $curr_path -Recurse -Filter *.ppt? | ForEach-Object {
 +
    Write-Host "Processing" $_.FullName "..."
 +
    # Open it in PowerPoint
 +
    $document = $ppt_app.Presentations.Open($_.FullName)
 +
    # Create a name for the PDF document; they are stored in the invocation folder!
 +
    # If you want them to be created locally in the folders containing the source PowerPoint file, replace $curr_path with $_.DirectoryName
 +
    $pdf_filename = "$($curr_path)\$($_.BaseName).pdf"
 +
    # Save as PDF -- 17 is the literal value of `wdFormatPDF`
 +
    $opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF
 +
    $document.SaveAs($pdf_filename, $opt)
 +
    # Close PowerPoint file
 +
    $document.Close()
 +
}
 +
# Exit and release the PowerPoint object
 +
$ppt_app.Quit()
 +
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ppt_app)
 +
Get-ChildItem -Filter "*Chp*.pdf" -Recurse | Rename-Item -NewName {$_.name -replace 'Chp','Kapitel_'}
 +
</div></div>
  
 
== Bash ==
 
== Bash ==
Line 12: Line 54:
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
 
  #This is a bash script to check ssh services on hosts
 
  #This is a bash script to check ssh services on hosts
 +
#You need a host file with each IP-address on a separate row
 +
#The source file for the text is used as an argument, ie ./ssh-check.sh ip.txt
 +
#Edit the nc row to alter the port you are testing for, in this case its ssh=22
 +
#Results are output to screen and a text file in the same directory
 
  #!/bin/bash
 
  #!/bin/bash
 
  echo "=================================="
 
  echo "=================================="
Line 32: Line 78:
 
  # This is a bash script to generate qrcodes by reading a text string
 
  # This is a bash script to generate qrcodes by reading a text string
 
  # from a file and adding a human readable text part below the qr code
 
  # from a file and adding a human readable text part below the qr code
 +
# The source file for the text is used as an argument, ie ./qrcodes.sh hosts.txt
 
  #!/bin/bash
 
  #!/bin/bash
 
  echo "=================================="
 
  echo "=================================="
Line 47: Line 94:
 
</div></div>
 
</div></div>
  
== PowerCLI för VMware (körs i Powershell! ==
+
== PowerCLI för VMware (körs i Powershell! - spara som powershell skript...) ==
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
 
'''För att skapa en mängd VLANS utan att sitta manuellt i Vcentret (obs PowerCLI är farligt!)'''
 
'''För att skapa en mängd VLANS utan att sitta manuellt i Vcentret (obs PowerCLI är farligt!)'''
Line 110: Line 157:
 
  }
 
  }
 
  }
 
  }
 +
</div></div>
 +
 +
== Övervakning via Linux ==
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
'''För att skicka notifieringar från linux cli till Android telefon'''
 +
<div class="mw-collapsible-content">
 +
https://medium.com/better-programming/how-to-send-push-notifications-to-your-phone-from-any-script-6b70e34748f6
 +
 +
* Visit http://ifttt.com and sign up for an account.
 +
* Click on your account icon (top right), and then click on Create.
 +
* Click on the If+ icon and type “Webhooks” in the search box.
 +
* Select Webhooks → Receive a Web Request and type “notify” for your event name.
 +
* Next, click on the +That icon and type “Notifications” in the search box.
 +
* Select “Send a notification from the IFTTT app.”
 +
* Type the following text into the Message box:
 +
Notification: <nowiki>{{Value1}}</nowiki>
 +
* Click 'Create action' and then give your action a name, e.g. “Push Notification.” Then click on Finish.
 +
 +
'''Testing the Notification'''
 +
* Install the ifttt app on your android device
 +
* Visit https://ifttt.com, and then:
 +
** Click on your account icon (top right), and then click on My Services.
 +
** Select Webhooks from the list, and then click on Documentation.
 +
* A screen should be displayed showing your API URL as well as some fields to pass values via a POST request.
 +
** Type “notify” (the name of our event) in the event box.
 +
** Type “test” in the Value 1 box.
 +
 +
The resulting command at the bottom should look something like this:
 +
 +
curl -X POST -H "Content-Type: application/json" -d '{"value1":"Meddelande här!"}' https://maker.ifttt.com/trigger/notify/with/key/DIN_API_NYCKEL_HÄR
 +
 +
* Klicka på test it för att skicka ett test till telefonen (eller kör kommandot via din linux/curl)
 +
 +
'''Kort bash function för att skicka ett custom meddelande'''
 +
cli-2-phone()
 +
{
 +
    printf -v message '{"value1":"%s"}' "$*"
 +
    [[ $1 ]] && curl -X POST -H "Content-Type: application/json" -d "$message" https://maker.ifttt.com/trigger/notify/with/key/DIN_API_NYCKEL_HÄR > /dev/null 2>&1 ; return
 +
    [[ $0 ]] && echo "You need to specify a message!"; return
 +
}
 +
Det är nu hyfsat enkelt att anropa detta via ett python program t.ex.
 +
 
</div></div>
 
</div></div>

Latest revision as of 13:04, 17 May 2022

Contents

[edit] Mediawiki

Expanderbar text, typ folds

<div class="toccolours mw-collapsible mw-collapsed">
This text is not collapsible; but the next is collapsible and hidden by default:
<div class="mw-collapsible-content">Text to be expanded/hidden</div>
</div>

Mer info här Testar

[edit] Powershell

Konverterar powerpoint filer till pdf från prompt eller via skript (spara som .ps1 fil och kör där pptx filerna ligger) Källa
!OBS! Ta bort nedersta raden, den gör bara namnbyte på filerna enligt mitt eget tycke!

# Batch convert all .ppt/.pptx files encountered in folder and all its subfolders
# The produced PDF files are stored in the invocation folder
#
# Adapted from http://stackoverflow.com/questions/16534292/basic-powershell-batch-convert-word-docx-to-pdf
# Thanks to MFT, takabanana, ComFreek
#
# If PowerShell exits with an error, check if unsigned scripts are allowed in your system.
# You can allow them by calling PowerShell as an Administrator and typing
# ```
# Set-ExecutionPolicy Unrestricted
# ```
# Get invocation path
$curr_path = Split-Path -parent $MyInvocation.MyCommand.Path
# Create a PowerPoint object
$ppt_app = New-Object -ComObject PowerPoint.Application
# Get all objects of type .ppt? in $curr_path and its subfolders
Get-ChildItem -Path $curr_path -Recurse -Filter *.ppt? | ForEach-Object {
    Write-Host "Processing" $_.FullName "..."
    # Open it in PowerPoint
    $document = $ppt_app.Presentations.Open($_.FullName)
    # Create a name for the PDF document; they are stored in the invocation folder!
    # If you want them to be created locally in the folders containing the source PowerPoint file, replace $curr_path with $_.DirectoryName
    $pdf_filename = "$($curr_path)\$($_.BaseName).pdf"
    # Save as PDF -- 17 is the literal value of `wdFormatPDF`
    $opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF
    $document.SaveAs($pdf_filename, $opt)
    # Close PowerPoint file
    $document.Close()
}
# Exit and release the PowerPoint object
$ppt_app.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ppt_app)
Get-ChildItem -Filter "*Chp*.pdf" -Recurse | Rename-Item -NewName {$_.name -replace 'Chp','Kapitel_'}

[edit] Bash

Testa om en viss port är öppen på en lista hostar

#This is a bash script to check ssh services on hosts
#You need a host file with each IP-address on a separate row
#The source file for the text is used as an argument, ie ./ssh-check.sh ip.txt
#Edit the nc row to alter the port you are testing for, in this case its ssh=22
#Results are output to screen and a text file in the same directory
#!/bin/bash
echo "=================================="
rm results.txt

while read p; do
   nc -znv -w3 $p 22
   if [ $? -ne 0 ]
   then
     echo "No connection" | tee -a results.txt
   else
     echo "OK" | tee -a results.txt
   fi
done < $1

Genererar QR koder från en lista

# This is a bash script to generate qrcodes by reading a text string
# from a file and adding a human readable text part below the qr code
# The source file for the text is used as an argument, ie ./qrcodes.sh hosts.txt
#!/bin/bash
echo "=================================="

while read p; do
  if echo "$p" | grep '*\|\/'; then
    echo "Illegal entry in hosts.txt file"
    break
  else
    qrencode -s 5 -i $p -o $p-QR.png
    convert $p-QR.png -pointsize 24 label:"$p" -gravity center -append $p.png
      rm $p-QR.png
  fi
done < $1

[edit] PowerCLI för VMware (körs i Powershell! - spara som powershell skript...)

För att skapa en mängd VLANS utan att sitta manuellt i Vcentret (obs PowerCLI är farligt!)

// 1. behövs för att köra vm-ware modulen i windows då den är "tredjepart"
Set-ExecutionPolicy remotesigned 

// 2. -Scope begränsar installation till inloggad användare
install-module -Name VMware.PowerCLI -Scope CurrentUser

// 3. Kommer att fråga om dina credentials, använd samma som gui inloggningen 
Connect-VIServer -server vcenter-b.cnap.hv.se

// 4. länk till object som är h6 hosten (till exempel)
$myVMHost = Get-VMHost -name esxi-h6.cnap.hv.se

!! VA RIKTIGT RIKTIGT SÄKER PÅ ATT DET ÄR HÄR DU VILL PILLA !!

// 5. länk till vswitch som skall pillas på
$vswitch = Get-VirtualSwitch -vmhost $myVMHost -name vSwitch1

// 6. Skapar VLAN med namn "VLAN20XX" och vlan id 20XX
for ($i=2004; $i -le 2038; $i++) {New-VirtualPortGroup -VirtualSwitch $vswitch -name "VLAN$i" -VLanId $i}

// 7. tar bort portgroups "VLAN2037-VLAN2038" på vswitchen som definieras i steg >>5<<
// OBS! -confirm:$false stänger av en "are you sure, dummy" fråga. Är du inte säker, ta bort detta ur raden! OBS!
for ($i=2037; $i -le 2038; $i++) {Get-VirtualPortGroup -VirtualSwitch $vswitch -name "VLAN$i" | Remove-VirtualPortGroup -confirm:$false}

// Bryt kopplingen till vcentret
Disconnect-VIServer

Taget från virtuallymikebrown.com


Mass-radera VMs som matchar en sträng | Obs: anslut till rätt host också först (steg 1-4) ovan

$remove_vms = Read-Host -Prompt 'Input the server match string to remove from disk'
Write-Host "The entered string was: '$remove_vms'"
$select_confirm = Read-Host -Prompt 'Confirm your selection string (y/n)'

if($select_confirm -eq 'y') {
	 $VMs = Get-VM $remove_vms
	 foreach($VM in $VMs) {
	 	 Write-Host $VM
	 	 }
	 Write-Host "The listed VMs above will be permanently deleted from disk!"
	 Write-Host "Are you absolutely sure you want to do this?"
	 $nuclearoption = Read-Host -Prompt 'Type in "DELETE" to confirm this operation!'
	 if($nuclearoption -eq 'DELETE') {
	 	 Write-Host "Deleting..."
	 	 foreach($active in $VMs) {
	 	 	 if($active.Powerstate -eq "PoweredOn") {
	 			 Stop-VM -VM $active -Confirm:$false -RunAsync | Out-Null
	 			 Start-Sleep -Seconds 7 
	 			 }
	 		 }
	 	 foreach($delete in $VMs) {
	 			 Remove-VM -VM $delete -DeleteFromDisk -Confirm:$false -RunAsync | Out-Null 
	 			 }
	 	 }
	 }

[edit] Övervakning via Linux

För att skicka notifieringar från linux cli till Android telefon

https://medium.com/better-programming/how-to-send-push-notifications-to-your-phone-from-any-script-6b70e34748f6

  • Visit http://ifttt.com and sign up for an account.
  • Click on your account icon (top right), and then click on Create.
  • Click on the If+ icon and type “Webhooks” in the search box.
  • Select Webhooks → Receive a Web Request and type “notify” for your event name.
  • Next, click on the +That icon and type “Notifications” in the search box.
  • Select “Send a notification from the IFTTT app.”
  • Type the following text into the Message box:
Notification: {{Value1}}
  • Click 'Create action' and then give your action a name, e.g. “Push Notification.” Then click on Finish.

Testing the Notification

  • Install the ifttt app on your android device
  • Visit https://ifttt.com, and then:
    • Click on your account icon (top right), and then click on My Services.
    • Select Webhooks from the list, and then click on Documentation.
  • A screen should be displayed showing your API URL as well as some fields to pass values via a POST request.
    • Type “notify” (the name of our event) in the event box.
    • Type “test” in the Value 1 box.

The resulting command at the bottom should look something like this:

curl -X POST -H "Content-Type: application/json" -d '{"value1":"Meddelande här!"}' https://maker.ifttt.com/trigger/notify/with/key/DIN_API_NYCKEL_HÄR
  • Klicka på test it för att skicka ett test till telefonen (eller kör kommandot via din linux/curl)

Kort bash function för att skicka ett custom meddelande

cli-2-phone()
{
    printf -v message '{"value1":"%s"}' "$*"
     $1  && curl -X POST -H "Content-Type: application/json" -d "$message" https://maker.ifttt.com/trigger/notify/with/key/DIN_API_NYCKEL_HÄR > /dev/null 2>&1 ; return
     $0  && echo "You need to specify a message!"; return
}

Det är nu hyfsat enkelt att anropa detta via ett python program t.ex.

Personal tools
Namespaces

Variants
Actions
Navigation
Tools