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

TBotNik 01-24-2020 02:55 PM

Will it work?
 
Turbocapitalist,

In particular I found this script:

Code:

for service in `qdbus | grep org.kde.konsole-`; do
    for session in `qdbus $service | grep ^/Sessions/`; do
        qdbus $service $session org.kde.konsole.Session.runCommand "cd"
    done
done

Ran qdbus and got:
Code:

:1.10
 org.freedesktop.Notifications
:1.13
:1.2
 org.kde.klauncher
:1.3
 org.kde.kded
:1.6
 org.kde.knotify
:1.8
:1.9
 org.gtk.vfs.Daemon
org.freedesktop.DBus

Tried it with the grep filter
"qdbus | grep org.kde.konsole"
Got nothing and cmd:
qdbus | grep org
Gave:
Code:

org.freedesktop.Notifications
org.kde.klauncher
org.kde.kded
org.kde.knotify
org.gtk.vfs.Daemon
org.freedesktop.DBus

And cmd:
"qdbus | grep konsole"
Opened a new terminal window in sudo mode, but not a new tab.

Cheers!

TBNK

Turbocapitalist 01-25-2020 01:41 AM

Both of the examples I provided worked for me. So I am having trouble reproducing the problem you describe. Could you try summarizing it concisely with different words?

TBotNik 02-07-2020 11:37 AM

ll,

Nothing I've tried has worked yet!

Can I get some help here?

TBNK

Turbocapitalist 02-07-2020 11:50 AM

I gave the solution in #6 above. Please be specific about any error messages it provides on your system.

Edit: the solution in #12 above works on more distros. Again, please be specific with the error messages.

TBotNik 03-09-2020 09:28 PM

Help Please
 
All;

No matter how I try to get this to work, none of the konsole sessions ever login to the SUDO mode!

Sure could use some more ideas, since none of the suggestions posted have worked!

Cheers!

TBNK

TBotNik 03-09-2020 09:32 PM

Quote:

Originally Posted by Turbocapitalist (Post 6082737)
Both of the examples I provided worked for me. So I am having trouble reproducing the problem you describe. Could you try summarizing it concisely with different words?

Turbocapitalist,

Sorry! No other words to describe it, it or none of the suggestions have worked! This does not produce errors, but wondering is there is a system log that might show at least something, so I know why it's not working.

Cheers!

TBNK

Turbocapitalist 03-10-2020 01:02 AM

What happens when you use the example in #12 above, but add in the following as the second line of the script?

Code:

set -e -v -x
It should show each line of the script before it is executing, including variable contents, but quit on the first error it encounters.

TBotNik 03-10-2020 11:45 AM

Quote:

Originally Posted by Turbocapitalist (Post 6099030)
What happens when you use the example in #12 above, but add in the following as the second line of the script?

Code:

set -e -v -x
It should show each line of the script before it is executing, including variable contents, but quit on the first error it encounters.

Turbocapitalist,

Your script creates the 4 konsole sessions you described but nonoe are SDUO mode which will show with the /root in the dir prompt.

I captured your script and changed it for the 3 I was defining and here's it's code:
Code:

#! /bin/bash
# Script to open 3 Konsole sessions logged into SUDO mode
#
# CMD: bash /Scripts/Extras/kslog.sh
#
# Must be run in the SYDO mode! Also must be run from the App Launcher!

# Get the current dir to enable return to it
curdir=$pwd;
cd "/Scripts/Extras/";


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

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

pkill -x konsole
konsole --tabs-from-file ${t}
sleep 1
rm -f ${t}
cd $curdir;
exit 0

Where do I insert your currently suggested line?

Cheers!

TBNK

Turbocapitalist 03-10-2020 12:03 PM

Insert it as the second line below the shebang. If you run the script manually from the shell, the added line will display each line of the script before it executes.

Then, as for sudo, take a look at each tab's invocation in there. In the script in the preceding post (#23) that would be lines 16 through 18 and you will see that sudo really is invoked. It is not invoked for the whole konsole program, that would be stupid as it is a graphical program. Instead sudo is invoked separately for each tab.

TBotNik 04-01-2020 09:11 PM

3 Tries
 
All,

OK I've written 3 scripts to try this and none of them work"

Script#1
Code:

#! /bin/bash
# Script to login to SUDO mode on all open tabs
# CMD: bash /Scripts/Extras/logsudo.sh

# Get the current dir to enable return to it
curdir=$pwd;
cd "/Scripts/Extras/";

konsole --tabs-from-file ktabs.txt -e "su -u root -p nomened.1497"
# Return to the starting dir
cd $curdir;

Script#2
Code:

#! /bin/bash
# Script to login all tabs in 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;

konsole --tabs-from-file ktabs.txt -e command=/usr/bin/sudo su -u root -p nomened.1497
set -xv;

# konsole --tabs-from-file ktabs.sh
# Return to the starting dir
cd $curdir;

Script#3
Code:

#! /bin/bash
# Script to login all tabs in SUDO mode
# CMD: bash /Scripts/Extras/tmpslogin.sh


t=$("
title: Konsole1;; workdir: /home/files/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: Konsole2;; workdir: /home/files/Dropbox/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: Konsole3;; workdir: /Scripts/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
")

pkill konsole

konsole --tabs-from-file ${t}
set -xv
sleep 1

exit 0

# title: %n;; workdir: /tmp/;; profile: default;; command=/usr/bin/sudo -i /bin/sh

I also tried putting the "set -xv" right after the pkill konsole cmd line, but that does not work either. There are no errors produce, so no idea what is wrong.

Cheers!

TBNK

Turbocapitalist 04-01-2020 09:26 PM

In your post above, script #3 is the closest to working. Export the contents of ${t} into a file and then point the --tabs-from-file at that file. Note the use of a temporary file there in post #12 above. The --tabs-from-file only works with an actual file.

TBotNik 04-02-2020 12:01 AM

Quote:

Originally Posted by Turbocapitalist (Post 6106842)
In your post above, script #3 is the closest to working. Export the contents of ${t} into a file and then point the --tabs-from-file at that file. Note the use of a temporary file there in post #12 above. The --tabs-from-file only works with an actual file.

Turbocapitalist,

OK! Modified Scipt #3 to:

Code:

#! /bin/bash
# Script to login all tabs in SUDO mode
# CMD: bash /Scripts/Extras/tmpslogin.sh


# t=$("
# title: Konsole1;; workdir: /home/files/;; profile: default;; command=/usr/bin/sudo -# i /bin/sh
# title: Konsole2;; workdir: /home/files/Dropbox/;; profile: default;; command=/usr/
# bin/sudo -i /bin/sh
# title: Konsole3;; workdir: /Scripts/;; profile: default;; command=/usr/bin/sudo -i /# bin/sh
# ")

pkill konsole
curdir=$pwd;
cd "/Scripts/Extras/";

konsole --tabs-from-file ktabs.txt
# konsole --tabs-from-file ${t}
set -exv
sleep 1

# Return to the starting dir
cd $curdir;
exit 0

# title: %n;; workdir: /tmp/;; profile: default;; command=/usr/bin/sudo -i /bin/sh

and file ktabs.txt contains:

Code:

title: Konsole1;; workdir: /home/files/;; profile: default;; command=/usr/bin/sudo su -u root -p mypass
title: Konsole2;; workdir: /home/files/Dropbox/;; profile: default;; command=/usr/bin/sudo su -u root -p mypass
title: Konsole3;; workdir: /Scripts/;; profile: default;; command=/usr/bin/sudo su -u root -p mypass

but the only action is the actual opening of konsole with the correct title and working dir, but not sudo mode.

I'm thinking something needs to be added to the ktabs.txt file to make it go sudo! Am I right? do I need to add the "set -exv" line after each konsole def line in that file?

Cheers!

TBNK

TBotNik 04-02-2020 12:13 AM

Well,

Modified ktabs.txt to:

Code:

title: Konsole1;; workdir: /home/files/;; profile: default;; command=/usr/bin/sudo su -u root -p mypass
set -exv
title: Konsole2;; workdir: /home/files/Dropbox/;; profile: default;; command=/usr/bin/sudo su -u root -p mypass
set -exv
title: Konsole3;; workdir: /Scripts/;; profile: default;; command=/usr/bin/sudo su -u root -p mypass
set -exv

No change in operation. Still opens the 3 konsole sessions in the normal, not sudo mode!

Cheers!

TBNK

Turbocapitalist 04-02-2020 12:40 AM

Please try the recipe shown in #12 verbatim. If it works, then make one change at a time. There are too many changes happening at the same time to make it easy to walk through this.

TBotNik 04-03-2020 01:57 PM

Quote:

Originally Posted by Turbocapitalist (Post 6106877)
Please try the recipe shown in #12 verbatim. If it works, then make one change at a time. There are too many changes happening at the same time to make it easy to walk through this.

Turbocapitalist,

I've told you at least 3 times the solution in #12 does not work!

To make sure here is the script directly from your code:

Code:

#!/bin/sh
# Script to login all tabs in SUDO mode
# CMD: bash /Scripts/Extras/newslogin.sh

curdir=$pwd;
cd "/Scripts/Extras/";

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

cat <<EOT > ${t}
title: Konsole1;; workdir: /home/files/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: Konsole2;; workdir: /home/files/Dropbox/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: Konsole3;; workdir: /Scripts/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
EOT

pkill -x konsole
set -exv
konsole --tabs-from-file ${t}

sleep 1

rm -f ${t}
# Return to the starting dir
cd $curdir;

exit 0

Then modified it to:

Code:

#!/bin/sh
# Script to login all tabs in SUDO mode
# CMD: bash /Scripts/Extras/newslogin.sh

curdir=$pwd;
cd "/Scripts/Extras/";

t="ktabs.txt";
# t=$(tempfile ktabs.XXXXX) || exit 1

# cat <<EOT > ${t}
# title: Konsole1;; workdir: /home/files/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
# tile: Konsole2;; workdir: /home/files/Dropbox/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
# title: Konsole3;; workdir: /Scripts/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
# EOT

pkill -x konsole
set -exv
konsole --tabs-from-file ${t}

sleep 1

#rm -f ${t}
# Return to the starting dir
cd $curdir;

exit 0

Even change3d the "ktabs.txt" to:

Code:

title: Konsole1;; workdir: /home/files/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: Konsole2;; workdir: /home/files/Dropbox/;; profile: default;; command=/usr/bin/sudo -i /bin/sh
title: Konsole3;; workdir: /Scripts/;; profile: default;; command=/usr/bin/sudo -i /bin/sh

# title: Konsole1;; workdir: /home/files/;; profile: default;; command=/usr/bin/sudo su -u root -p nomened.1497
# set -exv
# title: Konsole2;; workdir: /home/files/Dropbox/;; profile: default;; command=/usr/bin/sudo su -u root -p nomened.1497
# set -exv
# title: Konsole3;; workdir: /Scripts/;; profile: default;; command=/usr/bin/sudo su -u root -p nomened.1497
# set -exv

Added only the directory control and the "set -exv" you recommended.

None of that works except to the point of opening the 3 konsole sessions in the "normal" mode, but not "sudo" mode.

I suspected that the "sudo su" cmd was not where you think it is so entered cmd:

Code:

cd /usr/bin/sudo
And got the msg: "cd: /usr/bin/sudo: Not a directory", but it is in the /usr/bin dir. However the cmd: "command=/usr/bin/sudo -i /bin/sh" is not properly executing so something different is needed that is why I had tried:"command=/usr/bin/sudo su -u root -p mypass"! That changed nothing so I ran that cmd directly and got the error: "sudo: unable to resolve host (none)".

That cmd has to work directly from the cmdline, so need to be able to fix that and then I think it will work!

Cheers!

TBNK

PS
Oh! Running the cmd: "/usr/bin/sudo -i /bin/sh" @ the cmdline yields:

##########################################################
sudo: unable to resolve host (none)
#
##########################################################

with the "#" prompt and not knowing what to enter, only "crtl+D" exits at that point. Entering the password does not actually allow entrance into the sudo mode.

man sudo show that -l not -i is a legit option for the sudo cmd; but does not produce anything usable either.

TBNK

Turbocapitalist 04-03-2020 02:09 PM

What is the exact error message from running the following manually?

Code:

/usr/bin/sudo -i /bin/sh

TBotNik 04-23-2020 12:19 PM

Quote:

Originally Posted by Turbocapitalist (Post 6107510)
What is the exact error message from running the following manually?

Code:

/usr/bin/sudo -i /bin/sh

Turbocapitalist,

Error is:
Quote:

sudo: unable to resolve host (none)
Cheers!

TBNK

Turbocapitalist 04-23-2020 01:12 PM

Quote:

Originally Posted by TBotNik (Post 6115008)
sudo: unable to resolve host (none)

That means the error is with the configuration of sudo and not with konsole. Re-check your settings in /etc/sudoers and make sure you can run the above command manually before retrying with the terminal emulator.

TBotNik 04-24-2020 05:42 PM

Quote:

Originally Posted by Turbocapitalist (Post 6115032)
That means the error is with the configuration of sudo and not with konsole. Re-check your settings in /etc/sudoers and make sure you can run the above command manually before retrying with the terminal emulator.

Turbocapitalist,

What am I looking for in /etc/sudoers?

Cheers!

TBNK

Turbocapitalist 04-25-2020 02:26 AM

Quote:

Originally Posted by TBotNik (Post 6115504)
What am I looking for in /etc/sudoers?

You are looking for an error there. :/ Take a look at the output using the -l option.

Code:

sudo -l
It should list what you have available for that host. If that does not give a clue then look at /etc/sudoers itself.

Code:

sudo more /etc/sudoers
And pay attention to the parts you yourself have changed. Most distros seem not to be setup for resolving the host names properly, so you may have to have it ignore the host name. There are not too many use-cases for small-office-home-office situations where it would be useful anyway.

TBotNik 04-27-2020 10:13 PM

Turbocapitalist,

I never modify this file, so don't understand why it should error. Is the error msg saying my hostname is missing or invalid? I typed in the cmd: "hostname" and the return was "(none)". But I checked the /etc/hosts file and the hostname is set to "Aspire-LT" at 127.0.1.1

Doesn't the /etc/sudoers file read this /etc/hosts file?

Cheers!

TBNK

Turbocapitalist 04-28-2020 12:23 AM

No, I don't think it does read /etc/hosts. I am somewhat sure that sudo uses the same source as the hostname utility does.

I would recommend starting a new thread about the sudo problem and then returning to this thread when that is resolved. People more familiar with sudo will see the new thread even if they have ignored this one about konsole.

TBotNik 05-03-2020 11:52 AM

Turbocapitalist,

OK opened the new thread at:

Hope I explained it right!

Cheers!

TBNK


All times are GMT -5. The time now is 04:30 PM.