Bash script to read USB UPS status - linux

I have an UPS attached via USB port to my linux machine. Unfortunately the UPS is unsupported and doesn't show up as a reconized device. However it DOES show up correctly with the lsusb command.
I'd like to read the battery status from my UPS via a simple bash script (triggered by cron for example). I don't want a fully functional script as it would require to know the protocol of the specific UPS; I just want to create a script to write raw commands and read raw responses to/from the UPS given the known USB address, then it will be up to me to encode the actual command and parse the response.
Thank you.

You might be able to use the normal echo command to send strings directly to a device, like:
echo "some command" > /dev/usbXXX
To get the output you might be able to just read from it using e.g. cat:
cat /dev/usbXXX | grep "what you're looking for"

Related

Passing control characters to remote bash shell (alternative to ssh)

I'd like to run a remote bash shell that is fully interactive.
The client side should be able to send commands to this bash process and get the outputs interactively.
This means that, just like in a local bash prompt, the correlation between commands and their respected responses isn't necessarily guaranteed, since we may get outputs from a previous command that runs in the background, after running another command.
It works when I run separate bash process and use pipes:
bash 2>&1 | tee file
bash > file 2>&1
However, if I need to send control characters like Tab for auto-completion or Ctrl+C, in case we want to terminate the current command.
It may sound similar to SSH protocol, but I'd like to experiment with this option of running an alternative remote shell, with different access control and UI experience.
So, my question is: How do I pass the control character to the remote bash process (for example, the auto-completion) and get back the results (for example, list of choices for auto-completion)?
NOTE: I don't wish to develop bash equivalent shell script, but just build a proxy process that is able to pass both data and control characters and provide seamless bash experience as in local /ssh mode.

How to access mplayer output/know when mplayer video stopped playing?

I'm running a bash script that will play a video with mplayer depending on an input from an Arduino (on/off).
When the movie ends, I need to get a timestamp in a txt file. First question is whether there's a command in mplayer slave mode to tell me that, so I can output a timestamp easily.
If not, here's my strategy so far:
I'm running mplayer in slave mode with a fifo, where I echo "pause", whenever I want it to stop.
So, I've been doing this: echo "get_time_pos" to my fifo, which will tell mplayer to show in my Terminal the current position in the movie in seconds. When I say in my Terminal, it's in the same window where I'm running my script.
Now, I need to store this value in a variable to be able to compare with the total length and then output time.
I'm stuck at getting this output into a variable in my bash script.
I recently put together a small bash library that may grow with time. At the moment, it has the functionality you're looking for. I'll explain how to get the info you seek and then point you to my library which simplifies the task.
To get the information you seek, you don't even need to call get_time_pos. You can simply dump the mplayer (not running in quiet mode) output to a file and search that for the last timestamp. The trick here is that the timestamps listed in the dump may not be intuitive to search because of some special characters that control how text is displayed. You have to replace some of these special characters with new lines so that you can easily search it. Then you have to grab the last two lines in case the last line is not a timestamp.
Using my bash library:
Now, if you would like to simplify this process, check out this little library I wrote. Follow the usage directions on my GitHub to incorporate it, and then when you play a media file, play it with the playMediaFile function. If you do this, you'll be able to call the getElapsedSeconds or getElapsedTimestamp function to retrieve the current playback position or the playback position after mplayer has stopped. Storing it to a variable from within bash would be as simple as:
pos=$(getElapsedSeconds)
or
pos=$(getElapsedTimestamp)
This library contains other functions as well. The isFinishedPlaying function may or may not also be of use to you.

Using different console for respawn process in inittab on embedded device

I am connected on my embedded device with the serial port and would like to start my custom binaries on boot and be able to see the output generated.
My /etc/inittab file contains:
console::respawn:/sbin/getty -L 115200 ttyAPP3 vt100
console::respawn:/usr/bin/mybinary
With this configuration, I can see the output of mybinary in the serial console but It is difficult/impossible to connect (insert login and password) to getty because of the interference of the output generated.
I tried to switch the output in inittab to another not used tty (tty10) like this:
console::respawn:/sbin/getty -L 115200 ttyAPP3 vt100
tty10::respawn:/usr/bin/mybinary
And now I can connect but how can I see the output generated to /dev/tty10 ?
I tried cat /dev/tty10 but nothing is shown.
I know the question is old, but it has no answers at all for crying out loud.
Remember that a TTY is both an output device and also an input device -- by cat'ing from it you're reading input from the terminal which means the keyboard, NOT the screen.
I don't know if there's a parallel in other *nixes, but Linux systems have /dev/vcsX and /dev/vcsaX character devices (nodes c,7,0+X and c,7,128+X respectively) for each /dev/ttyX device - these are mirrors of the data currently on the output of the TTY device (ie. the screen part of the TTY, not the keyboard part). These will give you what you're looking for. The vcsaX devices will give you a displaybyte+attributebyte pair (i.e. the foreground/background text colour -- see other references for more information on text attribute bytes) for each character on the screen, while the vcsX devices give only the displaybyte for each character. Of course it's a raw stream/dump so if the row and/or column count of your terminal doesn't match that of the the TTY you're dumping then you'll need to parse the data and reformat it to match.
tl;dr: use "cat /dev/vcs10"
Hope that helps.

Ubuntu: wait for network link up and execute a bash command

In Ubuntu (the latest distro is fine), I want to reboot a router and inside a bash script I'd like to have a command that waits for the network link to be up again and, when it detects that, it has to start a bash command.
I could implement this with some kind of polling loop, but the ideal solution would be to have a bash command that, when executed, waits for the link to be up and automatically executes a bash command that I gave to it.
I read something about dbus (and dbus seems the way to go) but it also seems that it takes too much time to fully understand how to use it properly. I was suggested to check if a tool like ethtool was able to do that kind of "wait and execute" but in the man pages I didn't find anything about it.
Note: I forgot to say that I'd like the command to check if the PHYSICAL layer of the link is up. So solutions working at upper layers are not accepted. Moreover, solutions involving putting scripts inside directories (such as/etc/network/if-up.d) are not accepted too.
Any ideas?
Thank you
The event listener I suggested:
inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected'
When you plug or unplug network cable, it will trigger echo 'Change detected', of course it could trigger just about anything.
And this will run as one off, but I take you know how to make a daemon out of it, if not it will be a good exercise to learn :)
If you want a command to check if the link is up or down use ip :
ip addr show eth0 | grep -Po "(?<=state ).*?(?=\s)"
DOWN
ip addr show wlan0 | grep -Po "(?<=state ).*?(?=\s)"
UP
You can check link is up/down by /sys/class/net/eth0/carrier file
cat /sys/class/net/eth0/carrier
if output is 1 then ethernet cable is plugged in and link is up.
if output is 0 then ethernet cable is removed and link is down.
Note:if you have interface other then eth0 then replace interface like eth2/eth3 in place of eth0

Redirecting stdin through a FIFO

I'm running a server app (written in Java) under GNU/Linux which takes input (from stdin, I guess) and interprets it to run some commands. I dont want to run the app inside a terminal window (I'd like to run a daemon), but I'd still like to be able to input commands whenever I want to. I thought I might be able to do that using fifos, so I created it using mknod. The problem is cat fifofile > java... and cat fifofile | java ... fail with a "file not found" error for some reason.
Using only cat to read and write and the fifo works flawlessly.
Is there any way to fix this, or any other way to achieve the same goal?
So, Minecraft? The best way to do this is to have a bona-fide tty for the console part of the application. screen is an easy way to do that.
Have you tried java < fifofile? What about something like exec 3<&0; exec 0<fifofile; java?
What shell are you using? You might be able to use process substitution or coprocesses if you're using a shell that supports them.

Resources