PDA

View Full Version : help with writing script



bob58
10-04-2004, 03:00 AM
I need to wite a very small simple script and save it to desktop to run when i need to. It is an TELNET session that runs in the Konsole. At the prompt: bob@Gateway:~$ I type in "telnet", without quotes, then "open" and then "n7us.net" So it looks like this:

bob@Gateway:~$ telnet <enter>
telnet> open n7us.net <enter>

Then it connects to the ham radio site where I can sign in. I need to know how to make a script that would do this instead of me having to type it in each time. I need the commands to:

1. bring up the Konsole
2. have it put in the command "telnet"
3. have it put in the command open n7us.net
4. have it enter/carraige return

then i can take control from there.......any ideas???? bob

firebyrd10
10-04-2004, 03:15 AM
I need to wite a very small simple script and save it to desktop to run when i need to. It is an TELNET session that runs in the Konsole. At the prompt: bob@Gateway:~$ I type in "telnet", without quotes, then "open" and then "n7us.net" So it looks like this:

bob@Gateway:~$ telnet <enter>
telnet> open n7us.net <enter>

Then it connects to the ham radio site where I can sign in. I need to know how to make a script that would do this instead of me having to type it in each time. I need the commands to:

1. bring up the Konsole
2. have it put in the command "telnet"
3. have it put in the command open n7us.net
4. have it enter/carraige return

then i can take control from there.......any ideas???? bob

Try makeing a script called telconnect.sh (You can use a standard txt file)
Open it and put in


telnet n7us.net


Then open up a consol type
su
chmod a+x /directory/telconnect.sh

then just type /directory/telconnect.sh (or ./telconnect.sh if your in the directory.)

*edited, fixed it

champagnemojo
10-04-2004, 03:25 AM
Couldn't you just create an icon in your KDE panel? Make sure that it's set to run in terminal (or however KDE calls it). Then the command would just be "telnet n7us.net". I think that would work anyways.

Or if you really want a script you would just need to create a file with:


#!/bin/sh
konsole -e telnet n7us.net

You could call it hamtelnet.sh or whatever. Make sure that you chmod +x on it and it should work.

champagnemojo
10-04-2004, 03:28 AM
Oops...sorry firebyrd...I got distracted with something and didn't see your post before I responded. :D

shah
10-04-2004, 03:39 AM
#!/bin/sh
konsole -e telnet n7us.net
No need for konsole arg. if you going to run it in konsole unless you want to open new konsole.

THe above command is suitable if you want to create link on your desktop but you need to remove the sh extension first. Make sure you change it to executable.

:D :D

bob58
10-04-2004, 12:02 PM
Hello gentlemen...thank you for your help! I noticed in my KDE menu is another program called Telnet-ssl. I can save a stewp by using that, because when it is started, unlike the regular Konsole, the prompt is already at the TELNET point. Would i be able to substitute the word telnet in the below example with telnet-ssl?


#!/bin/sh
konsole -e telnet n7us.net

I want to try learning scripts but I still am not too sure about how to go about it. OK I will try this example when I get home and post my results....thank you...bob

bob58
10-04-2004, 12:05 PM
Wow its too early in the morning! My last post had an error: What I would like to know, is in the below example:

Code:
#!/bin/sh
konsole -e telnet n7us.net


Can i take out the word Konsole and put in telnet-ssl? Telnet-ssl is another Konsole-type mode that, when started, is already at the telnet prompt....I am wondering if this will save me a step or two. I am very familiar with doing DOS batch files, but Linux scripts are very confusing! I would like to learn because knowing scripts would be a benefit. thanks...bob

shah
10-04-2004, 12:20 PM
#!/bin/sh
konsole -e telnet-ssl n7us.net

Don't think you can remove konsole because telnet run from konsole :D :D

shah
10-04-2004, 12:23 PM
If you want a gui version of telnet-ssh , try Putty. Just apt-get.
:D :D

bob58
10-04-2004, 01:00 PM
Hello again....Yah I see what you mean about the Konsole and Telnet-ssl being one in the same! I did make the script and it runs great! :D

Thank you for the tip.....I do have another question....and I might be biting off alot more than I can chew with this one......considering my knowledge of shell scripts at the moment!

I would like to be able to create a MENU with a few simple Knoppix programs that will run in the terminal. I like making batch files in DOS and I would like to see if I can do the same thing using Linux. I have an older computer that would be perfect for this...to run in command line only.

If you are familiar with how to go about making a menu in dos using errorlevels and ANSI graphics.....I can do that very easily, but does anyone know how to code in linux to do something similar that will gun in a GUI - less environment? I can see now how to make a simple shell script and make it executable....but I have no clue where to start to creat a menu screen, using a box around it maybe with 4 or 5 menu choices in it....then when you pick. say the first choice on menu which is labeled "A" for example....the script finds the label and runs the program...then when you exit program, you are taken back to menu!

I know this may sound crazy but this is goodtherapy for me to keep the ol brain working! bob58

bob58
10-04-2004, 03:51 PM
I have an idea to further improve this script.....

#!/bin/sh
konsole -e telnet n7us.net

This script assumes there is a valid internet connection

I ran the script with the network cable pulled out of side of computer....it brings up the Konsole, but just sits there with blank screen.......

Just to make the script nicer, is there a way to test for network connection, if there is, the script just runs as normal. But if there is no connection, the script just exits, and that way, you dont have a blank Konsole screen staring at you. Not that its a big deal, but I think this would be a nicer script.

I am reading up on linux shell scripts but its hard to find the right command for the job. Would you use an if command? Not sure of syntax......bob

Durand Hicks
10-05-2004, 05:36 AM
You could use an if/then/else pair in your script. For example:



if eth0 up
then exec telnet-ssl session
else exit


This isn't actual code but just a layout so you get the idea for error-proofing the script. The else statement catches anything that doesn't apply to the if statement, otherwise it would trigger the telnet-ssl session and exit when the session is done.


HTH,

Durand

bob58
10-05-2004, 03:18 PM
Hello Durand......I will try what you suggested. The script i am usingis below:

#!/bin/sh
konsole -e telnet n7us.ne

But where would i insert this coding? :



if eth0 up
then exec telnet-ssl session
else exit

Would I put it right after the last line in my script? I posted a separate message about getting scripting help for different things. I have also been reading the help sites on web but cannot really find what I want. I want to understand the code you suggested and learn what each line does. I will try this and post backlater my results....bob

Markus
10-05-2004, 06:59 PM
Found something on the net for you:


#!/bin/sh
NET=eth0

if ! ifconfig | grep $NET >/dev/null 2>&1 ||\
! ifconfig $NET | grep UP >/dev/null 2>&1; then
echo "Get a net!"
exit 1
fi
konsole -e telnet n7us.ne
exit 0

Durand Hicks
10-05-2004, 07:31 PM
Try this block as your script:


#!/bin/sh
if [-e /etc/network/ifstate ]
then konsole -e telnet
else exit
exit
fi


That should be all it should take. Upon perusing the other scripts with this type of block, it seems that you must end an if statement with fi. Note I inserted two exits, one to catch if the eth0 connection isn't up, and the other to terminate the script once the telnet session is done. This way you wouldn't have to create two scripts when one like this would kill two birds with one stone.

HTH,

Durand

bob58
10-05-2004, 11:36 PM
thanks Duran and Markus......I will try the scripts later tonite and see what happens.....a few questions tho.....for Markus...that coding sure looks complicated! How would I go about deciphering it? I would l;ike to know what the script line by line does if possible. And Durand, in your script i didnt see a reference to the "n7us.net" that I connect with. OK i will try out ad post back tomorrow....thank you..bob

Markus
10-06-2004, 08:25 AM
Hehe, As I said I found it on the net and modified just a bit, just can't get used to not declaring a variable before using it which you don't seem to have to do in bash.
Basically it looks for the word eth0 in ifconfig and directs the stderr output to the same filedescriptor as stdout, don't ask me how: http://en.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html#ss3.1
If it can't find eth0 it echos a message and exits, if it finds it you get a connection and it exits afterwards.

Markus
10-06-2004, 09:17 AM
Hmm, Durands approach seems a bit easier, how about:

#!/bin/sh
NET=$(grep eth /etc/network/ifstate)

if [ -n "$NET" ]
then
konsole -e telnet n7us.ne
exit 1
else
echo "Get a net!"
exit 0
fi If eth0 is found in /etc/network/ifstate NET gets the value eth0=eth0, then -n tests if the variable NET is not empty and proceeds with telnet, else echoes a message.
Hmm again. His block is still shorter :( :wink:

user unknown
10-07-2004, 02:11 AM
A different approach would be to look if n7us.net is availabĺe:


ping -c1 n7us.net >/dev/null 2>&1 && echo success
or

ping -c1 n7us.net >/dev/null 2>&1 || exit 1

eth0 may be up in the local net.
may be up, but no dns-server found ...

-c1 will only ping one time.
>/dev/null sends the output to 'nowhere'
2>&1 sends errormessage to the location, where the output goes ('nowhere' too)
&& (read: AND) will execute the following code only if the first command succeeded. Here you may call a script-function, if your command is too complex.

|| (read: OR [exclusive]) will execute the following command (exit 1) only, if the previous failed.
So your code for continuing when everything is fine will just follow.

In general, an excellent scripting guide is found here:
http://freshmeat.net/projects/advancedbashscriptingguide/

Do you want to run the script totally automated?
If you want to login, where you have to wait for a login-prompt, you have to use 'expect' - which isn't installed on my system - why?

user unknown
10-07-2004, 02:20 AM
(sorry - doubled 'submit'-hit)

bob58
10-12-2004, 12:34 PM
Hello 'USER UNKNOWN'.....I will try that script....unfortunatly the others i have tried didnt do as expected. I do not need the script to run totally automated...I dont mind logging in, that no problem. I just dont understand why none of the suggestions work! It seems simple enuff to run a script like this. Maybe its me.Hahahaha OK well anyway I will try this latest script when i get home and post back tomorrow....bob

Durand Hicks
10-12-2004, 07:50 PM
Bob

The script I wrote does work, but the server you were trying to login with either doesn't exist or is on a private server, so I left the n7us.net off to get the resulting telnet prompt. All you would have to do is type in the telnet server name you wanted to log into, and the script does work once you've made it an executable and created a shortcut linking to the executable script.

Edit:
Whoops, I copied and pasted the code I posted, and it lacked a space before the -e, so you'll need to insert it or I can repost the modification to this thread if you like. :)

Edit #2:
I typed open n7us.net in the telnet prompt window, and it responded with a call: prompt. If you want to insert the n7us.net server into the script, that will work as well.

Durand

bob58
10-13-2004, 05:51 PM
Hi Durand.....OK I will try again. Repost the modified script if you have time and I will try it again.

Markus
10-13-2004, 08:17 PM
I actually tested both my scripts with an ssh connection and they did work, don't know why they didn't for you.

Durand Hicks
10-13-2004, 10:08 PM
Bob,

Not a problem, here it is.



#!/bin/sh
if [ -e /etc/network/ifstate ]
then konsole -e telnet n7us.net
else exit
exit
fi


I tested this twice already just so you know it works. One possible problem that comes to mind when it didn't work for you is that maybe you're on dialup? Don't forget to chmod a+x this script and make a link on your desktop to the script.

HTH,

Durand

P.S. On a side note, I have the telnet-ssl program, not the standard telnet program, so if that makes a difference, let me know.