Skip to content

Instantly share code, notes, and snippets.

@Epictek
Created March 8, 2019 10:09
Show Gist options
  • Save Epictek/b6205cd8d655bd337928e60667e2acaa to your computer and use it in GitHub Desktop.
Save Epictek/b6205cd8d655bd337928e60667e2acaa to your computer and use it in GitHub Desktop.
Get latest debian updates in C# .NET Core
using CRplc.Ics.Pi.Data;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public static class Apt {
public static async Task Update() {
using(var process = new Process() {
StartInfo = new ProcessStartInfo {
FileName = "/bin/bash",
Arguments = "-c \"sudo unbuffer apt update\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
}) {
process.Start();
StreamReader stdout = process.StandardOutput;
string line;
while ((line = stdout.ReadLine()) != null) {
Console.WriteLine(line);
}
string result = await process.StandardOutput.ReadToEndAsync();
process.WaitForExit();
}
}
public static async Task < String > GetUpgrades() {
await Update();
using(var process = new Process() {
StartInfo = new ProcessStartInfo {
FileName = "/bin/bash",
Arguments = "-c \"sudo apt list --upgradable\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
}) {
process.Start();
StreamReader stdout = process.StandardOutput;
string line;
List < Package > packageList = new List < Package > ();
while ((line = stdout.ReadLine()) != null) {
var pattern = @ "(\S+)\/(\S+) (\S+) ([a-z]+) \[upgradable from: (\S+)]";
Match match = Regex.Match(line, pattern);
if (match.Success) {
var package = new Package {
Name = match.Groups[1].Value,
CurrentVersion = match.Groups[2].Value,
Arch = match.Groups[3].Value,
UpgradeVersion = match.Groups[4].Value
};
packageList.Add(package);
}
}
process.WaitForExit();
Console.WriteLine(JsonConvert.SerializeObject(packageList));
return JsonConvert.SerializeObject(packageList);
}
}
public static async Task Upgrade() {
using(var process = new Process() {
StartInfo = new ProcessStartInfo {
FileName = "/bin/bash",
Arguments = "-c \"sudo unbuffer apt upgrade -y\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
}) {
process.Start();
StreamReader stdout = process.StandardOutput;
string line;
while ((line = stdout.ReadLine()) != null) {
Console.WriteLine(line);
}
string result = await process.StandardOutput.ReadToEndAsync();
process.WaitForExit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment