LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices



Reply
 
Search this Thread
Old 09-27-2004, 02:16 PM   #1
nistur
Member
 
Registered: Sep 2004
Location: UK
Distribution: Fedora Core 5/Arch Linux
Posts: 42

Rep: Reputation: 15
Smile Help needed learning to write a shell script for logons


Hi,
I am a comparative Newbie to Linux, I have technically had a copy of Mandrake for nearly a year, but havent had much chance to 'play around' with it till about a month ago. I have tried my best to learn as much as I can as quick as I can. Anyway, enough of this basic background... My problem:

I have recently set up my PC as a server for me to access it from other machines, I have also allowed a few of my friends to access it as I do not need all the space at the moment, and also in an attempt to make it more secure...

I have downloaded a script that uses web based sms to send messages straight from the shell. I would like to use this to send myself a message when someone logs in, telling me who, and from what address. I have been told this is possible, and would need to write a shell script.

Please could someone point me in the right direction at least.

Thanks
Nistur
 
Old 09-27-2004, 07:47 PM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
It depends a little on how people are logging in. For example, if they are using SSH to log in, ssh sets a variable called SSH_CLIENT that you could use to send yourself the hostname. You could add some commands to /etc/profile to test for this and send yourself the text message. For example:

Code:
if [ "$SSH_CLIENT" ]; then
  sendsms "Login from $SSH_CLIENT by user $USER"
else
  sendsms "Login on tty `tty` by user $USER"
fi
You could also wrap the whole thing in a check for your own username, so you don't get messages about your own login.
Note also that SSH_CLIENT includes two port numbers.
 
Old 09-28-2004, 11:00 AM   #3
nistur
Member
 
Registered: Sep 2004
Location: UK
Distribution: Fedora Core 5/Arch Linux
Posts: 42

Original Poster
Rep: Reputation: 15
thanks for the info

Thanks for your explanation of how I can get it to work, just a few more questions though on the same subject ((as I am more or less a total newbie these might seem obvious to you, but please go soft on me, I am trying my best)) Most of the people that log on use telnet ((I know, I know)) but at the moment, there is no data on my PC that needs to actually be protected, as soon as there is something that I might actually mind being read by other people, I will not force them to use SSH. Is there anything I can do about that? ((not making them change to ssh, just how to get the same to work with telnet))

Also, you mentioned changing /etc/profile I am guessing it doesn't matter where I put ther code, as long as it isn't inside any more code, and before the 'done', right?

And now, perhaps for the most obvious... what do I have to do to get this running? Do I have to have one shell running root? do I need to do anything? I am sorry, I know this will be incredibly obvious for you

Thanks again

Nistur
 
Old 09-28-2004, 06:37 PM   #4
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
I would like to say I know how you could make it work with telnet, but I haven't used it in so long! I don't even think I have a telnet daemon I could test with. One way you can find out is to log in at the console, and do this:

set > console

then login via telnet, and do:

set > telnet

then compare the two files (diff is good for this). Look for variables that are set when logged in via telnet that are not normally, and see if any of those have the information you need.

Re /etc/profile... 'done' usually marks the end of a loop, like a while or for loop. It's not used to mark the end of the script, so you could put these commands at the very end of the file if you like.

To get it running - /etc/profile is run directly by bash when a user logs in. That means that the script will execute with that user's permissions, within their own shell. Basically, you don't have to do anything to make it work You do need to make sure that your friends have permission to run your SMS script though.

Oh, and no need to apologise for not knowing something - we all start with zero knowledge
 
Old 09-29-2004, 04:49 PM   #5
nistur
Member
 
Registered: Sep 2004
Location: UK
Distribution: Fedora Core 5/Arch Linux
Posts: 42

Original Poster
Rep: Reputation: 15
Thanks again for all the help... I'm nearly there, just a few more problems ((Why is nothing ever straight forward with me? Something always goes wrong)) I did what you said and did a diff on the files and it came out with the following:

$diff telnet console
7c7
< COLUMNS=80
---
> COLUMNS=100
44c44
< LINES=25
---
> LINES=37
58c58
< PPID=11322
---
> PPID=2821
69c69
< TERM=ansi
---
> TERM=linux

From what I can tell, none of this helps in any way, except possibly the TERM statement, but how, I dont really know.
Thanks again for all the help
Nistur
 
Old 09-29-2004, 06:04 PM   #6
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
OK, looks like it's going to be tricky to tell a telnet session from other sessions (in theory, anything could use TERM=ansi). I would encourage you to set up SSH and get your friends using that (Cygwin or PuTTY on windows), but if you're still determined to go through with the telnet thing, I think you could edit your /etc/inetd.conf and find the telnet line... change the part that launches telnetd to read "env TELNET=YES /path/to/telnetd", then do a "killall -HUP inetd" and try your telnet connection again. Once logged in, use "echo $TELNET" to verify that the variable is set. I don't know how to get the remote IP though - this is one thing that using SSH would just fix
 
Old 09-30-2004, 09:09 AM   #7
nistur
Member
 
Registered: Sep 2004
Location: UK
Distribution: Fedora Core 5/Arch Linux
Posts: 42

Original Poster
Rep: Reputation: 15
Thanks a lot, I will try this as soon as I can. I actually still need to set up the sms program to a server as the only one I am subscribed to at the moment isn't compatible with linuxsms. But I will set this up, is there anyway I can set it up to send a standard output on my computer, just to test it? I have asked everyone using it to switch to ssh, but it would be useful, for now at least to have the telnet anyway.

Thanks, hopefully I'm getting there

Nistur
 
Old 09-30-2004, 09:31 AM   #8
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
For things like this, I like to send the output to tty12. This stops the output from bugging you in the middle of a task, and you can check it whenever with ctrl-alt-f12. This also avoids the tricky problem of figuring out which terminal you're currently looking at - I'm not actually sure how you do this.

instead of sendsms "message", just use [code[echo "message" > /dev/tty12[/code]
Of course, you have to make sure all the users have write access to that device - easy fix is chmod o+w /dev/tty12, lengthier but probably safer fix is chgrp users /dev/tty12, chmod g+w /dev/tty12, and add all legal users to the users group.
 
Old 10-02-2004, 03:34 AM   #9
nistur
Member
 
Registered: Sep 2004
Location: UK
Distribution: Fedora Core 5/Arch Linux
Posts: 42

Original Poster
Rep: Reputation: 15
Thanks, I got most of that working :S but for some reason, it wont let me do an if statement around the code saying that if its me logging in, to ignore it. This must be one of the most simple pieces of code, and I cant do it. I'm guessing I got the syntax wrong. Also, the SSH works fine, it tells be who and where from someone logs in, but telnet and tty connections dont seem to recognise the $USER. I also can't find inetd.conf to set the telnet variable. it doesnt seem to be in /etc/

I will sort this out at some point
I'm sorry if I'm being a huge pain

Thanks again
Nistur
 
  


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
HELP:write a CGI program with shell script supermyself Programming 3 06-12-2005 04:10 PM
write a shell script to send email yenonn Programming 4 02-11-2005 11:06 PM
learning how to write shell ashley75 Linux - General 3 05-07-2004 03:05 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 04:00 PM
learning Shell script ???? Atrocity Programming 3 02-02-2003 02:49 PM


All times are GMT -5. The time now is 04:10 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration