Help needed learning to write a shell script for logons
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Help needed learning to write a shell script for logons
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.
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:
if [ "$SSH_CLIENT" ]; then
sendsms "Login from $SSH_CLIENT by user $USER"
sendsms "Login on tty `tty` by user $USER"
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.
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
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
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:
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
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.
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.
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