Simons Roliga Kod

From Datateknik
(Difference between revisions)
Jump to: navigation, search
(Övervakning via Linux)
Line 6: Line 6:
[ Mer info här]
[ Mer info här]
== Powershell ==
== Powershell ==

Revision as of 14:03, 17 May 2022



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>

Mer info här ~~Testar~~


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
# 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
# Exit and release the PowerPoint object
Get-ChildItem -Filter "*Chp*.pdf" -Recurse | Rename-Item -NewName {$ -replace 'Chp','Kapitel_'}


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 ./ 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
echo "=================================="
rm results.txt

while read p; do
   nc -znv -w3 $p 22
   if [ $? -ne 0 ]
     echo "No connection" | tee -a results.txt
     echo "OK" | tee -a results.txt
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 ./ hosts.txt
echo "=================================="

while read p; do
  if echo "$p" | grep '*\|\/'; then
    echo "Illegal entry in hosts.txt file"
    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
done < $1

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

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


// 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

Taget från

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 

Övervakning via Linux

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

  • Visit 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, 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!"}'Ä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

    printf -v message '{"value1":"%s"}' "$*"
     $1  && curl -X POST -H "Content-Type: application/json" -d "$message"Ä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