Bash Shell: How can I know if TeamViewer has disconnected? - linux

Sometimes TeamViewer disconnects itself (or gets disconnected) from its internet's main servers.
I am programming a script that will check if connection is lost and, if yes, kills and reopens the concerned process to make TeamViewer up and running again.
The problem is: I don't know how to discover that TeamViewer has lost its remote access capability (this is: the capability to be remotely accessed and controlled).
Tested until now:
Check TeamViewer process and/or daemon. Not valid: they keep working even after disconnected.
NICs review. Not valid: TeamViewer seems not to add any.
See the TeamViewer's main window. Not programmatically valid or easy to implement.
How can I programmatically know if TeamViewer has disconnected?
I don't know if this method differs between platforms, but at least I would like to know about a solution for some Linux shell. Bash if possible.

Probably I'm late, but run into the same problem and found a possible solution. I'm using teamviewer 12.
I noticed that, in my case sometimes some GUI related process are not launched so the machine is not online in my computer and contact list, if I ssh it and check for the list of teamviewer processes using:
ps -ef | grep [t]eamviewer
I get just one process, the teamviewer daemon:
root 1808 1 0 09:22 ? 00:00:53 /opt/teamviewer/tv_bin/teamviewerd -d
But, when everything is fine I have:
root 1808 1 0 09:22 ? 00:00:53 /opt/teamviewer/tv_bin/teamviewerd -d
rocco 10975 8713 0 09:31 ? 00:00:58 /opt/teamviewer/tv_bin/wine/bin/wineserver
rocco 11064 10859 0 09:31 ? 00:00:33 /opt/teamviewer//tv_bin/TVGuiSlave.64 31 1
rocco 11065 10859 0 09:31 ? 00:00:28 /opt/teamviewer//tv_bin/TVGuiDelegate 31 1
So simply counting the number of process works for me..
#!/bin/bash
online() {
## Test connection
ping -c1 www.google.com > /dev/null
return $?
}
online
if (test $? -eq 0)
then
network=$(ps -ef | grep [t]eamviewer | wc -l)
if (test $network -gt 3)
then
echo Machine online, teamviewer connected
else
echo Machine online, teamviewer not connected, trying restart daemon
sudo teamviewer --daemon restart
fi
fi

Have you considered trapping the signal(if possible) and executing a function that will restart TeamViewer.
Start it from a script and trap an exit signal
function restartTV {
# re-start TeamViewrt
sudo /etc/init.d/something start
}
trap finish EXIT # or appropriate signal
sudo /etc/init.d/something stop
# Do the work...

Related

A bash shell program to wait for all servers in a list to start, then run startup scripts

I'm trying to automate the startup (after reboot) of an application that runs on a grid of 12 Linux 7 servers.
The 12 servers get rebooted in random order.
All the servers need to be running before I can start up the application that resides on the 12 servers.
What I'd like to do is to test that all 12 Linux 7 servers are up and then when all 12 are up, I'd proceed with the startup sequence of commands.
All 12 server are set up with ssh keys.
cat serverlist.txt
10.0.0.6
10.0.0.7
10.0.0.8
10.0.0.9
...
10.0.0.18
I want to ping a server and then wait until the ping is successful, then move to the next IP address.
My apologies for my question.
How to code this?
Read in the first line from IP list file.
Ping first IP until success, then ping the other IP addresses (one at a time) until the success of all 12.
Then, run commands to start the application on the grid of 12 servers.
Question: How to code this in the bash shell.
The inner loop can be as simple as
while ! ssh "${connection_string}" -o ConnectTimeout=5 true
do
sleep 0.5
done
This runs a trivial command, and waits for 0.5 seconds between retries.
for i in `cat /home/Startup/serverlist.txt`
do
ssh ${i} -o ConnectTimeout=5 true
while test $? -gt 0
do
ssh ${i} -o ConnectTimeout=5 true
done
done
exit
If ssh works the server is alive, plus the use of the timeout is a better way to speed up the script execution. If the script get finish that means all the servers are up and responding.

Bash poweroff script hangs system

My intention is to cycle through my list of ips and poweroff if my ping succeeds first. However the systems seems to hang. After running this script I can't ping the systems anymore and they aren't powered off. If I run ssh 192.168.1.ip "sudo poweroff" through terminal I dont encounter this issue. Any advice?
for ((ip=40, cnt=0; ip<=max; ip++, cnt++))
do
if ping -c 1 192.168.1.$ip &> /dev/null
then
printf "\n${array[$cnt]}: Ping Successful"
ssh 192.168.1.$ip "sudo poweroff"
printf "\n${array[$cnt]}: Power Down Executed\n"
sleep 1
else
printf "\n${array[$cnt]}: Ping Failed\n"
fi
done
After running a single ssh 192.168.1.40 "sudo poweroff" the system is properly powered off. When running my script, I am unable to ping the systems however I can visually see the fans and leds are still on. I think I should use a KVM to take a closer look since ssh doesn't allow allow connection after this script is run. Still at first glance I dont understand how running ssh 192.168.1.40 "sudo poweroff" and running it through my script really makes a difference. Anyways I'll try to add more information tomorrow.
ssh 192.168.1.$ip "(sleep 5; sudo poweroff)&" to put the process in the background on the remote host and sleep for 5 seconds before powering off to give time for the script to complete and exit the remote host before it goes down... – David C. Rankin
This Resolved my Issue.

Dovecot/Amavis restart script (work in progress)

We have a mail server that is dying and in the process of having accounts migrated to a new server before decommissioning. With 800+ email accounts across 25+ domains, it is important for this machine to stay up until migration is finished.
Lately it has started to fill up with error logs, which freeze mysql because of no space, stop mail flow, and generally give me a headache. Until the root problem of the errors can be found and fixed, I have come up with a script to check if Dovecot and Amavis-new are running, and if not restarts them.
After reading:
https://stackoverflow.com/a/7096003/4820993
As well as a few other common examples, I came up with this.
netstat -an|grep -ce ':993.*LISTEN' >/dev/null 2>&1
if [ $? = 0 ]
then
echo 'Dovecot is up';
else
echo 'Dovecot is down, restarting...';
/etc/init.d/dovecot restart
logger -p mail.info dovecot_keepalive: Dovecot is down, restarting...
fi
/etc/init.d/amavis status |grep -ce 'running' >/dev/null 2>&1
if [ $? = 0 ]
then
echo 'AmavisD is up';
else
echo 'AmavisD is down, restarting...';
/etc/init.d/amavis restart
sleep 2
/etc/init.d/amavis status |grep -ce 'running' >/dev/null 2>&1
if [ $? = 1 ]
then
echo 'AmavisD had a problem restarting, trying to fix it now...';
logger -p mail.info amavis_keepalive: AmavisD had a problem restarting...
output=$(ps aux|grep a\[m\]avisd)
set -- $output
pid=$2
kill $pid
rm /var/run/amavis/amavisd.pid
/etc/init.d/amavis start
else
echo 'AmavisD restarted successfully';
logger -p mail.info amavis_keepalive: AmavisD is down, restarting...
fi
fi
Who knows, I'm probably making it harder that it is, and if so PLEASE LET ME KNOW!!!
I checked it against http://www.shellcheck.net and updated/corrected according to it's debug reports. I am piecing this together from examples elsewhere and would love someone to proofread this before I implement it.
The first part checking dovecot is already working just fine as a cronjob every 6 hours (yes the server is that messed up that we need to check it), it's the section about amavis I'm not sure about.
You can use Monit which will monitor your services and restart itself.
Amavisd:
# File: /etc/monit.d/amavisd
# amavis
check process amavisd with pidfile /var/amavis/amavisd.pid
group services
start program = "/etc/init.d/amavisd start"
stop program = "/etc/init.d/amavisd stop"
if failed port 10024 then restart
if 5 restarts within 5 cycles then timeout
Dovecot:
# File: /etc/monit.d/dovecot
check process dovecot with pidfile /var/run/dovecot/master.pid
start program = "/etc/init.d/dovecot start"
stop program = "/etc/init.d/dovecot stop"
group mail
if failed host localhost port 993 type tcpssl sslauto protocol imap then restart
if failed host localhost port 143 protocol imap then restart
if 5 restarts within 5 cycles then timeout
depends dovecot_init
depends dovecot_bin
check file dovecot_init with path /etc/init.d/dovecot
group mail
check file dovecot_bin with path /usr/sbin/dovecot
group mail

A script to test for traffic from a streaming server?

I have an instrument that streams data out a tcp port, and I'd like to use standard tools in a script to determine if the stream is available.
Manually I use ncat, which promptly exits if the data stream isn't available.
Here's my initial bash script:
#!/bin/bash
ncat somehost 1234 >/dev/null &
pid=$!
sleep 1
if [ -d /proc/$pid/ ]; then
kill -KILL $pid
echo "It's alive, ALIVE\!"
# Launch clients
else
echo "He's dead, Jim."
# Perform resurrection
fi
It works, but I'm wondering if there is a simpler or better way to accomplish this that doesn't rely on job control or procfs or even ncat. I'd also like to know how much data was sent before a timeout exires.
And, yes, in bash ncat host port can be replaced by cat </dev/tcp/host/port, but I'd also like to avoid bash-isms (so it can work under busybox).
Another approach would be to use wc to count the lines/chars output by ncat, since it outputs only one line and exits if the connection can't be made. But I can't wait forever for termination if the stream is up, so I'd need to use something like timeout, which has its own complications when trying to access the output of a command.
Is there a "plain" approach that works with minimal dependencies?
Or should I write a simple tool instead? It would return the number of bytes read if a connection was made to host/port, a negative errno otherwise, and would support a wait time and protocol spec (tcp/udp). A return value of 0 would mean the connection was made, but no data arrived before the wait time expired (a good thing to know).
Or maybe patch ncat to do the above?
I use netcat in FreeBSD, which has a -z option that simply checks whether a port is open. This eliminates the background and sleep you're using in your script.
-z Specifies that nc should just scan for listening daemons, without
sending any data to them.
The option exists in netcat on an old Ubuntu box here, so that might be an option for you.
$ nc -z somehost 1234 && echo "It's alive, ALIVE!"
I don't see an equivalent option in ncat. You may be able to compile netcat into busybox; it's much smaller than ncat, at least on my systems:
Linux:
$ ls -lH `which nc ncat`
-rwxr-xr-x 1 root root 31296 2010-02-21 01:32 /bin/nc
-rwxr-xr-x 1 root root 130448 2009-11-06 04:39 /usr/bin/ncat
FreeBSD:
ls -l `which nc ncat`
-r-xr-xr-x 1 root wheel 28112 Jan 15 14:53 /usr/bin/nc
-rwxr-xr-x 1 root wheel 182775 Mar 19 2012 /usr/local/bin/ncat
Of course, this doesn't help you check the amount of traffic that has come through the stream, or to analyse its content. For that, I think your solution is innovative and reasonable, though the challenge you've presented may not be resolvable easily.
You can use ps to avoid the procfs dependency, and you can store things in a temp file for analysis rather. Note that busybox should include a mktemp, but you should probably check the options. I haven't tested this:
#!/bin/sh
TMPFILE=`mktemp /tmp/str.XXXX`
trap "rm -f $TMPFILE" 0 1 2 3 15
nc somehost 1234 > $TMPFILE &
pid=$!
sleep 1
if ps $pid >/dev/null; then
kill -KILL $pid
echo -n "It's alive, "
if [ -s $TMPFILE ]; then
echo "ALIVE!"
else
echo "but unresponsive."
fi
else
echo "He's dead, Jim."
fi
That's all I've got. And it's not much different from what you already have.
Another option might be to build a custom tool that you might be able to compile into your own busybox, but that'll be a c question rather than a shell question. :-)

How to check if X server is running?

Is there any way to find out if the current session user is running an Xserver (under Linux) ?
I've started off with things like:
ps -e | grep X
but this doesn't work always
and one more thing I tried is checking the $DISPLAY variable
Are there any other ways to check this?
EDIT:
Some people suggested using the $DISPLAY variables but what if the user fiddles with this variable ? what if he tries to do something and changes this variable and then when I check it, it no longer reflects an accurate state of the system.
Is there no specific way to do this that will always return a correct answer ?
I found that it can be done programmatically thus:
#include <X11/Xlib.h>
int main()
{ exit(XOpenDisplay(NULL) ? 0 : 1); }
$ gcc -o xprobe xprobe.c -L/usr/X11R6/lib -lX11
But I am looking for a script way.
I often need to run an X command on a server that is running many X servers, so the ps based answers do not work. Naturally, $DISPLAY has to be set appropriately. To check that that is valid, use xset q in some fragment like:
if ! xset q &>/dev/null; then
echo "No X server at \$DISPLAY [$DISPLAY]" >&2
exit 1
fi
EDIT
Some people find that xset can pause for a annoying amount of time before deciding that $DISPLAY is not pointing at a valid X server (often when tcp/ip is the transport). The fix of course is to use timeout to keep the pause amenable, 1 second say.
if ! timeout 1s xset q &>/dev/null; then
⋮
$DISPLAY is the standard way. That's how users communicate with programs about which X server to use, if any.
One trick I use to tell if X is running is:
telnet 127.0.0.1 6000
If it connects, you have an X server running and its accepting inbound TCP connections (not usually the default these days)....
I use
pidof X && echo "yup X server is running"
pgrep and $DISPLAY are other options.
Other considerations:
su then $DISPLAY will not be set. Things that change the environment of the program running can make this not work.
I don't recommand ps -e | grep X as this will find procX, which is not the X server.
You can use xdpyinfo (can be installed via apt-get install x11-utils).
xprop -root &> /dev/null
...is my tried & true command to test for an "X-able" situation. And, it's pretty much guaranteed to be on any system running X, of course, the command fails if not found anyways, so even if it doesnt exist, you can pretty much assume there is no X either. (thats why I use &> instead of >)
I wrote xdpyprobe program which is intended for this purpose. Unlike xset, xdpyinfo and other general tools, it does not do any extra actions (just checks X server and exits) and it may not produce any output (if "-q" option is specified).
The bash script solution:
if ! xset q &>/dev/null; then
echo "No X server at \$DISPLAY [$DISPLAY]" >&2
exit 1
fi
Doesn't work if you login from another console (Ctrl+Alt+F?) or ssh. For me this solution works in my Archlinux:
#!/bin/sh
ps aux|grep -v grep|grep "/usr/lib/Xorg"
EXITSTATUS=$?
if [ $EXITSTATUS -eq 0 ]; then
echo "X server running"
exit 1
fi
You can change /usr/lib/Xorg for only Xorg or the proper command on your system.
1)
# netstat -lp|grep -i x
tcp 0 0 *:x11 *:* LISTEN 2937/X
tcp6 0 0 [::]:x11 [::]:* LISTEN 2937/X
Active UNIX domain sockets (only servers)
unix 2 [ ACC ] STREAM LISTENING 8940 2937/X #/tmp/.X11-unix/X0
unix 2 [ ACC ] STREAM LISTENING 8941 2937/X /tmp/.X11-unix/X0
#
2) nmap
# nmap localhost|grep -i x
6000/tcp open X11
#
First you need to ensure foundational X11 packages are correctly installed on your server:
rpm -qa | grep xorg-x11-xauth
If not then, kindly install all packages :
sudo yum install xorg-x11-xauth xterm
Ensure that openssh server is configured to forward x11 connections :
edit file : vim /etc/ssh/sshd_config
X11Forwarding yes
NOTE : If that line is preceded by a comment (#) or is set to no, update the file to match the above, and restart your ssh server daemon (be careful here — if you made an error you may lock yourself out of the server)
sudo /etc/init.d/sshd restart
Now, configure SSH application to forward X11 requests :
ssh -Y your_username#your_server.your_domain.com
if [[ $DISPLAY ]]; then
…
fi
This is PHP script for checking.
$xsession = `pidof X`;
if (!$xsession) {
echo "There is no active X session, aborting..\n";
exit;
}
Similar command can be used in shell script too. like the pidof command.

Resources