How to open a new Konsole window from CLI - dbus

Instead of running konsole &, which is slow, I'd like to open a new Konsole window using D-Bus, which is faster because it doesn't have to start a new Konsole process.
However, inspecting the interface with qdbusviewer, I can't find a method call that opens a new window.

It turns out you have to use actions. You can list them with
qdbus org.kde.konsole-`pidof -s konsole` /konsole/MainWindow_1 org.kde.KMainWindow.actions
and you can call the action new-window with
qdbus org.kde.konsole-`pidof -s konsole` /konsole/MainWindow_1 org.kde.KMainWindow.activateAction new-window
From qdbusviewer I also found org.freedesktop.Application.Activate which also opens a new window but I didn't manage to call it using qdbus.

Related

How to attach the second next process with lldb

Is is possible to let lldb to attach to the 2nd new process of some executable? We could attach the next new process of some executable with
attach -w -n "/path/to/executable"
But now an action will launch a pair of instances of some executable, I want to only attach to the 2nd process.
lldb doesn't have a "wait for second next version" primitive. But using the SB API's it would be pretty easy to write a Python command that does an attach wait, then detaches after the first attach and immediately waits for the next one.
When you attach to a process in the SB API, you create an SBAttachInfo specifying what you want to attach to. Be sure to call SetIgnoreExisting(True) on the second attach, that way it won't try to attach again to the first process. More details on writing Python based commands is here:
https://lldb.llvm.org/use/python-reference.html#create-a-new-lldb-command-using-a-python-function
And the API reference is here:
https://lldb.llvm.org/python_reference/index.html

How to enable logging in xterm

Is it possible to turn on logging feature by default in xterm?
Lets say for example, I have example program in c that give an output in xterm everytime i ran the program from default bash terminal in linux. And I want to save the output that shows in xterm into a file everytime the programs is run.
I'm using centos7_x86_64 fyi
Thanks.
In Windowmaker:
Hold down ctrl + left click in xterm window (on the terminal text), click on "log to file".
A cool thing to do is when you're coding, log the terminal, and then when you cat the Xterm log, you see coding in fast forward. If you wanted you could video it.
Note that there are also other menus in xterm, accessible using Ctrl+Left Click, Ctrl+Middle Click, Ctrl+Right Click.
Log file will be in the directory that you launched Xterm from, and will be in the format: Xterm.log.<hostname>.<date>.<time>.log.
This is a very good question, there's no reason to mark down a question like this.
It can be done in two ways:
Using script:
xterm -e script mylogfile -c "someCommand -i input_file -o output_file -f someArg"
Redirect to a file:
xterm -e 'someCommand --arguments 2>&1> /path/to/mylogfile'
I do essentially this with "terminal-window", mrxvt, "hcm" and "pypty".
terminal-window wraps mrxvt, just filling in some commandline options. mrxvt is a lightweight, multitabbed, nonunicode terminal emulator not dissimilar to xterm.
hcm is a GUI that makes it easy to run a shell (or other command) on a remote host. It can also start an mrxvt with remote ssh's without requiring the GUI if you prefer (using hcm-term).
pypty is a /usr/bin/script reimplementation that is written in Python. It is not significantly different from /usr/bin/script, except it gives a "dated files mode", that allows you to have one file per pseudo terminal per day. So if you leave a shell logged in overnight, you get one file per day - this tends to make it easier to find what you're looking for.
All this combines to give you pseudo terminal logging with great ease. Commands are run on remote hosts, but logged locally. Just start a "fancy terminal-window" (or use hcm-term), and everything you see on the screen plus control characters (but not nonechoed passwords) will be logged under ~/.hcm/logs/<year>/<month>/<day>/* .
Also, if you hit the shell button in the lower right of mrxvt, you get another ssh session into the same remote host, which is also logged locally (to a different file under ~/.hcm/logs/...). When I started making use of that feature, I had no idea how much I would grow to like it.
You can obtain them from http://stromberg.dnsalias.org/~strombrg/hcm/ There's a video there that shows how easy it is to set up and get started with.
BTW, fancy terminal-window sets up $PS0 or "trap DEBUG" to give you command start times and finish times. It's great for post mortems. It does this without replacing any of the usual bash startup files.
I wrote terminal-window, hcm and pypty, but I can't take credit for mrxvt. :)
HTH

Is it possible to override SSH expecting password input from /dev/tty in C

I am developing a piece of software in C that needs to SSH to another machine and run commands as root.
Something that looks like this:
char* GetPasswd(void);
void run(char* apnSshCommand)
{
FILE* lphSshFD = popen(apnSshCommand,"w");
fprintf(lphSshFD,GetPasswd());
fflush(lphSshFD);
fprintf(lphSshFD,"#Command to run in shell");
fflush(lphSshFD);
}
GetPasswd() would be a callback to a gui where the user has typed in the password
I know that the above code is not possible since SSH looks to it's own /dev/tty to provide the password for authentication.
I have read posts such as this that teases an answer using ioctl() and fcntl() but does not provide one. Along with this that shows it is possible from the command line however I have not been able to translate it.
Using expect is NOT an option
Using SSH keys are NOT an option
The SSH C library is NOT an option
Using sshpass is NOT an option
Without these, the only thing that I can think of is starting a new child process and redirect/close file descriptors to control what ssh has access to.
EDIT: These restrictions come from the fact that the system I am working on is extremely old and does not contain tools such as expect, sshpass and the SSH C library as well as being subject to multiple restrictions in regards to when sshkeys can be used
This works for me:
Create a script called pwd.sh that emits the password:
#!/bin/bash
echo -n mypassword
Run ssh in a new session like this:
SSH_ASKPASS=/path/to/pwd.sh DISPLAY= setsid -w ssh root#192.168.1.10 <command>
The effect of setsid is to run ssh detached from the controlling terminal, which is what is needed for it to respect SSH_ASKPASS.
I haven't tried, but I would expect to be able to run this command from C using system().
For the record, I tested this with OpenSSH_7.2p2 on Ubuntu.
I was able to come up with a solution by looking at the source code for sshpass which I found here

Can not connect to an already active screen through system() in c

Here is the part of code I have written to stuff 0 to a screen session open in one of my Ubuntu terminal tabs.
char command[60];
strcpy( command, "screen -S 8305.pts-1.MYUb -X stuff $'0'" );
system(command);
It gets compiled fine with only a warning like
ignoring return value of ‘system’,
But when it comes to running I get the message shown below:
No screen session found.
I have tried system() with other shell commands and it works perfectly fine. The command for screen also works fine when you run it in a terminal session not in c code.
Most probably you are running the command as a different user than the user that owns the screen. For example running the binary as sudo.
You can run ps aux to find the user under which your binary is running as.
To make the system command work you should run it as the user who owns the screen.
I think the problem is you are using -S which creates a new named screen, and -X which submits a command to an already running screen session.
You either want:
system( "screen -S 8305.pts-1.MYUb cmd" );
OR
system( "screen -r 8305.pts-1.MYUb -X cmd" );
FYI -- I'm not sure what stuff $0 is suppose to be, and in the context of the code you supplied is not going to work -- but I believe that a different problem then the one you reported.
From man(1) page
-S sessionname
When creating a new session, this option can be used to specify a meaningful name for the session.
And
-X Send the specified command to a running screen session.

output to a new terminal

Suppose I create a thread from my "main" thread, which aims to monitor and print some variables in "main". But "main" is also generating some output, so I want to print the outputs of these two threads separately. How can I redirect the output of the new thread to a new console other than the one "main" thread is using in my c program?
I would output the monitoring information to a file, then use tail -f filename to display it in my other terminal.
That has the advantage that you can run grep or what-have-you on the output as well.
I would go with writing the data to a log file and then using tail -f log_file.txt from another window to watch it since that way you get to keep a copy of the data, but if you decide that you need to use separate terminals then you can just open the new terminal (tty) like a regular file. The main problem with this is that you probably really want it to open a new terminal window for you as well.
Opening a new terminal window to do something like this is tricky and is different between different terminal emulators. Here is another question about opening new terminals from a make file. You could try doing the same thing from your program or from a script that runs your program and run cat or tail in the new terminal to be your log window.
#!/bin/sh
truncate --size=0 ./logfile.txt
xterm "tail -f ./logfile.txt" 2>&1 > /dev/null &
your_program --log-file=./logfile.txt
I don't currently know of a better way to accomplish this.
Another thing you might want to look into is syslog.
What would you expect to happen in such case (externally)?
If you run a program you are attached to a console. You of course don't have to write output to the console (you can use files), but the console cannot be duplicated obviously.
Isn't using a different file descriptor good enough for you?

Resources