Script to verify SSH is running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to verify SSH is running
# 1  
Old 06-16-2007
Script to verify SSH is running

Writing a simple test script that looks for ssh, kills if its running and verifies if its still running. If it isn't, move on. My issue, its cause I don't know how, is to verify if ssh is running still. Also, is there a way have this do this on a remote server? I already have the ssh keys working so all I need is to know how to code that part. below is what I have, help is much appreciated!

#!/sh/bin
check_stat=`ps -ef | grep sshd | grep -v grep | awk '{print $2}'`
if [ "${check_stat}X" != "X" ]
then
echo "SSHD is running"
else
echo "SSHD isn't running"
for pid in $check_stat
do
echo "Killing $pid"
kill -9 $pid
done
fi
# 2  
Old 06-16-2007
Quote:
Originally Posted by djinn
Writing a simple test script that looks for ssh, kills if its running and verifies if its still running. If it isn't, move on. My issue, its cause I don't know how, is to verify if ssh is running still. Also, is there a way have this do this on a remote server?

If it is not running, you will not be able to ssh into the remote machine.
Quote:
I already have the ssh keys working so all I need is to know how to code that part. below is what I have, help is much appreciated!
#!/sh/bin
check_stat=`ps -ef | grep sshd | grep -v grep | awk '{print $2}'`
if [ "${check_stat}X" != "X" ]
then
echo "SSHD is running"
else
echo "SSHD isn't running"
for pid in $check_stat
do
echo "Killing $pid"
kill -9 $pid
done
fi

Why do you kill it if it isn't running?

Code:
check_stat=`ps -ef | grep '[s]shd' | awk '{print $2}'`
if [ -n "$check_stat" ]
then
   echo "SSHD is running"
   echo "Killing $check_stat"
   kill -9 $check_stat
else
   echo "SSHD isn't running"
fi

# 3  
Old 06-16-2007
one way
Code:
while [ 1=1 ];
do
    pgrep sshd
    if [ $? -eq 1 ];then
      echo "sshd stopped"
      break
    fi
    echo "Still running"    
    pkill sshd
    sleep 2
done

# 4  
Old 06-16-2007
Quote:
Originally Posted by cfajohnson

If it is not running, you will not be able to ssh into the remote machine.

Why do you kill it if it isn't running?

Code:
check_stat=`ps -ef | grep '[s]shd' | awk '{print $2}'`
if [ -n "$check_stat" ]
then
   echo "SSHD is running"
   echo "Killing $check_stat"
   kill -9 $check_stat
else
   echo "SSHD isn't running"
fi

This script is meant for an application, I'm only using SSHD as an example but you brought up a good point. I'll switch the sshd to ftpd and go from there.
# 5  
Old 06-16-2007
So the new script is:
#!/sh/bin
check_stat=`ps -ef | grep vsftpd | grep -v grep | awk '{print $2}'`
if [ "${check_stat}X" != "X" ]
then
echo "vsftpd is running"
else
echo "vsftpd isn't running"
for pid in $check_stat
do
echo "Killing $pid"
kill -9 $pid
done
fi
# 6  
Old 06-16-2007
'#!/bin/sh' != '#!/sh/bin'
# 7  
Old 06-16-2007
Quote:
Originally Posted by djinn
So the new script is:
#!/sh/bin
check_stat=`ps -ef | grep vsftpd | grep -v grep | awk '{print $2}'`
if [ "${check_stat}X" != "X" ]
then
echo "vsftpd is running"
else
echo "vsftpd isn't running"
for pid in $check_stat
do
echo "Killing $pid"
kill -9 $pid
done
fi

There are a few problems with it:
Incorrect shebang

Unnecessary 2nd grep

Overly verbose test expression

Killing processes in the wrong section of the if command

Unnecessary for loop
Code:
#!/bin/sh
check_stat=`ps -ef | grep '[v]sftpd' | awk '{print $2}'`
if [ -n "$check_stat" ]
then
  echo "vsftpd is running"
  echo "Killing $check_stat"
  kill -9 $check_stat
done
else
  echo "vsftpd isn't running"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with running a script via ssh

Hi, I'm trying to run a user defined shell script with options and arguments via ssh but getting error as ksh: Script.sh: not found. Here is what i'm running: ssh -t username@server 'cd /path/to/script; script.sh -t start here '-t' with script.sh, is an user defined option and 'start' is also... (3 Replies)
Discussion started by: xsam
3 Replies

2. Shell Programming and Scripting

Running Script at a location with SSH

Hi All I have created a script which is running properly "Script". I want that script to run when I login into another server. in the code below: when ssh is executed it asks for password ..After entering password the user is logged in but the script does not run.... whereas when I exit... (1 Reply)
Discussion started by: pulkitbahl
1 Replies

3. UNIX for Dummies Questions & Answers

Script still running after ssh

I have the lines below on my script: script.ksh: case `hostname` in some_host) ssh server1A "/home/script.ksh $1 $2" ssh server1B "/home/script.ksh $1 $2" ssh server1C "/home/script.ksh $1 $2" ssh server1D "/home/script.ksh $1 $2" ssh... (1 Reply)
Discussion started by: erin00
1 Replies

4. Shell Programming and Scripting

Running Local Script from SSH with SUDO

Hello, I know for SSH'ing and running a local script is... ssh -t user@servername < /path/to/localscript.sh and with SSH'ing and SUDO'ing is... ssh -t user@servername "sudo -u username ls -l /home/username" My inquiry is how can I combine both, by SSH'ing and SUDO'ing but running... (4 Replies)
Discussion started by: WPGPoseidon
4 Replies

5. Shell Programming and Scripting

ssh does not work in script while running crontab

Hi All, I have prepared a small script to monitor few applications running on diff unix boxes(frontend/backed node1/node2 etc). ssh does not work for node2 when script executed from crontab..:wall: it work fine when i run it manually. Regards, Pavan (4 Replies)
Discussion started by: pavanchouksey
4 Replies

6. Shell Programming and Scripting

Running script and commands through SSH

Hello All, I am trying to run some simulations through SSH from my mac on our university SOLARIS system. My problem is that whenever I want to execute a command I get an error which says "Invalid argument". Maybe I should explain more what I want to do and what I did. Firstly I installed a... (10 Replies)
Discussion started by: Apfik
10 Replies

7. Shell Programming and Scripting

SSH Connection drops - but does my script keep running?

Hello there. I'm fairly new to Linux, but I am connecting via SSH and PuTTY to a remote server, and I am running a fairly heavy MySQL script in a PHP page. Our connection here is dodgy to say the least and I get continuous disconnections. My question is, when I get disconnected, does my... (4 Replies)
Discussion started by: christatedavies
4 Replies

8. Shell Programming and Scripting

Running a function on a remote server via SSH in a script

I'm working on a script (mostly for practice) to simplify a task I have to do every now and then. I have a cluster with 6 servers on it, each server has a directory with a set of files called *.pid and *.mpid. Each file contains the pid of a process that may or may not be running on that server.... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

9. Cybersecurity

Running script through SSH as root

Hi all, I have a situation where I have a shell script that I need to run remotely on multiple *nix machines via SSH. Unfortunately, some of the commands in it require root access. I know that best practices for ssh entail configuring it so that the root account cannot log in, you need to... (4 Replies)
Discussion started by: irinotecan
4 Replies

10. Shell Programming and Scripting

running script in cron - with ssh commands - not working

I ran an ssh command to run a script on a remote server ssh -l <user> <servername> /path/to/script/scriptname This works fine - and the script is executed correctly. However - I put this command into a script, that I want to run from cron every hour, to execute the file on the remote... (31 Replies)
Discussion started by: frustrated1
31 Replies
Login or Register to Ask a Question