Page 1 of 1
Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Wed Oct 23, 2024 2:06 pm
by mikewalsh
Afternoon, gang.
I confess, I'm not entirely sure quite what I'm doing here.
On several occasions in the past, I've scripted an instance of rxvt to run, stay open, and execute a command to display the output. Which all works fine. However, I'm getting a wee bit stuck on this one.....whether it's old age playing tricks with the old grey matter, or I'm just having one of those days when it simply isn't functioning, I don't know.
Here's the scenario:-
termbin.com is a terminal-based pastebin, OK? The particular operation I want to try & script is this:-
You can run inxi, with whatever parameters you choose, send the output direct to termbin, and the output you get back is a URL to your data.....which anybody else can then view direct in their browser for trouble-shooting purposes.
If I run
.....in the terminal (these are termbin's own instructions, right?), I get back a URL as output. As expected.
What I'd like to do is to run a script which will open (u)rxvt, run that command & get the URL back, followed by sending that output - the URL - to a text file. Sounds simple, right? But trying to implement it is proving a right royal PITA...!
Code: Select all
rxvt --hold -e inxi -Fxzzr | nc termbin.com 9999 > /tmp/report.txt
.....is not doing it. Any suggestions, guys? Where am I going wrong?
Mike.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Wed Oct 23, 2024 4:07 pm
by fredx181
Hi Mike,
This should do:
rxvt --hold -e bash -c 'inxi -Fxzzr | nc termbin.com 9999 > /tmp/report.txt'
Or if you want to show the url in rxvt too (besides in /tmp/report.txt):
rxvt --hold -e bash -c 'inxi -Fxzzr | nc termbin.com 9999 | tee /tmp/report.txt'
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Wed Oct 23, 2024 4:54 pm
by williwaw
wouldnt you want to send the output of inxi to file, and send the file to termbin?
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Wed Oct 23, 2024 9:15 pm
by mikewalsh
@fredx181 :-
Thanks for that, Fred. That pretty much did the trick.....with a bit of modification.
The command now looks like this:-
Code: Select all
rxvt -ls -e bash -c '/usr/local/bin/inxi -Fxzzrc0 | nc termbin.com 9999 > /tmp/report.txt'
I've dropped the '-hold' option, since I don't want that terminal window open all the time. It stays open just long enough to run the command, upload the output to termbin, get the URL link back, and write that to a file in /tmp.....and then it closes. Moments later, a gxmessage window appears displaying the termbin.com URL, while a gtkdialog banner shows at the top of the screen & reminds the user to copy the URL before closing the gxmessage window.......because less than a second after closing gxmessage, the file in /tmp is deleted.
I've done it this way because trying to copy/paste to a browser URL bar with urxvt's middle-click function fails every time; with gxmessage, you CAN actually rt-click -> 'Copy'.
======================
@williwaw :-
I've done it like this because of differences between fora. Some don't care about bandwidth usage, and are happy for the full output to be pasted into a forum post in plain text. Other fora are quite fussy about wasting bandwidth unnecessarily; these prefer the pastebin link, because it's short & to the point, and the individual user only opens the output in their browser if they're interested .....and this way, sending the output direct via curl, it cuts down on the number of steps needed.
The reworked version can be found here:-
https://www.forum.puppylinux.com/viewtopic.php?t=10783
Mike.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Wed Oct 23, 2024 10:29 pm
by williams2
Why open a new terminal window?
Just execute it as a line in the first script.
Like this:
Code: Select all
echo 'running a script ...'
echo 'running a script ...'
echo 'running a script ...'
inxi -Fxzzrc0 | nc termbin.com 9999 > /tmp/report.txt
echo 'running a script ...'
echo 'running a script ...'
echo 'running a script ...'
or something like this.
Code: Select all
defaultbrowser $(inxi -Fxzzrc0 | nc termbin.com 9999)
trying to copy/paste to a browser URL bar with urxvt's middle-click function fails every
In most Pups, urxvt automatically copies selected text to both
primary and clipboard buffers.
So select the text in urxvt to copy the text
then press ctrl+V to paste it to Geany, Firefox, etc.
couldn't be easier.
(ctrl+L to focus to the url bar. So,
select text, ctrl+L, ctrl+V)
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Wed Oct 23, 2024 10:46 pm
by norgo
Hello @mikewalsh
here another application. https://github.com/theZiz/aha
Actually this app is doing the same like termbin.com.
The difference is, that you can write directly a html file and you can change the color.
It's often useful to turn background to black color because it is better readable.
You can read the file in a webbrowser or convert to pdf or whatever, I like it very much.
the commandline for your "inxi" example with black background
Code: Select all
inxi -Fxzzr | aha --black > ~/inxi.htm
- aha_output_example.jpg (104.58 KiB) Viewed 1534 times
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Thu Oct 24, 2024 12:15 am
by mikewalsh
@fredx181 / @williams2 :-
Okay; modified yet again. More files have been moved out of the 'DATA' sub-directory into the main one, and renamed; the tray icon script has been re-jigged to suit, as have some of the files themselves.
I've done away with the terminal altogether. Reading williams2's suggestions, I realised that I'd been trying all sorts of combinations earlier on; I had actually got a file to appear in /tmp WITH the info, BUT.....every other word or two, I'd got the word 'ESC' in a little black box, along with various combos of letters/numbers in square brackets, all muddled in with the output.
I initially thought I was doing something wrong.....and then I had a read through the 'options list' that I'd provided.....and realised what that 'c0' was that had been added onto the end of the 'filter list'. I.e:-
.....becoming
.....and so on, and so forth.
Inxi provides its terminal readout in different combinations of colours, to make it easier to read - somewhere around 40-odd different colour themes. I'd used one of them in the 'RUN' script's output command ('24').....but of course, this colour coding is all interspersed with the readout, and makes it all but impossible to read as a text file. So; 'c0' disables the colour theming - turns it off - along with all that superfluous code. Result? A clear, easy-to-read text file.
Which means that williams2's suggestion did in fact work as originally expected. 'Twas my own fault for not fully understanding what all that 'extra' coding every other word was.....but now that I do, Fred, the terminal stuff is effectively redundant. It's simply not needed.
The re-re-worked 'portable' is now uploaded to the original thread, as detailed a few posts back.....and is working very sweetly indeed.
Mike.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Thu Oct 24, 2024 12:40 am
by mikewalsh
@norgo :-
Heh. Ooh, that's very neat, mate. Works beautifully.
Am I right in thinking that anyone who wants to view your HTM file needs to have this installed.....or is it only used to actually generate the HTM file? In other words, the HTM file can be viewed anywhere, by anybody, and 'aha' won't be needed in order to view it?
Notice I've used the same '-c 24' (colour theme) as I do in the portable's output command. Anyways; thanks for sharing. Cheers!
Mike.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Thu Oct 24, 2024 1:51 am
by MochiMoppel
mikewalsh wrote: ↑Wed Oct 23, 2024 9:15 pmI've done it this way because trying to copy/paste to a browser URL bar with urxvt's middle-click function fails every time
???
Middle-click from urxvt or rxvt or any other selection source works for me. The browser's address field may have to be emptied first, which makes it a bit more tedious than pasting from clipboard, but it works.
@williams2 's proposal also works, but only in urxvt, not in rxvt and only if the perl extensions are implemented and configured accordingly. I wouldn't count on it.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Thu Oct 24, 2024 9:59 am
by mikewalsh
@MochiMoppel :-
MochiMoppel wrote: ↑Thu Oct 24, 2024 1:51 am
???
Middle-click from urxvt or rxvt or any other selection source works for me. The browser's address field may have to be emptied first, which makes it a bit more tedious than pasting from clipboard, but it works.
@williams2 's proposal also works, but only in urxvt, not in rxvt and only if the perl extensions are implemented and configured accordingly. I wouldn't count on it.
In fact, it's not Puppy that's at fault here, Mochi. I've eventually figured out it's the VicTsing mouse I've been using for the last few months; it appears to have an intermittent, "iffy" contact under the scroll-wheel. Since the middle-click doesn't get used here that often, I overlook this one small issue because otherwise it's a very good mouse.
It just catches me out on the occasions when it decides to misbehave, because I keep forgetting it HAS this "downside"...
=============================
As for the inxi -> termbin command, I'm just using it directly in the 'inxi-report' script now.....with no (u)rxvt or other terminal involved at all. Despite being a terminal-based pastebin, it seems happy to run direct from a Bash script, too:-
Code: Select all
#!/bin/sh
#
# Send Inxi output to Termbin.com, and get readout URL...
#
inxi -Fxzzrc0 | nc termbin.com 9999 > /tmp/report.txt
#
/usr/lib/gtkdialog/box_splash -placement top -bg white -fg black -timeout 10 -text " Copy URL BEFORE closing gxmessage window..! " &
sleep 0.25
#
gxmessage -bg yellow -fg black -name 'Termbin URL...' -center -file /tmp/report.txt
#
rm -f /tmp/report.txt
rm /usr/local/bin/inxi
Works for me.
Mike.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Thu Oct 24, 2024 10:28 am
by fredx181
Why remove /usr/local/bin/inxi ?
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Thu Oct 24, 2024 10:47 am
by MochiMoppel
mikewalsh wrote: ↑Thu Oct 24, 2024 9:59 am
Works for me.
Last line reads rm /usr/local/bin/inxi
Looks like your script will work only once
Did you try @williams2's proposal to send the URL generated by inxi -Fxzzrc0 | nc termbin.com 9999
directly to the browser, skipping the tempfile and gtkdialog and gxmessage detour?
[Edit] fredx181 posted while I was still writing, but I see that we have the same concerns
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Thu Oct 24, 2024 12:28 pm
by mikewalsh
@fredx181 / @MochiMoppel :-
Heh. Yah; good catch, guys. That's not supposed to be there. Since posting that script a little bit earlier, I've re-jigged things quite a bit more.
I originally linked the 'inxi' script into /usr/local/bin, because the linked 'RUN' script for the portable's executable tray icon was also called 'inxi' as well (in /usr/bin). This has been renamed to 'inxi-portable', and since the main 'inxi' script is now being used from within the portable directory, it no longer needs to be linked-in at all.
'Twas done this way because initially, I couldn't make the termbin command 'see' the 'inxi' script inside the portable; it appeared to only want to search the main file-system /bins, so I figured it was just easier to link the inxi script into one of the bins as part of the 'Tray-Add' script. So that's what I did.
Now, this morning, it's doing what it should have done all along, and is using the main 'inxi' script directly from inside the portable. I was having "one of those days" yesterday when I first started figuring this termbin report thing out.....nothing seemed to want to do quite what it was supposed to!
Code: Select all
#!/bin/sh
#
# Send Inxi output to Termbin.com, and get readout URL...
#
HERE="$(dirname "$(readlink -f "$0")")"
#
$HERE/inxi -Fxzzrc0 | nc termbin.com 9999 > /tmp/report.txt
#
/usr/lib/gtkdialog/box_splash -placement top -bg white -fg black -timeout 10 -text " Copy URL BEFORE closing gxmessage window..! " &
sleep 0.25
#
gxmessage -bg yellow -fg black -name 'Termbin URL...' -center -file /tmp/report.txt
#
rm -f /tmp/report.txt
=============================
I've added a small multi-launch GUI for anyone who wants to use this purely AS a 'portable' application. If the "Add tray icon" option is selected, the tray icon behaves as usual, launching the Inxi 'add options' GUI directly.
Yes, I know this is all awfully complicated for what is supposed to be just a simple, terminal-based application. But since it's the most commonly-requested diagnostic item on all the Linux fora that I belong to, I wanted to make it as simple as possible to use for any of our new Windows "refugees".
=============================
So; why don't I bypass the other stuff and open direct in the browser? I could, most certainly.....but for the simple reason that I didn't WANT to. I've seen my INXI readout so many times I'm sick of the sight of it, and I don't want to be opening it up in the browser every time it's requested. I'd rather just supply the URL direct; that way, only those who are interested in it will actually bother looking at it.
('Salright, guys; I know my approach often veers toward excessive over-complication, but I DO try to help make Puppy as accessible as possible for all......especially given that it's during the first days with a new operating system that most noobs NEED a certain amount of familiarity. I don't know of too many that dive straight into the terminal on their very first boot..! *shrug*)
Mike.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Thu Oct 24, 2024 5:47 pm
by puppy_apprentice
I you need only URL that you can paste into the forum:
Code: Select all
cal | nc termbin.com 9999 | xclip -selection c
Terminal output to html:
https://github.com/masukomi/oho
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Thu Oct 24, 2024 6:02 pm
by mikewalsh
@puppy_apprentice :-
Hm. Looks rather similar in concept to 'aha', which norgo posted about a little further back in the thread. Good find, though..!
Mike.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Fri Oct 25, 2024 12:38 am
by MochiMoppel
Instead of
Code: Select all
$HERE/inxi -Fxzzrc0 | nc termbin.com 9999 > /tmp/report.txt
/usr/lib/gtkdialog/box_splash -placement top -bg white -fg black -timeout 10 -text " Copy URL BEFORE closing gxmessage window..! " &
sleep 0.25
gxmessage -bg yellow -fg black -name 'Termbin URL...' -center -file /tmp/report.txt
rm -f /tmp/report.txt
try
Code: Select all
gxmessage -bg yellow -fg black -name 'Termbin URL...' -center $'Copy URL BEFORE closing this window..!\n'"$($HERE/inxi -Fxzzrc0 | nc termbin.com 9999)"
BW64 users may run this one-liner from the command line "as is" after removing $HERE/
because inxi is already included in BW64.
The script ends with a harmless warning since termbin.com adds a (non printable) NUL character to the URL. Ignore it.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Fri Oct 25, 2024 1:42 am
by williwaw
I started to use but I found this
https://www.malwarebytes.com/blog/detec ... ermbin-com
not sure if a particular url supplied by termbin is suspicious or what the issue is.
the warning at malwarebytes is undated
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Fri Oct 25, 2024 10:57 am
by mikewalsh
@MochiMoppel :-
MochiMoppel wrote: ↑Fri Oct 25, 2024 12:38 am
Instead of
Code: Select all
$HERE/inxi -Fxzzrc0 | nc termbin.com 9999 > /tmp/report.txt
/usr/lib/gtkdialog/box_splash -placement top -bg white -fg black -timeout 10 -text " Copy URL BEFORE closing gxmessage window..! " &
sleep 0.25
gxmessage -bg yellow -fg black -name 'Termbin URL...' -center -file /tmp/report.txt
rm -f /tmp/report.txt
try
Code: Select all
gxmessage -bg yellow -fg black -name 'Termbin URL...' -center $'Copy URL BEFORE closing this window..!\n'"$($HERE/inxi -Fxzzrc0 | nc termbin.com 9999)"
Ah, thanks for this, Mochi. That's much appreciated. I was trying to figure it out the other day, but my mind had just gone blank.....I simply wasn't thinking straight, and couldn't make stuff behave itself! Which is why I resorted to using what I could remember how to do.....
I've credited both you and Fred on the 'About' splash screen. (I may not have used Fred's code directly - like I have with yours - but it gave me ideas, and set me off on the right track..!)
Credit where credit is due.....and appreciated.
Mike.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sat Oct 26, 2024 1:10 am
by MochiMoppel
williwaw wrote: ↑Fri Oct 25, 2024 1:42 am
not sure if a particular url supplied by termbin is suspicious or what the issue is
All URLs look suspicious
Try @mikewalsh 's first approach and send the output to a file:
echo hello | nc termbin.com 9999 > /tmp/url.txt
Now the file should contain the URL sent by termbin, right?
Open the file in Geany and have a look
If you have a Chinese font installed you might see a bunch of Chinese characters, if not you might see a bunch of boxes. If the random number supplied with the URL is larger than 4 digits Geany may refuse to open the file. If this doesn't look suspicious, what does?
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sat Oct 26, 2024 1:58 am
by williwaw
MochiMoppel wrote: ↑Sat Oct 26, 2024 1:10 am
Try @mikewalsh 's first approach and send the output to a file:
echo hello | nc termbin.com 9999 > /tmp/url.txt
Now the file should contain the URL sent by termbin, right?
Open the file in Geany and have a look
I see what you mean, geany contains 瑨灴㩳⼯整浲楢潣⽭㝭㈷
but if I go back to the terminal and
# cat /tmp/url.txt
bash returns
https://termbin.com/m772
so maybe a geany issue? because if I open /tmp/url.txt with leafpad I also see.......
https://termbin.com/m772
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sat Oct 26, 2024 4:43 am
by MochiMoppel
williwaw wrote: ↑Sat Oct 26, 2024 1:58 am
so maybe a geany issue?
Bingo!
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sat Oct 26, 2024 9:54 am
by puppy_apprentice
Code: Select all
$HERE/inxi -Fxzzrc0 | nc termbin.com 9999 | xsel
/usr/lib/gtkdialog/box_splash -placement top -bg white -fg black -timeout 10 -text " The URL was copied to the clipboard... " &
sleep 0.25
https://www.ipqualityscore.com
- Screenshot.png (69.2 KiB) Viewed 1011 times
https://www.ipqualityscore.com/domain-r ... ermbin.com
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sat Oct 26, 2024 10:22 am
by Doggy
The command you're using is sending the output of inxi to nc, but you need to capture the URL from nc instead. Try this:
Code: Select all
rxvt --hold -e sh -c "inxi -Fxzzr | nc termbin.com 9999 > /tmp/report.txt"
This way, you're running a shell command that pipes the output of inxi through nc and directly into your text file. Give it a shot and see if that works!
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sat Oct 26, 2024 11:22 am
by fredx181
MochiMoppel wrote: ↑Sat Oct 26, 2024 4:43 am
williwaw wrote: ↑Sat Oct 26, 2024 1:58 am
so maybe a geany issue?
Bingo!
Well, not really a geany issue, I'd say, because the output url.txt is a "data" file.
Piping through "strings" fixes it, btw. :
Code: Select all
# echo hello | nc termbin.com 9999 > /tmp/url.txt
# file /tmp/url.txt
/tmp/url.txt: data
# echo hello | nc termbin.com 9999 | busybox strings > /tmp/url.txt
# file /tmp/url.txt
/tmp/url.txt: ASCII text
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sat Oct 26, 2024 6:19 pm
by williwaw
fredx181 wrote: ↑Sat Oct 26, 2024 11:22 am
Piping through "strings" fixes it, btw. :
Hi Fred,
I do not completely understand the difference between a data file and an ASCII text file, but if termbin.com returns a data file rather than an ASCII text file, is that a possible vector for a bad actor?
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sat Oct 26, 2024 7:58 pm
by puppy_apprentice
On Windows it shows as ASCII.
- mobaxterm-on-win.png (93.63 KiB) Viewed 771 times
Don't use $(nc termbin.com 9999 or command | nc termbin.com 9999) you can get command to execute if the bad guys take control of the service
- screenshott.png (36.14 KiB) Viewed 771 times
https://mobaxterm.mobatek.net
CRLF Injection
https://owasp.org/www-community/vulnera ... _Injection
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sat Oct 26, 2024 9:18 pm
by norgo
mikewalsh wrote: ↑Thu Oct 24, 2024 12:40 am
Am I right in thinking that anyone who wants to view your HTM file needs to have this installed.....or is it only used to actually generate the HTM file? In other words, the HTM file can be viewed anywhere, by anybody, and 'aha' won't be needed in order to view it?
Hello @mikewalsh
Aha is an Ansi Html Adapter (Converts ANSI escape sequences of a unix terminal to HTML code)
Aha is not needed to read the html file.
Furthermore is aha drastically faster in comparison to termbin.com
Code: Select all
time dmesg | aha > ~/dmesg.htm
real 0m0.018s
user 0m0.017s
sys 0m0.016s
Code: Select all
time dmesg | nc termbin.com 9999
https://termbin.com/06su
real 0m10.529s
user 0m0.018s
sys 0m0.006s
and simpler to use in my opinion
example:
dmesg --color=always | aha --black > ~/dmesg.htm
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sat Oct 26, 2024 9:43 pm
by puppy_apprentice
But with termbin.com, you can immediately share the output with other users. This is the functionality that Mike wanted i think.
You have to send the output from aha to the server yourself to show it to others.
Re: Write script to run a command in (u)rxvt, and send the output to a text file?
Posted: Sun Oct 27, 2024 1:26 am
by MochiMoppel
fredx181 wrote: ↑Sat Oct 26, 2024 11:22 amWell, not really a geany issue, I'd say, because the output url.txt is a "data" file.
@fredx181 @williwaw With "issue" I don't mean that it's a bug. On the contrary: Geany is completely correct here - if you agree with the way Geany tries to guess the encoding of a given file. When you look at Geany's status bar you can see that it considers the file to be encoded as UTF-16LE
The problem starts with the wrong encoding by termbin. Termbin adds 2 characters to the URL: A linefeed (0x0a) and a NUL character (0x00). This makes it a binary file and the mime type here is application/octet-stream. Geany is not supposed to read binary files and in most cases it refuses to do so with the message "The file does not look like a text file or the file encoding is not supported". But Geany supports UTF-16LE, and since UTF-16 reads a file in 2 byte chunks and the last 2 bytes are hex 0a and 00 and LE reverses the order the result is 000a, the correct encoding of a linefeed in UTF-16LE.
And indeed there is a linefeed in Geany's output. Now look at the first "block" character. In the block you can see the numbers 74 and 68, which means that this block is a placeholder for the Unicode character U+7468, a character that is part of the huge Unicode section "CJK Unified Ideographs" for Chinese/Japanese/Korean characters and which can't be properly displayed when a suitable Chinese font is not installed. Makes no sense? In UTF-16LE it does because termbin's URL starts with https://... Combining bytes of letter h (0x68) and letter t (0x74) and reversing the bytes results in 7468. Voilà, a Chinese character!
On the other hand Leafpad - seemingly - can read the file because it has a different policy. It just stops reading at the first NUL character it encounters and outputs whatever it was able to read up to this point.