Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchen committed Dec 28, 2024
1 parent d7a3d3f commit 8cc4e3f
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 2 deletions.
15 changes: 13 additions & 2 deletions docs/document/Articles/docs/Setup nix-on-droid.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@
## Init
- nix-on-droid may ask for url for certain file, if the url is not accessible on your phone, download it and transfer to your phone. And replace the default url as `file:///sdcard/...`
> remember to allow file permision for nix-on-droid.
- type `yes` when nix prompt for downloads for first init.
- add and update channels:
```sh
nix-channel --add https://github.com/nix-community/home-manager/archive/release-24.05.tar.gz home-manager && nix-channel --update
nix-channel --add https://nixos.org/channels/nixos-unstable nixpkgs && nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager && nix-channel --update
```
> [!TIP]
> If you use the wrapper function mentioned above, would be like this:
>```ps1
>adbin -Enter 'nix-channel --add https://github.com/nix-community/home-manager/archive/release-24.05.tar.gz home-manager'
>```
- Update nix: default nix is out-dated, might not work with your current config like home-manager or any other.
```sh
nix profile install nixpkgs#nix --priority 4
```
## Connect to nix-on-droid
Expand Down Expand Up @@ -63,10 +68,16 @@ ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key -N "" # key is generated in pwd
mkdir -p ~/.ssh/ && touch ~/.ssh/authorized_keys && echo <pub> >> ~/.ssh/authorized_keys
```

> [!TIP]
> Use this instead if you prefer pwsh, replace the pubkey name if needed.
>```ps1
>adbin -Enter ('mkdir -p ~/.ssh/ && touch ~/.ssh/authorized_keys && echo ''{0}'' >> ~/.ssh/authorized_keys' -f (gc ~/.ssh/id_ed25519.pub))
>```
- start ssh daemon by `sshd`
```sh
$(which sshd) -p <port> -h <host_key> -d
$(which sshd) -p 8080 -h ./ssh_host_rsa_key -d
```
`-d` is essential to know whether your port is been taken or not. See details in `man sshd`.
Expand Down
28 changes: 28 additions & 0 deletions docs/document/PowerShell/docs/Language/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,34 @@ Use `*` to repeat a string.
'abc' * 2 # abcabc
```

## Grep

`Select-String` is a builtin cmdlet that does the similar thing like `grep` in gnu coreutils.
It can select one or more lines that matches the specified regex.

- Grep single line from pipeline

```ps1
# -Pattern is positional
help sls | sls -Pattern 'Position\??\s*\d'
```

- Grep multiple lines from above and below

```ps1
# include 3 lines above and 5 lines below the macthed line
help sls | sls 'Position\??\s*\d+' -Context 3,5
```

- Grep from pipeline passed by property(`-Path`)

```console
PS ~> gci /nix/store/ -dir | sls 'roslyn'

/nix/store/m4npcw66155bbi8i7ipp0bbp54wdwwlg-roslyn-ls-4.13.0-3.24577.4
/nix/store/np6bigb7d3lvpinw0mrdvj38xi67q6sa-roslyn-ls-4.12.0-2.24422.6
```

## String Evaluation in Interpolation

### Collection
Expand Down
63 changes: 63 additions & 0 deletions docs/document/PowerShell/docs/Understanding Remoting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Understanding Remoting

## The Protocol

PowerShell uses a protocol that you might have never heard of, the *Web Services for Management(WSMan)*, which is one of the builtin *PSProvider* on Windows.
WSMan is Windows specific feature, so as PowerShell came into the cross-platform world, Microsoft decided to support SSH which is more commonly used over industries.
Microsoft made effort to bring SSH to Windows world, they're maintaining [a fork of SSH in github](https://github.com/PowerShell/openssh-portable)

> [!NOTE]
> `openssh` is builtin since certain version of Windows 10.
## Connect to Remote

No matter which protocol you use with PowerShell, it has a unified cmdlet to connect to one.
Of course this requires PowerShell available on both sides.

- Using SSH

```ps1
# login as <user> on <ip>
Enter-PSSession -HostName <host> -UserName <user> -Port <port>
# or use a short syntax
Enter-PSSession -HostName <usr>@<host>
```

- Using WSMan

```ps1
# -ComputerName used instead for connection by WSMan
Enter-PSSession -ComputerName <host> -UserName <user>
```

Optionally, you can store the session as variable instead of entering it by `New-PSSession`.

```ps1
$session = New-PSSession -HostName <host> -UserName <usr>
```

## Disconnect from Remote

```ps1
Exit-PSSession
```

## Execute Command on Remote

- Execute a same command on one or more remotes.

```ps1
Invoke-Command -HostName <host1>[,<host2>, ...] -UserName <usr> -ScriptBlock { <cmd> }
```

- Execute on a persistent session

```ps1
$session = New-PSSession -HostName <host> -UserName <usr>
icm -Session $s -ScriptBlock { <cmd> }
```

> [!NOTE]
> `Invoke-Command` returns deserialized object converted from transferred xml from remotes, it reflects data on remote only.
> You should only aggregate format it without mutating the values.
> `Enter-Session` does enter a PowerShell session, so it operates on a real PowerShell instance just like what you can do with SSH.
46 changes: 46 additions & 0 deletions docs/document/PowerShell/docs/What to Learn for New cmdlet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# What to Learn from New cmdlet

## Find Positional Parameter

Positional parameter matters for understanding the examples listed on documentations since the example might elide parameter name.
We can take advantages of `Get-Help` and `Select-String` to capture those parameters that have a explicit order.
The `-Context` includes 3 lines above and 5 lines below the display the whole info of the parameter.

```ps1
help <cmd> | sls 'Position\??\s*\d' -Context 3,5
```

The example usage is exactly what we have used here, the `Select-String`.
It has two positional parameters, `-Pattern` and `-Path`.
So in the previous example, the `-Pattern` was omitted.

```console
$ help sls | sls 'Position\??\s*\d+' -Context 3,5

-Path <string[]>

Required? true
> Position? 1
Accept pipeline input? true (ByPropertyName)
Parameter set name File, FileRaw
Aliases None
Dynamic? false
Accept wildcard characters? false
-Pattern <string[]>

Required? true
> Position? 0
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
```

## Find Parameters Accepts Pipeline

Similar to finding position parameter.

```ps1
help <cmd> | sls 'Accept pipeline input\??\s*true.*$' -Context 5,4
```
Empty file.
Empty file.
Empty file.

0 comments on commit 8cc4e3f

Please sign in to comment.