LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2009, 04:50 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,033

Rep: Reputation: 69
I need some help writing a bash script...


I'm starting to like making bash scripts. It's kewl making creative ones. Right now I'm trying to make a bash script that will open up port 23 for a netcat connection. Once there's a connection, I'd like for the script to open up xmms and play a sound effect, as well as echo a txt file to the desktop saying that a connection was made at this specific time. When I execute the script, it stops exection at the first line. So far I have this:

sudo nc -lvnp 23
while [ 1 ]
do
if $(netstat -antp | grep 23) == 'ESTABLISHED'
do xmms /home/user/sound.mp3 && echo "Connection made at $(date | cut -d " " -f4) > /home/user/Desktop/connection.txt
exit
fi
done

Last edited by trist007; 04-14-2009 at 04:51 PM.
 
Old 04-14-2009, 05:03 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
the nc command will run in the foreground, so for the most basic level of managing it, you'd need to stick an & on the end to push it into the background.
 
Old 04-14-2009, 10:15 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,033

Original Poster
Rep: Reputation: 69
So far I have this. Anybody got any pointers?

Code:
nc -lvnp 23&
x=1
T1="STABLISH"
while [ $x -eq 1 ]
do
        if [ $(netstat -antp | grep 23 | cut -d "E" -f2) = "$T1" ]
        then
                xmms /home/user/sound.mp3
                x+=1
        else
                x=1
        fi
done
 
Old 04-14-2009, 11:15 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,526

Rep: Reputation: 1821Reputation: 1821Reputation: 1821Reputation: 1821Reputation: 1821Reputation: 1821Reputation: 1821Reputation: 1821Reputation: 1821Reputation: 1821Reputation: 1821
You don't need the x=1 command in the loop: if you don't set it, it will stay 1. So you could simplify to
Code:
nc -lvnp 23&
x=1
T1="STABLISH"
while [ $x -eq 1 ]
do
        if [ $(netstat -antp | grep 23 | cut -d "E" -f2) = "$T1" ]
        then
                xmms /home/user/sound.mp3
                x+=1
        fi
done
Doing x+=1 seems like an obfuscated way to end a loop, I have to read the entire loop to realize that you meant to end the loop rather keep some sort of count. You can use the break command instead (and make the x variable redundant):
Code:
nc -lvnp 23&
T1="STABLISH"
while true
do
        if [ $(netstat -antp | grep 23 | cut -d "E" -f2) = "$T1" ]
        then
                xmms /home/user/sound.mp3
                break
        fi
done
I think the grep statement could be a problem if some other program was running on port 23000 (for instance).

But maybe the -c option of nc is what you really want here:
Quote:
-c string specify shell commands to exec after connect (use with caution). The string is passed to /bin/sh -c for execution.
Code:
nc -lvnp 23 -c 'xmms /home/user/sound.mp3'
 
Old 04-14-2009, 11:27 PM   #5
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,033

Original Poster
Rep: Reputation: 69
the nc -c option would work but it opens on client end instead of the server end.
 
Old 04-14-2009, 11:50 PM   #6
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,033

Original Poster
Rep: Reputation: 69
Sweet, got it!!

Code:
nc -lvnp 23&
if [ -e /root/status.txt ]
        then
        echo > /root/status.txt
        else
        touch /root/status.txt
fi
while [ -z $(cat /root/status.txt) ]
        do
        sleep 1
        netstat -antp | grep nc | grep ESTABLISHED > /root/status.txt
done
echo "connection established at" > /root/Desktop/conn.txt
netstat -antp | grep nc | grep ESTABLISHED | cut -d ":" -f2 | cut -d " " -f9 >> /root/Desktop/conn.txt
date | cut -d " " -f4 >> /root/Desktop/conn.txt
xmms /usr/share/sounds/voice-incoming-transmission.wav&
rm /root/status.txt
sleep 2
killall xmms

Last edited by trist007; 04-15-2009 at 01:09 AM.
 
Old 04-15-2009, 01:12 AM   #7
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,033

Original Poster
Rep: Reputation: 69
I placed my latest revision to the shell script in the previous post. Everything works perfectly, except that after the shell script has ended after 'killall xmms' I am unable to participate in the chat. I can however, see the input from the person on the client side, but anything I enter on the server side just returns bad command.

How do I rebind to the nc -lvnp 23& process after the last line 'killall xmms'? In other words, I need to take the nc -lvnp 23& process back from the background and bring it to the foreground so I can interact with it.

Last edited by trist007; 04-15-2009 at 01:14 AM.
 
Old 04-15-2009, 10:18 AM   #8
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,033

Original Poster
Rep: Reputation: 69
And even further refined...

Code:
nc -lvnp 23 &
while [ -z $(echo $T1) ]
        do
        sleep 1
        export T1="$(netstat -antp | grep nc | grep ESTABLISHED)"
done
echo "connection established at" > /root/Desktop/conn.txt
netstat -antp | grep nc | grep ESTABLISHED | cut -d ":" -f2 | cut -d " " -f9 >> /root/Desktop/conn.txt
date | cut -d " " -f4 >> /root/Desktop/conn.txt
xmms /usr/share/sounds/voice-incoming-transmission.wav &
sleep 2
killall xmms
fg %1
I'm trying to foreground the nc -lvnp 23 command, but the kernel just returns no job control. I've tried entering the jobs command near the end of the script and it shows the backgrounded nc process running as 1. However, once the script ends, it's like that backgrounded process gets removed from jobs, yet the backgrounded process still appears in the ps -A output.

Last edited by trist007; 04-15-2009 at 11:14 AM.
 
Old 04-15-2009, 11:31 AM   #9
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,033

Original Poster
Rep: Reputation: 69
Got it. So when a bash script runs, it runs with it's own environmental variables, which is why I had to set job control with 'set -m'. Also, I understand that once a shell script ends, whatever environmental variables are in there get lost, which is why I had to include the 'while true' statement at the bottom to keep the shell script running so that I can participate in the chat sessions. Otherwise, the backgrounded nc process would get erased from the jobs panel. Sick shell script. I'm getting better.

Code:
set -m
nc -lvnp 23 &
while [ -z $(echo $T1) ]
        do
        sleep 1
        export T1="$(netstat -antp | grep nc | grep ESTABLISHED)"
done
echo "connection established at" > /root/Desktop/conn.txt
netstat -antp | grep nc | grep ESTABLISHED | cut -d ":" -f2 | cut -d " " -f9 >> /root/Desktop/conn.txt
date | cut -d " " -f4 >> /root/Desktop/conn.txt
xmms /usr/share/sounds/voice-incoming-transmission.wav &
sleep 2
killall xmms
fg
while true
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help writing a bash script stympman Linux - Newbie 2 10-18-2008 08:55 PM
looking for help in writing a bash script onepostonly Linux - General 3 10-05-2008 07:42 PM
Help Me>> Need help in writing Bash script lamak_98 Programming 6 10-04-2007 11:44 AM
writing a bash script poiuytrewq Linux - Newbie 2 10-07-2004 11:26 PM
writing bash script ankitgdit Programming 4 08-19-2003 07:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration