View Single Post
Old July 3rd, 2008   #11
LinuX-M@n1@k
Gee! These Aren't Roasted!
 
Join Date: Jan 2008
Location: Yambol, Bulgaria
Beans: 213
Ubuntu 8.04 Hardy Heron
Send a message via ICQ to LinuX-M@n1@k Send a message via AIM to LinuX-M@n1@k Send a message via MSN to LinuX-M@n1@k Send a message via Yahoo to LinuX-M@n1@k Send a message via Skype™ to LinuX-M@n1@k
Re: I'm a noob learning python... need help!

Here it is!!! My first Python script!
PHP Code:
#!/usr/bin/env python
#
# This script will ping to google.bg and if network is
# unreachable it will connect to the modem and reboot it.

import os
import commands
import pexpect


def adsl_reboot
():
    
pexpect.spawn('telnet 192.168.1.1')

    
p.expect('Login user: ')
    
p.sendline('root')    # Sending Username.

    
p.expect('Password: ')
    
p.sendline('GSrootaccess')    # Sending Password.

    
p.expect('> ')
    
p.sendline('reboot')    # Sending command to the shell.

    
os.system('zenity --notification --text="The modem has been rebooted."')
                
# Show an icon in the notification area


result commands.getoutput("ping -c 1 google.bg")
if 
result.find("Unreachable") == -1:
    
result True
    
print 'Connected!'
else:
    
result False
    
print 'Not connected! - Rebooting the modem.'

if result == False:
    
adsl_reboot()


#
# This was my first script.
# Author: Boris Bolgradov
#
# Big thanks to: walkerk, days_of_ruin and Psykotik!
# http://ubuntuforums.org 
http://ubuntuforums.org/showthread.php?t=847218
http://www.noah.org/wiki/Pexpect#Description_of_Pexpect

Edit: I fixed it. Thanks LaRoza.
Attached Files
File Type: py .adsl-reboot.py (993 Bytes, 29 views)
File Type: sh .adsl-reboot.sh (88 Bytes, 23 views)

Last edited by LinuX-M@n1@k; July 4th, 2008 at 07:22 AM..
LinuX-M@n1@k is offline   Reply With Quote