LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   Bash scriipt calling Konsole in Sudo Mode (https://www.linuxquestions.org/questions/linux-desktop-74/bash-scriipt-calling-konsole-in-sudo-mode-4175666677/)

TBotNik 12-28-2019 12:17 PM

Bash scriipt calling Konsole in Sudo Mode
 
All,

I'm running Kubuntu, so it is "Konsole", not "Terminal" on my box. I need a bash script that does the following:
  1. Kills any current Konsole sessions,
    Guessing it is the cmd: myvar < ps -A | grep konsole && kill $myvar
  2. Re-opens Konsole with 3 session tabs,
  3. Logs each tab session into sudo mode for root
I need to register this in the Login Registry so I always have these 3
Konsole sessions open at the start of any login on my computer.

It's such a pain to have to manually do this every time I login.

Ideally, for security, the command will be:
Code:

bash /...path.../sudologin.sh mypass
which will also allow it's call at anytime and be used by any linux user and also allow, with the right symlink or rc registry, it to be called without the path name. Non KDE users will use "terminal" not konsole.

All help appreciated!

Cheers!

TBNK

BW-userx 12-28-2019 12:48 PM

sounds like a lot to figure out, your side panel says you've been using Linux for at least 3 yrs now, what have you figured out so far, other then the steps of operation?

using a Konsole, or you can install a different terminal to work in so it does not kill that one while you're working on figuring out how to kill a current Konsole terminal. work out the steps in a cli then add to a script test, fix, repeat until you get it. one step at a time.

somethink like this to kill kill kill the terminals
Code:

ps ux | grep xfce4-terminal | xargs kill
xfce4-terminal --tab -T one
xfce4-terminal --tab -T two
xfce4-terminal --tab -T three

got a look up how your terminal you're going to use is used to do these things.

teckk 12-28-2019 01:10 PM

Quote:

Kills any current Konsole sessions
pkill Konsole

Quote:

Re-opens Konsole with 3 session tabs,
https://docs.kde.org/trunk5/en/appli...ole/index.html
https://docs.kde.org/trunk5/en/appli...e-options.html

Quote:

Logs each tab session into sudo mode for root
See:
man sudo
man sudoers
man kill
man pgrep
man ps
man killall

TBotNik 01-23-2020 12:37 PM

Progress
 
Quote:

Originally Posted by teckk (Post 6072013)
pkill Konsole


https://docs.kde.org/trunk5/en/appli...ole/index.html
https://docs.kde.org/trunk5/en/appli...e-options.html


See:
man sudo
man sudoers
man kill
man pgrep
man ps
man killall

teckk,

From what you are saying my script needs to say:
Code:

curdir='$pwd;
cd /Scripts/Extras/
pkill konsole;
konsole --tabs-from-file ktabs.txt

But I have not found what to put into the ktabs.txt file to:
  1. Open the konsole with the tab bar at the top,
  2. open each tab with execution of: sudo su -u root -p mypass. I understand I need a line for every tab I open,
I'm still researching this but no progress so far!

Cheers!

TBNK

TBotNik 01-23-2020 12:49 PM

All,

On the Redhat and Suse flavors of Linux the cli cmd:
Code:

kdesu konsole
works but on the debian and Ubuntu flavors the cli cmd:
Code:

konsole -e "su -"
is the working cmd for opening a new konsole session.

Since I'm attempting the opening of multiple tabs, needing the ktabs.txt file, not sure where to put this command set, in the ktabs.txt file or before it?

Cheers!

TBNK

Turbocapitalist 01-23-2020 01:08 PM

It seems that other terminals are better than konsole nowadays. But with konsole,

Code:

#!/bin/sh

pkill -x konsole
konsole -e su - & pid=$!
konsole --new-tab -e sudo -i exec /bin/bash
konsole --new-tab -e sudo -i exec /bin/bash
konsole --new-tab -e sudo -i exec /bin/bash

echo $pid

exit 0


TBotNik 01-23-2020 01:35 PM

Code
 
All,

The code I currently have is Bash Script:
Code:

#! /bin/bash
# Script to login to SUDO mode
# CMD: bash /Scripts/Extras/sudologin.sh

# Get the current dir to enable return to it
curdir=$pwd;
cd "/Scripts/Extras/";
# kill the current konsole sessions
pkill konsole;
# Restart konsole in sudo mode with tabs from the file
konsole -e "su -" --tabs-from-file ktabs.txt
# Return to the starting dir
cd $curdir;

and the "ktabs.txt file of:

Code:

#! ktabs.txt file

title: Konsole%n;; workdir: /home/files;; profile: Shell
title: Konsole%n;; workdir: /home/files/Dropbox;; profile: Shell
title: Konsole%n;; workdir: /Scripts; profile: Shell

The problem here is when executed from Konsole the kill terminates the commands, so unless it is somehow stored in memory the remaining "restart" does not execute. I'm
adding this to a custom application launcher to get around this, but if you know the way to store this command set for continued execution it would be nice.

Out of curiosity I'm wondering if the "nohup" background run cmd will work?

Cheers!

TBNK

Turbocapitalist 01-23-2020 01:36 PM

The -e option should be the last item on the line.

TBotNik 01-23-2020 01:44 PM

I ran my code, both from a launcher and from an "F2" window piping to error file and got no errors, but also the 3 knosole sessions did not apprear, so there is something wrong with the code.

Cheers!

TBNK

TBotNik 01-23-2020 01:47 PM

Quote:

Originally Posted by Turbocapitalist (Post 6082145)
The -e option should be the last item on the line.

Turbocapitalist,

Thanks! Moving the -e to the end of the line, it now executes only I'm just only getting 2 tabs and none are in the sudo mode.

Cheers!

TBNK

TBotNik 01-23-2020 01:56 PM

All,

OK found the issue with the 3rd tab in the ktabs.txt file but now wondering if the sudo cmd needs to be in that file at the end of each tab line, since none of the sessions are coming up "SUDO"?

Cheers!

TBNK

Turbocapitalist 01-23-2020 02:03 PM

Here is an example:

Code:

#!/bin/sh

t=$(tempfile ktabs.XXXXX) || exit 1

cat <<EOT > ${t}
title: %n;; workdir: /tmp/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: %n;; workdir: /tmp/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: %n;; workdir: /tmp/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: %n;; workdir: /tmp/;; profile: default;; command=/usr/bin/sudo -i /bin/sh

EOT

pkill -x konsole

konsole --tabs-from-file ${t}

sleep 1

rm -f ${t}

exit 0

If there is still difficulty, try adding "set -xv" as the second line.

TBotNik 01-24-2020 02:12 PM

Quote:

Originally Posted by Turbocapitalist (Post 6082156)
Here is an example:

Code:

#!/bin/sh

t=$(tempfile ktabs.XXXXX) || exit 1

cat <<EOT > ${t}
title: %n;; workdir: /tmp/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: %n;; workdir: /tmp/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: %n;; workdir: /tmp/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: %n;; workdir: /tmp/;; profile: default;; command=/usr/bin/sudo -i /bin/sh

EOT

pkill -x konsole

konsole --tabs-from-file ${t}

sleep 1

rm -f ${t}

exit 0

If there is still difficulty, try adding "set -xv" as the second line.

Turbocapitalist,

Thanks, but didn't work! Still never logs to SUDO mode!

Cheers!

TBNK

TBotNik 01-24-2020 02:22 PM

Quote:

Originally Posted by TBotNik (Post 6082149)
Turbocapitalist,

Thanks! Moving the -e to the end of the line, it now executes only I'm just only getting 2 tabs and none are in the sudo mode.

Cheers!

TBNK

Turbocapitalist,

I see in some HOWTOs that it's -c for a cmd instead of -e to execute, but my system runs the -e. My question then is should the -e be in the ktabs.txt file. And should that replace the:
Code:

command=/usr/bin/sudo -i /bin/sh
you showed in your script as that "command=/usr/bin/sudo -i /bin/sh" is not working. Also should it be the actual cli cmd of "sudo su -u root -p mypass"? That would have the end of each tab line of:
Code:

-e sudo su -u root -p mypass
So I tried that and no change not coming up SUDO!

I'm seeing this is one of those: "Hold your mouth just right!" situations! He He!

Cheers!

TBNK

TBotNik 01-24-2020 02:31 PM

Turbocapitalist,

Would it be that my script needs to call another script the uses a for loop to find each konsole tab and execute the "sudo su -u root -p mypass" cmd on each tab?

Cheers!

TBNK


All times are GMT -5. The time now is 08:15 PM.