{"id":14980394,"url":"https://github.com/lifailon/dotnet-systemd-api","last_synced_at":"2025-08-08T13:12:48.807Z","repository":{"id":200362192,"uuid":"705338380","full_name":"Lifailon/dotNET-Systemd-API","owner":"Lifailon","description":"REST API server and set of endpoints for managing Linux services based on .NET HttpListener.","archived":false,"fork":false,"pushed_at":"2023-11-13T11:29:00.000Z","size":8612,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"rsa","last_synced_at":"2025-04-13T03:13:59.127Z","etag":null,"topics":["api","api-server","bash","dotnet","endpoint","endpoints","linux","net","netcore","powershell","powershell-core","powershell-script","pwsh","rest","rest-api","systemctl","systemd"],"latest_commit_sha":null,"homepage":"","language":"PowerShell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lifailon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-15T18:31:12.000Z","updated_at":"2025-03-06T08:18:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"d0855883-db0c-418c-8b36-cdae69ca7bdf","html_url":"https://github.com/Lifailon/dotNET-Systemd-API","commit_stats":{"total_commits":10,"total_committers":1,"mean_commits":10.0,"dds":0.0,"last_synced_commit":"4329653a1355cd7d1bb421e84aac73f5a1afa231"},"previous_names":["lifailon/systemctl-api"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lifailon%2FdotNET-Systemd-API","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lifailon%2FdotNET-Systemd-API/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lifailon%2FdotNET-Systemd-API/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lifailon%2FdotNET-Systemd-API/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lifailon","download_url":"https://codeload.github.com/Lifailon/dotNET-Systemd-API/tar.gz/refs/heads/rsa","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248657920,"owners_count":21140846,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-server","bash","dotnet","endpoint","endpoints","linux","net","netcore","powershell","powershell-core","powershell-script","pwsh","rest","rest-api","systemctl","systemd"],"created_at":"2024-09-24T14:01:42.356Z","updated_at":"2025-04-13T03:14:22.106Z","avatar_url":"https://github.com/Lifailon.png","language":"PowerShell","readme":"# dotNET-Systemd-API\r\n\r\nSet of endpoints for managing Linux services via **systemctl (systemd)**. Cross-platform REST API server **is based .NET HttpListener and PowerShell** with base authorization and error handling.\r\n\r\nThe goal is to demonstrate the ability of **PowerShell to be operating system independent** and used concurrently with the Bash language 🐧. It was also prompted by the impossibility to create a full-fledged server for REST API using standard means of Bash language and netcat.\r\n\r\n![Image alt](https://github.com/Lifailon/dotNET-Systemd-API/blob/rsa/Example.gif)\r\n\r\n## 🚀 Launch\r\n\r\n**Dependencies**:\r\n\r\nOnly **[PowerShell Core](https://github.com/PowerShell/PowerShell)**\r\n\r\nDownload latest version the distribution from the official repository on GitHub and install it on your system, here is an example for Ubuntu:\r\n\r\n```Bash\r\ndeb_latest=$(curl https://api.github.com/repos/PowerShell/PowerShell/releases/latest | grep -Eom 1 \"https://.+.deb\")\r\ncurl -LO $deb_latest\r\ndeb_name=$(echo $deb_latest | awk -F / '{print $NF}')\r\ndpkg -i $deb_name\r\n```\r\n\r\nSet variables:\r\n```Bash\r\n$ip   = \"192.168.3.104\"\r\n$port = \"8080\"\r\n$path = \"/var/log/systemctl-api.log\"\r\n$cred = \"cmVzdDphcGk=\"\r\n```\r\n\r\nOpen the specified network port in your firewall.\r\n\r\n**Uncomplicated Firewall**:\r\n\r\n```\r\nufw allow in 8080\r\nufw reload\r\n```\r\n\r\n**iptables**:\r\n\r\n```\r\niptables -A INPUT -p tcp --dport 8080 -j ACCEPT\r\niptables-save\r\n```\r\n\r\n**FirewallD**:\r\n\r\n```\r\nfirewall-cmd --permanent --add-port=8080/tcp\r\nfirewall-cmd --reload\r\n```\r\n\r\n\r\n**Start the server**: \\\r\n`powershell systemctl-api.ps1`\r\n\r\nWarning: **use root privileges** to start the server if you need to manage services via POST requests.\r\n\r\n## 🔒 Authorization\r\n\r\nBasic authentication scheme is used on the basis of **Base64**.\r\n\r\nFor authorization it is necessary to pass the login and password in Base64 format to the server in the **variable $cred**. To get the login and password in Base64 format, use the construct:\r\n\r\n```PowerShell\r\n$user = \"rest\"\r\n$pass = \"api\"\r\n[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(\"${user}:${pass}\"))\r\n```\r\n\r\n**For client connectivity, use one of the designs**:\r\n\r\n```PowerShell\r\n$user = \"rest\"\r\n$pass = \"api\"\r\n$SecureString = ConvertTo-SecureString $pass -AsPlainText -Force\r\n$Credential = New-Object System.Management.Automation.PSCredential($user, $SecureString)\r\n#$Credential = Get-Credential\r\nInvoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/service/cron\r\n```\r\n\r\n2nd option, using header:\r\n\r\n```PowerShell\r\n$user = \"rest\"\r\n$pass = \"api\"\r\n$EncodingCred = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(\"${user}:${pass}\"))\r\n$Headers = @{\"Authorization\" = \"Basic ${EncodingCred}\"}\r\nInvoke-RestMethod -Headers $Headers -Uri http://192.168.3.104:8080/api/service/cron\r\n```\r\n\r\nThe server will match the credentials received in the header request from the client.\r\n\r\n## 🎉 GET Request\r\n\r\nTo obtain specific service status:\r\n\r\n```PowerShell\r\nPS C:\\Users\\Lifailon\u003e Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/service/cron | Format-List                                       \r\n\r\nLoaded : loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)\r\nActive : active (running) since Sun 2023-10-15 20:57:43 MSK; 1h 23min ago\r\nTasks  : 1 (limit: 2220)\r\nMemory : 2.2M\r\n```\r\n\r\nTo get a list of all registered unit services:\r\n\r\n`Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/service`\r\n\r\nTo get the list of all registered unit services with their operation status (works slower):\r\n\r\n`Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/service-status`\r\n\r\n## 💎 POST Request\r\n\r\n**Stop** the service:\r\n\r\n`Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/service/cron -Method Post -Headers @{\"Status\" = \"Stop\"}`\r\n\r\n**Start** the service:\r\n\r\n`Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/service/cron -Method Post -Headers @{\"Status\" = \"Start\"}`\r\n\r\n**Restart** the service:\r\n\r\n`Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/service/cron -Method Post -Headers @{\"Status\" = \"Restart\"}`\r\n\r\n## 🎊 Other Endpoints\r\n\r\nGet a list of all processes with a detailed description of all characteristics in **html format**:\r\n\r\n`Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/process`\r\n\r\nGet a list of all processes with a detailed description of all characteristics in **json format**:\r\n\r\n`Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/process`\r\n\r\nGet current uptime and average load:\r\n\r\n```PowerShell\r\nPS C:\\Users\\Lifailon\u003e Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/uptime | Format-List\r\n\r\nUptime : 22:26:54 up 1 day\r\nUsers  : 2\r\navg    : 0.35 0.21 0.19\r\n```\r\n\r\nOutput the state of physical disks (lsblk is used):\r\n\r\n```PowerShell\r\nPS C:\\Users\\Lifailon\u003e $(Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication \r\n-Uri http://192.168.3.104:8080/api/disk).blockdevices.children\r\n\r\nname       : sda1\r\nmaj:min    : 8:1\r\nrm         : False\r\nsize       : 1M\r\nro         : False\r\ntype       : part\r\nmountpoint : \r\n\r\nname       : sda2\r\nmaj:min    : 8:2\r\nrm         : False\r\nsize       : 1G\r\nro         : False\r\ntype       : part\r\nmountpoint : /boot\r\n\r\nname       : sda3\r\nmaj:min    : 8:3\r\nrm         : False\r\nsize       : 49G\r\nro         : False\r\ntype       : part\r\nmountpoint : \r\nchildren   : {@{name=ubuntu--vg-ubuntu--lv; maj:min=253:0; rm=False; size=49G; ro=False; type=lvm \r\n             ; mountpoint=/}}\r\n```\r\n\r\n## ⚠️ Error Handling\r\n\r\nIf the service name is incorrect or the service does not exist, you will receive a message.\r\n\r\n**400. Bad Request**:\r\n\r\n```PowerShell\r\nPS C:\\Users\\Lifailon\u003e Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/service/cronn\r\nInvoke-RestMethod: Bad Request (service cronn could not be found)\r\n```\r\n\r\nIn case your login or password is incorrect, you will receive the following message.\r\n\r\n**401. Unauthorized**:\r\n\r\n```PowerShell\r\nInvoke-RestMethod: Unauthorized (login or password is invalid)\r\n```\r\n\r\n**404. Not Found endpoint**:\r\n\r\n```PowerShell\r\nPS C:\\Users\\Lifailon\u003e Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api\r\nInvoke-RestMethod: Not Found endpoint\r\n```\r\n\r\n**405. Method Not Allowed**:\r\n\r\n```PowerShell\r\nPS C:\\Users\\Lifailon\u003e Invoke-RestMethod -Credential $Credential -AllowUnencryptedAuthentication -Uri http://192.168.3.104:8080/api/service/cron -Method Put -Headers @{\"Status\" = \"Restart\"} | fl   \r\nInvoke-RestMethod: Method Not Allowed\r\n```\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flifailon%2Fdotnet-systemd-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flifailon%2Fdotnet-systemd-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flifailon%2Fdotnet-systemd-api/lists"}