You are not logged in.

#1 2007-12-14 21:53:32

axion419
Member
Registered: 2007-04-12
Posts: 185

Teaching Myself Python. How Can I Stop The Program From Closing :)

I wrote a program today that will log into a router here at work and check if the line protocols are up and if i can see thir mac address.  If i run it from the IDLE program it works fine.  When i run it as just crosnet.py it runs fine but closes before i can see the output.  I imagine there is some simple line that will have run and just sit?  Or even better, loop back to the beginning and wait for me to give it a new number to then telnet with.

-------
#import getpass
import sys
import telnetlib

HOST = "111.111.111.111"
password = "password"
vpc = raw_input("Enter The VPC Number: ")
#password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

#tn.read_until("login: ")
#tn.write(user + "\n")
#if password:
tn.read_until("Password: ")
tn.write(password + "\n")

#tn.write("sh int atm6/0.vpc\n")
tn.write("sh int atm6/0." + str(vpc) + "\n")
tn.write("sh arp | in " + str(vpc) + "\n")
#tn.write("exit\n")

print tn.read_all()
-----------------------------------------
Sorry the code is bad, dont know python and i was shown a outline of how to telnet and adjusted it to my needs.

Offline

#2 2007-12-14 22:02:24

buttons
Member
From: NJ, USA
Registered: 2007-08-04
Posts: 620

Re: Teaching Myself Python. How Can I Stop The Program From Closing :)

while True:

and just indent everything else under it smile

Ends when you press ^C.

if you wish to have a way to break out of the loop, say, when you press enter at the VPC number without entering anything, add the following after your vpc input:

if not vpc:
    break

which will break out of the while loop and end your program.

Last edited by buttons (2007-12-14 22:03:06)


Cthulhu For President!

Offline

#3 2007-12-14 22:21:35

axion419
Member
Registered: 2007-04-12
Posts: 185

Re: Teaching Myself Python. How Can I Stop The Program From Closing :)

In Linux how to I save it and make it excutable?
its in my home directory and i did a chmod +x crosnet.py
then opened another terminal and ran ./crosnet.py and it said
[justin@myhost ~]$ ./crosnet.py
./crosnet.py: line 1: import: command not found
./crosnet.py: line 2: import: command not found
./crosnet.py: line 6: syntax error near unexpected token `('
./crosnet.py: line 6: `    vpc = raw_input("Enter The VPC Number: ")'


any ideas?

Last edited by axion419 (2007-12-14 22:34:18)

Offline

#4 2007-12-14 22:28:15

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Teaching Myself Python. How Can I Stop The Program From Closing :)

oh. do you mean the terminal window closed when you clicked on the file to run it from python?

Try running it from the command prompt, and see if it behaves differently.
using a .pyw extension changes behavior in windows as well. It wont launch a terminal at all.
a .py extension does launch a terminal, but closes once the program finishes (you wont be able to read the output).

You could add a stanza at the end (after the print statement) to "hit any key to close", and read for any key.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#5 2007-12-14 22:43:40

axion419
Member
Registered: 2007-04-12
Posts: 185

Re: Teaching Myself Python. How Can I Stop The Program From Closing :)

ok it works on windows as a .py, now i wish it would work in linux lol

in terminal i type
chmod +x crosnet.py
then type ./crosnet.py and i get the errors from below.
----

Offline

#6 2007-12-14 22:47:01

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: Teaching Myself Python. How Can I Stop The Program From Closing :)

axion, is bash running the script? By omitting a shebang /bin/sh runs executable text files (right?)
You have to make the first line '#! /usr/bin/python'.

Offline

#7 2007-12-14 22:49:34

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,694
Website

Re: Teaching Myself Python. How Can I Stop The Program From Closing :)

in linux you type "python programname.py" and that's it

Offline

#8 2007-12-14 23:16:26

axion419
Member
Registered: 2007-04-12
Posts: 185

Re: Teaching Myself Python. How Can I Stop The Program From Closing :)

ok, i can run it now smile python crosnet.py worked perfectly.

"You could add a stanza at the end (after the print statement) to "hit any key to close", and read for any key."

That would be great, if it would run, end the telnet, load again for next use.

how would I go about that?



You all have been a great help.

Offline

Board footer

Powered by FluxBB