log in adb shell "interactive mode" and "non-interactive mode" - adb

I'm writing script to run iperf on linux device accessible via adb.
From a terminal, when I run the command: adb shell iperf -c ......
I get the log of the command 50 s after the command starting.
inconvenient: during 50 s you are not sure that iperf has started
I tried the logcat command (logcat --pid ...), It gives the same result.
From a terminal, when I enter adb session first and then I run the command: iperf -c .....
I get the log of the command in real time.
inconvenient: I'm not able to automate the procedure, because I have to start the adb session first
For the first use case: Is there a way to force "log flush" in order to get the log in real time?
For the second use case: Is there a way to send commands to already opened adb shell session?
Is there any other ways to launch iperf on device and get the log in real time?

The solution i found is the library pexpect that allow to interact with adb session.

Related

catch console output from background process

I have a program running on armbian single board computer. The program starts with -b option during the startup of the system. I created this simple shell script
#!/bin/bash
#Myprog server start
sudo -b /home/myprog/myprog
This program is C written and it sometimes outputs some information with printf functions. But since it is started with -b option there's is noting in the console.
Now when I log in to the armbian via ssh with Putty I want to occasionally read the output of this program. Is it even possible?
Not exactly what you’re asking, but generally speaking it’s better practice to redirect output to a log file than to try to interactively look at the console output for a background app.
Something like:
sudo -b /home/prog/myprog >> /home/prog/log.txt 2>&1
Should do it.
Then view output with
tail -f /home/prog/log.txt
If it’s really important to you to run interactively without logs, I would suggest running it from within “screen” without backgrounding it.
screen
sudo /home/prog/myprog
Then ctrl-d to detach and let it run in background. screen -r to reattach.

Adb Logcat to print only live traces

I am using following command which prints android device logs from connected device buffer in my desktop.
adb logcat
Now requirement is, i am running a test and want to see the output of current action in logs.
That is i need logs only since when i start logging command.
i don't want logs from the buffer or the logs prior to time when logging is started.
Also i don't want to clear the buffer using -c argument. because somebody (some other process) may need buffered traces during testing
adb logcat --live-trace
Thanks in advance for the help.
i found the perfect way to do it!
Read current time from the device only.
adb shell echo $(date +'%Y-%m-%d %H:%M:%S')
Merge the above output with ".000" (just adding milliseconds) and save into some variable say %timestamp%.
Then,
adb logcat -v threadtime - D -T %timestamp%
You can use it in one command:
adb logcat -v threadtime - D -T $(adb shell echo $(date +'%s')).000

Automating putty with .bat file [duplicate]

I want to run a few shell commands every time I SSH to a server via PuTTY. I'm connecting to a production web server managed by someone else, and I don't want to store my own scripts there.
I see the option Connection > SSH > Remote Command, but if I put my initialization commands there, after starting the session, it closes immediately after the commands execute. How can I run the Remote Command, and then keep the session open so I can continue using it?
The SSH session closes (and PuTTY with it) as soon as the command finishes. By default the "command" is a shell. As you have overridden this default "command" and yet you want to run the shell nevertheless, you have to explicitly execute the shell yourself:
my-command ; /bin/bash
See also Executing a specific command on the server.
One option to go is set up your putty remote command like this:
ls > dir.ls & /bin/bash
In this example command you want to run is "ls > dir.ls" what creates file dir.ls with content of directory listing.
And as you want to leave shell open you can add aditional command "/bin/bash" or any other shell of your choice.

windows, Putty, pscp combined in to a single batch file, is it possible?

I'm having trouble getting an answer to my question, in laymens terms. It is probably my lack of knowledge on the subject so, I'm dumbing down the question. I have a windows machine that I run the putty tool from and connect to a linux box. I run " killall /bob/bin/myfile.out " then close putty then type in a cmd prompt pscp.exe myfilet.out.2.3.4 root#192.168.1.1:/bob/bin/myfile.out . Can someone show me how to combine these into a single windows batch file? thank you
You could use the free command line tool Plink to run commands on external servers via SSH.
#echo off
Plink root#192.168.1.1 "killall /bob/bin/myfile.out"
pscp.exe myfilet.out.2.3.4 root#192.168.1.1:/bob/bin/myfile.out || echo an error occurred when copying the file.
the command after || on the second line will only run if an error level is set by the previous command.
I can't add comments yet, but can you elaborate on how you login with putty, but not do the exact same thing with plink? Plink not only accepts all the same options as putty, but if you have a saved session in putty, you can access it from plink. Without any subcommands, plink should essentially make you CMD shell look like a crude putty window, with subcommands, it will execute them and return:
C:\Users\riglerjo>plink savedputtysession
Using username "rigler".
# hostname
s9-chicago.accountservergroup.com
-bash-3.2$ exit
logout
Run the remote command as an option on plink:
C:\Users\riglerjo>plink savedputtysession hostname
s9-chicago.accountservergroup.com

Shell script to startup, exit and run other scripts once it completes

I have been trying to automate this process in shell script in a unix box. I am new to shell scripts.
I have not been able to figure out how to detect when the startup for it has finished . Also, how to
create a new terminal and check the domain.log for the new terminal. I would appreciate if anyone
can help me on this .
cd $/home/oracle/12/bin
./lsnrctl start
Login to sqlplus with username sys as sysdba and password: oracle3211
and run the database startup command startup
Once it is started type exit
--now start the dbconsole
cd $/home/oracle/12/bin
./emctl start dbconsole
--open a new terminal and execute this
$/home/oracle/startWeblogic.sh
--wait for domain.log keyword in the log file to confirm the server has started
--if the server is started proceed to the following in a new terminal
$/home/oracle/startManagedWeblogic.sh
--after this access the following urls
https://178:198:29:28:1167/em (username=system1, password=oracle123)
https://178:198:29:28:1176/em (username=system2, password=oracle132)
Sounds like you also need to learn about the GNU screen command. You may already have it installed. Try screen -R -D
If it gives you a shell prompt at the top of the screen then it works. Now if you disconnect your ssh session or close your terminal window (click on the X), the next time you log in and run the exact same screen command, you are reconnected to the same running terminal session.
This allows you to run scripts, etc. which do not stop when you disconnect. And when you reconnect you can see their current output.
Also, you can have many terminal sessions. Ctrl-A c creates another one. Ctrl-A [space] and Ctrl-A [backspace] rotate between sessions in the list. Ctrl-A ? gives you all the other Ctrl-A commands that you can use. For instance, one session could be tail logfile while another one is running a program waiting for input.

Resources