exec shell: detect default shell

Instead of using configured shell (e.g. bash) we can autodetect default container user's shell and execute it.
This is much safer because not all containers may have installed shell that is configured in ctop.
This commit is contained in:
Sergey Ponomarev 2020-11-20 10:42:52 +02:00
parent 5ec02f760e
commit 53a6b36bf5
1 changed files with 10 additions and 2 deletions

View File

@ -358,8 +358,16 @@ func ExecShell() MenuFn {
ui.DefaultEvtStream.ResetHandlers()
defer ui.DefaultEvtStream.ResetHandlers()
if err := c.Exec([]string{"/bin/sh", "-c", "printf '\\e[0m\\e[?25h' && clear && /bin/sh"}); err != nil {
// Detect and execute default shell in container.
// Execute Ash shell command: /bin/sh -c
// Reset colors: printf '\e[0m\e[?25h'
// Clear screen
// Run default shell for the user. It's configured in /etc/passwd and looks like root:x:0:0:root:/root:/bin/bash:
// 1. Get current user id: id -un
// 2. Find user's line in /etc/passwd by grep
// 3. Extract default user's shell by cutting seven's column separated by :
// 4. Execute the shell path with eval
if err := c.Exec([]string{"/bin/sh", "-c", "printf '\\e[0m\\e[?25h' && clear && eval `grep ^$(id -un): /etc/passwd | cut -d : -f 7-`"}); err != nil {
log.StatusErr(err)
}