Pexpect and GRUB - Why is Pexpect showing me a blank GRUB menu? - boot

I am attempting to script a reboot of a system and I have several GRUB entries. Pexpect doesn't seem to "see" the menu items.
Here is a code snippet:
def get_menu_selections(xtn):
print "Waiting for GNU GRUB to show"
xtn.expect_exact("GNU GRUB", timeout=480)
time.sleep(3)
xtn.expect_exact('Use the ^ and v keys')
print xtn.before
print xtn.after
def main():
connection = pexpect.spawn('ssh -l user -p2288 1.2.3.4')
# reboot box
get_menu_selections(connection)
main()
To explain why my snippet is like it is: Once "GNU GRUB" is on the screen, then my timeout stops, which means the wait for the system to reboot is over. At that point, I am guessing that GRUB draws the box, then fills it in, so I'm sleeping 3 seconds to wait for the content of the GRUB menu to be drawn on the screen. After I wait, then I was going to match on "Use the ^ and v keys" as my match so I can get a before and after.
This is what my GRUB looks like:
GNU GRUB version 1.98+20100804-14+squeeze1
+--------------------------------------------------------------------------+
|Base OS |
|Base OS -> ttyS0 |
|Base OS (recovery mode) |
|Base OS -> ttyS0 (recovery mode) |
|System Rescue |
|System Rescue -> ttyS0 |
| |
+--------------------------------------------------------------------------+
Use the ^ and v keys to select which entry is highlighted.
Press enter to boot the selected OS, 'e' to edit the commands
before booting or 'c' for a command-line.
The highlighted entry will be executed automatically in 0s.
Instead of seeing the menu items, I'm only seeing the drawn outline and the text at the bottom. This is what my code prints to the screen:
+--------------------------------------------------------------------------+
| |
| |
| |
| |
| |
+--------------------------------------------------------------------------+
Use the ^ and v keys
I'd like to get the menu options into a buffer ("before", "match", or "after") so that I can take inventory. Any idea how to grab the menu items?

I suspect that GRUB first prints the Use the ^ and v keys messages before printing the menu entries. So try like this:
def get_menu_selections(xtn):
print "Waiting for GNU GRUB to show"
xtn.expect_exact("GNU GRUB", timeout=480)
time.sleep(3)
xtn.expect_exact('Use the ^ and v keys')
time.sleep(3)
xtn.expect_exact('System Rescue -> ttyS0')
print xtn.before
print xtn.after

Related

Why my bash terminal shows "?[30;43m" instead of a well formatted text when using Symfony CLI commands like server:start?

I switched from Windows cmd terminal to bash in PhpStorm and now when I run Symfony CLI commands like server:start it shows characters like ?[30;43m instead of a formatted text.
I tried these Symfony commands on both PhpStorm and Visual studio code with the same results. It did work on git bash terminal though.
Here a screenshot from PhpStorm executing server:start:
and a screenshot using Windows cmd for the same command:
Do you know how to solve this problem?
My solution
I left Windows to Linux. Now everything works fine. I could not make it work properly on Windows though.
I think the ? means that my terminal on windows had trouble with the escape character used to interpret the formatting code [30;43m.
Here is my homemade explanation of this kind of formatting code:
Use echo -e to use text formatting.
Syntaxe:
\e[FORMAT;FORMAT;FORMATm
\e is the escape character (\033 works too).
[ mark the beginning of the format code.
; separate formating code sequence.
m mark the end of the format code.
FORMAT has to be replaced by a formatting code:
character effects using 1 digit:
code | effect
---- | ------
0 | normal
1 | bold
4 | underlined
5 | blinking
7 | reverse colors
colors using 2 digit:
first digit (the target):
code | effect
---- | ------
3 | foreground
4 | background
second digit (the color):
code | effect
---- | ------
0 | black
1 | red
2 | green
3 | brown
4 | blue
5 | purple
6 | cyan
7 | light gray
These are color codes, from the ANSI Escape codes : https://en.wikipedia.org/wiki/ANSI_escape_code
Some terminals use these codes to apply text formatting, while others just display them as regular text. Most programs that print such codes in an attempt to color their output also have an option to silence them, in order to make the display more readable on dumber terminals.

How to suppress the general information for top command

I wish to suppress the general information for the top command
using a top parameter.
By general information I mean the below stuff :
top - 09:35:05 up 3:26, 2 users, load average: 0.29, 0.22, 0.21
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
Cpu(s): 2.3%us, 0.7%sy, 0.0%ni, 96.3%id, 0.8%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 3840932k total, 2687880k used, 1153052k free, 88380k buffers
Swap: 3998716k total, 0k used, 3998716k free, 987076k cached
What I do not wish to do is :
top -u user | grep process_name
or
top -bp $(pgrep process_name) | do_something
How can I achieve this?
Note: I am on Ubuntu 12.04 and top version is 3.2.8.
Came across this question today. I have a potential solution - create a top configuration file from inside top's interactive mode when the summary area is disabled. Since this file is also read at startup of top in batch mode, it will cause the summary area to be disabled in batch mode too.
Follow these steps to set it up..
Launch top in interactive mode.
Once inside interactive mode, disable the summary area by successively pressing 'l', 'm' and 't'.
Press 'W' (upper case) to write your top configuration file (normally, ~/.toprc)
Exit interactive mode.
Now when you run top in batch mode the summary area will not appear (!)
Taking it one step further...
If you only want this for certain situations and still want the summary area most of the time, you could use an alternate top configuration file. However, AFAIK, the way to get top to use an alternate config file is a bit funky. There are a couple of ways to do this. The approach I use is as follows:
Create a soft-link to the top executable. This does not have to be done as root, as long as you have write access to the link's location...
ln -s /usr/bin/top /home/myusername/bin/omgwtf
Launch top by typing the name of the link ('omgwtf') rather than 'top'. You will be in normal top interactive mode, but when you save the configuration file it will write to ~/.omgwtfrc, leaving ~/.toprc alone.
Disable the summary area and write the configuration file same as before (press 'l', 'm', 't' and 'W')
In the future, when you're ready to run top without summary info in batch mode, you'll have to invoke top via the link name you created. For example,
% omgwtf -usyslog -bn1
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
576 syslog 20 0 264496 8144 1352 S 0.0 0.1 0:03.66 rsyslogd
%
If you're running top in batch mode (-b -n1), just delete the header lines with sed:
top -b -n1 | sed 1,7d
That will remove the first 7 header lines that top outputs and returns only the processes.
It's known as the "Summary Area" and i don't think there is a way at top initialization to disable those.
But while top is running, you can disable those by pressing l, t, m.
From man top:
Summary-Area-defaults
'l' - Load Avg/Uptime On (thus program name)
't' - Task/Cpu states On (1+1 lines, see '1')
'm' - Mem/Swap usage On (2 lines worth)
'1' - Single Cpu On (thus 1 line if smp)
This will dump the output and it can be redirected to any file if needed.
top -n1 |grep -Ev "Tasks:|Cpu(s):|Swap:|Mem:"
To monitoring a particular process, following command is working for me -
top -sbn1 -p $(pidof <process_name>) | grep $(pidof <process_name>)
And to get the all process information you can use the following -
top -sbn1|sed -n '/PID/,/^$/p'
egrep may be good enough in this case, but I would add that perl -lane could do this kind of thing with lightning speed:
top -b -n 1 | perl -lane '/PID/ and $x=1; $x and print' | head -n10
This way you may forget the precise arguments for grep, sed, awk, etc. for good because perl is typically much faster than those tools.
On a mac you cannot use -b which is used in many of the other answers.
In that case the command would be top -n1 -l1 | sed 1,10d
Grabbing only the first process line (and its header), only logging once, instead of interactive, then suppress the general information for top command which are the first 10 lines.

Print and update multiple lines in fixed position

I would like to print an output like the following in a fixed position while the numbers in the block keep updating every couple of seconds. It is similar to what top does.
Jobs monitor:
+-----------------------------------------+
| Waiting | Launched | Running | Finished |
+-----------------------------------------+
| 319 | 364 | 94 | 201 |
+-----------------------------------------+
Elapsed time: 21s
Is there a way to do that?
With only one line, I could do it with STDOUT.flush and "\r", but it does not work for multiple lines since the carriage will put the cursor at the beginning of the new line only.
The curses library is one way to make this work. It allows you to write to locations on a 2-d screen so you're not constrained to the current line. This question has some good resources for learning curses.

fanotify unable monitor entire system for FAN_OPEN_PERM event by multi-threaded program, and to ignore directories

I want to monitor whole system for FAN_OPEN_PERM | FAN_CLOSE_WRITE events by a multi - threaded program, and ignore some directories (say /home/mydir). I used fanotify_init() and fanotify_mark() in main() as:
//Is there any way to use FAN_GLOBAL_LISTENER?
fd = fanotify_init(FAN_CLOEXEC| FAN_NONBLOCK | FAN_CLASS_CONTENT | FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS, O_RDONLY | O_LARGEFILE)
...
//Marking "/" (doesn't work as multi-threaded program) or "/home" (works fine)
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN_PERM | FAN_CLOSE_WRITE | FAN_EVENT_ON_CHILD, AT_FDCWD, "/")
....
//Now, to ignore directory
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_ONLYDIR | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN_PERM | FAN_CLOSE_WRITE | FAN_EVENT_ON_CHILD, AT_FDCWD, "/home/mydir")
In my program, main() reads events and pass it to multiple threads to process further.
Problems : 1) System hangs for this multi-threaded program in case of monitoring "/", but works fine for "/home". 2) Still I am getting notifications for "/home/mydir" (marked "/home" & ignored "/home/mydir").
How to mark entire system without any problem with multi-threaded program?
How to use ignore mask to ignore entire directory (recursively)?
(Kernel 2.6.38-8-generic)
Read the man page.
the FAN_OPEN_PERM flag fires up an event when privileges are required to open the file. If you open a file, let say in /tmp, it does nothing.
Instead you should use FAN_OPEN.

How to pimp my (VS2010) editor?

While researching another issue, I came across this: http://james.padolsey.com/javascript/sorting-elements-with-jquery/ which made me green with envy for that color scheme (hey, I'm Portuguese).
So I found a .vssettings file online I liked, and tried to install it via "Import and Export Settings" - it has no effect; my "blah" color scheme remains the same (even after restarting VS2010). It's not my process that's wrong, as I was able to do it at home, the only difference being I've got VS2012 there.
So why are the vs-settings settings not getting set? To be more specific, I downloaded wekeroad-ink.vs-settings from http://studiostyl.es/schemes/wekeroad-ink (linked to it from http://weblogs.asp.net/scottgu/archive/2010/04/29/download-and-share-visual-studio-color-schemes.aspx).
I select Tools | Import and Export Settings... | Import selected environment settings | Next | Yes, save my current settings | Next | select My Settings | wekeroad-ink.vssettings | Next | Select "All Settings" | Finish | see "Your settings were successfully imported from wekeroad-ink.vssettings." and "To finish the wizard, click Close." | Close, ... no change in my editor colors. Restart VS2010; still no joy.
What's the trick to get it to "take" in VS2010?

Resources