piebju
Posts: 6
Joined: Thu Aug 02, 2012 12:05 pm

Re: RPi IP Emailer

Thu Aug 02, 2012 12:19 pm

Hello everyone!
when i try to run this script, i get several problems, fist it was the problem with the missing LSB Tags, solved that by adding this to the script

Code: Select all

### BEGIN INIT INFO
# Provides: Plesk Firewall
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# X-Start-Before:
# X-Stop-After:
# Default-Start:
# Default-Stop:
# X-Interactive: true
# Short-Description: Plesk Firewall (IP forwarding)
# Description:
### END INIT INFO
Now when i reboot the pi i get:
ImportError: no module named SMTP

This comes up in the boot informaiton, so it tries to run the script.

Using this version of the script:

Code: Select all

#!/usr/bin/python
# The idea behind this script is if plugging a RaspberryPi into a foreign network whilst running it headless
# (i.e. without a monitor/TV), you need to know what the IP address is to SSH into it.
#
# This script emails you the IP address if it detects an ethernet address other than it's usual address
# that it normally has, i.e. on your home network.

import smtp, string

### BEGIN INIT INFO
# Provides: check_ip.py
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# X-Start-Before:
# X-Stop-After:
# Default-Start:
# Default-Stop:
# X-Interactive: true
# Short-Description: check the ip and email it.
# Description:
### END INIT INFO


################################################
###### Remember to set all constants here ######
################################################
FIXED_IP = '192.168.0.8'
IP_FILEPATH = '/home/pi/temp/current_ip.txt'
SMTP_USERNAME = '**********@gmail.com'
SMTP_PASSWORD = 'password'
SMTP_RECIPIENT = '**********@gmail.com'
SMTP_SERVER = 'smtp.gmail.com'
SSL_PORT = 465
################################################

ipaddr_string = 'ip -4 addr > ' + IP_FILEPATH
subprocess.call(ipaddr_string, shell=True)
inet_string = ''

ip_file = file(IP_FILEPATH, 'r')
for line in ip_file:

    if 'eth0:' in line:
        inet_line = ip_file.next()
        _time = time.asctime()
        inet_string = inet_line[9:(inet_line.index('/'))]

        if inet_string != FIXED_IP:
            SUBJECT = 'IP Address from Raspberry Pi at: %s' % time.asctime()
            TO = SMTP_RECIPIENT
            FROM = SMTP_USERNAME
            text = 'The IP address is: %s' % inet_string
            BODY = string.join((
                'From: %s' % FROM,
                'To: %s' % TO,
                'Subject: %s' % SUBJECT,
                '',
                text
                ), '\r\n')
            server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT)
            server.login(SMTP_USERNAME, SMTP_PASSWORD)
            server.sendmail(FROM, [TO], BODY)
            server.quit()
            print '[' + '\033[36;1m' + 'info' + '\033[0m' + ']' + ' Emailing eth0 IP address ' + inet_string + 

' to ' + TO + ' from ' + FROM

        elif inet_string == FIXED_IP:
            print '[' + '\033[32;1m' + ' ok ' + '\033[0m' + ']' + ' Normal IP address' + inet_string + 'found'

    else:
        print '[' + '\033[31;1m' + 'warning' + '\033[0m' + ']' + ' eth0 not found in file!'


ip_file.close()


Anyone know how to fix the problem?

KCarscadden
Posts: 56
Joined: Sat Jun 09, 2012 9:00 pm
Location: Canada

Re: RPi IP Emailer

Tue Aug 07, 2012 1:20 am

Change the import line from:

import smtp, string
to
import smtplib, string, subprocess, time

should fix the import problems.

The message about the tags was a warning only, and the script will work without adding the tags.

User avatar
dliloch
Posts: 168
Joined: Wed Jun 27, 2012 6:28 pm
Location: cleveland, ohio usa

Re: RPi IP Emailer

Sun Sep 09, 2012 11:33 am

I found this python script very useful.. I can now run my pi headless .. but reboot and
shutdown caused the extraneous message so I added this code ..
p = subprocess.Popen(["runlevel"], stdout=subprocess.PIPE)
out, err=p.communicate()
print out
print out[2]
if out[2] == '0':
print 'Halt detected'
exit(0)
if out [2] == '6':
print 'Shutdown detected'
exit(0)
FeersumEndjinn wrote::o

That is rather strange! The init.d folder is the initialization/boot script folder, so I've never known it do that before!

cd into the /etc/init.d folder, and run:

Code: Select all

$  sudo update-rc.d check_ip.py defaults 19
The dollar sign obviously being the prompt (don't type that bit :D )

Also, type:

Code: Select all

$  sudo nano /etc/init.d/check_ip.py
And change the 'print' line to say something a little more obvious - you can comment out the current line with a # at the beginning if you don't want to permanently change the script - try doing that, and immediately below (and indented the same) type:

Code: Select all

print '++++++++++++++++ MELON MELON MELON ++++++++++++++++'
Then before you type startx, look for this line above the login prompt - if you see it, it is emailing at the correct point.

5 points to anyone who can tell me where the above line comes from ;)

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: RPi IP Emailer

Sun Sep 09, 2012 11:47 am

Just out of curiosity, how is this approach better than just getting on the router and looking up the "connected devices table" (and/or the "DHCP client table") on the router?

It would seem to me that the one case where something like this could be useful would be to get the *external* IP address of the home LAN, so that we can access the Pi remotely. But that is a different kettle of fish entirely...
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: RPi IP Emailer

Sun Sep 09, 2012 11:56 am

One advantage is that you can read the email on any device. This is much easier on a Smartphone than messing with router settings. Also the email could be modified to include other information about the Pi (disc space etc).
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

User avatar
FeersumEndjinn
Posts: 148
Joined: Mon Nov 21, 2011 4:02 pm

Re: RPi IP Emailer

Mon Sep 10, 2012 5:33 pm

This is exactly it - my brother was at uni and had no direct access to his router as it was owned and controlled by another student, hence writing this little script.
Morrolan / FeersumEndjinn

"And the lord God said unto John, 'Come forth and receive eternal life', but John came fifth and won a toaster."

User avatar
pmj1979
Posts: 17
Joined: Tue Sep 04, 2012 12:06 pm

Re: RPi IP Emailer

Wed Sep 19, 2012 3:42 pm

Thanks for this script - Very useful! :)

Is there a way to check and email the IP address if the Pi is headless running a WiFi connection?
Each of the three wifi dongles I've tried connect and I can SSH if I know the IP but at the moment I still have to check the IP depending on where I am or If I tether through my phone.

ps, my script also emails once at boot and again at shutdown or reboot. I can see the additions, Dliloch made to his script but I dont want to start cutting and pasting random code like I did with this ip script without knowing what the code is, im not learning anything that way.
Pi from Element14 (still not got the first Pi from RS :( )
Rev2 board | Belkin 7Port powered hub | ASUS N10 WiFi | Logitech Wired Mouse | Behavior Tech Wired KB

User avatar
FeersumEndjinn
Posts: 148
Joined: Mon Nov 21, 2011 4:02 pm

Re: RPi IP Emailer

Wed Sep 19, 2012 4:00 pm

I haven't seen Diloch's changes - I'll add them if they're useful!

Yes there is a way of using this under wifi - if you type 'ifconfig' at the command line and tell me what adapters are identified (i.e. lo, eth0, wlan0 etc) and I can modify the script so you can choose which adapter you wish to check the IP for :)

Because of the way the script runs under init.d, it will run at first boot and again every time it reboots, as a reboot is nothing more than a boot! Unfortunately I don't know how to change this as I don't know if there is a way to tell the difference between a cold-boot and a reboot.

Also, if you could point me to Diloch's changes, I'll implement them in the script and update this thread with the new script.

Cheers,
FeersuEndjinn
Morrolan / FeersumEndjinn

"And the lord God said unto John, 'Come forth and receive eternal life', but John came fifth and won a toaster."

User avatar
pmj1979
Posts: 17
Joined: Tue Sep 04, 2012 12:06 pm

Re: RPi IP Emailer

Wed Sep 19, 2012 4:07 pm

Sorry, when I said boot and reboot I meant it emails on boot (as it should), then again as its going down, then again when it comes back up (as it should).

Depending on which of the three devices I use I have wlan0 wlan1 or wlan2, some people might have more or less I suppose.

This is straight from the thread...
I found this python script very useful.. I can now run my pi headless .. but reboot and
shutdown caused the extraneous message so I added this code ..
p = subprocess.Popen(["runlevel"], stdout=subprocess.PIPE)
out, err=p.communicate()
print out
print out[2]
if out[2] == '0':
print 'Halt detected'
exit(0)
if out [2] == '6':
print 'Shutdown detected'
exit(0)
Pi from Element14 (still not got the first Pi from RS :( )
Rev2 board | Belkin 7Port powered hub | ASUS N10 WiFi | Logitech Wired Mouse | Behavior Tech Wired KB

User avatar
FeersumEndjinn
Posts: 148
Joined: Mon Nov 21, 2011 4:02 pm

Re: RPi IP Emailer

Wed Sep 19, 2012 7:11 pm

I've updated the script. The gist can be found here: https://gist.github.com/2987424.

However, please note that I have NOT tested this, as I am working away at the moment and don't have access to my Pi. However, Diloch's code seems straightforward enough.

pmj1979 - I've added an extra variable for you - you can see under the ##### block, a line that says:

Code: Select all

adapter = 'eth0:'
If you change the string within the quotes to 'wlan0:' etc, it will change which adapter it reports the IP address for :)

Let me know how you get on with it.

FeersumEndjinn
Morrolan / FeersumEndjinn

"And the lord God said unto John, 'Come forth and receive eternal life', but John came fifth and won a toaster."

User avatar
pmj1979
Posts: 17
Joined: Tue Sep 04, 2012 12:06 pm

Re: RPi IP Emailer

Wed Sep 19, 2012 7:30 pm

Thanks, I feel special now :) Ill let you know how it works out.
Any reason why it couldn't report multiple addresses if wifi and hard wired?


Reading under "Improving WiFi config" here >
http://www.raspberrypi.org/phpBB3/viewt ... 8&p=176847

...it mentions disabling "persistent renaming of wlan interfaces by changing:" so this sounds like regardless of which adapter is used or which port or hub port the wifi dongle is in, it should still be picked up as wlan0 (?).

The script still emails when the Pi is going up and down but Its not harming anything.
Pi from Element14 (still not got the first Pi from RS :( )
Rev2 board | Belkin 7Port powered hub | ASUS N10 WiFi | Logitech Wired Mouse | Behavior Tech Wired KB

User avatar
FeersumEndjinn
Posts: 148
Joined: Mon Nov 21, 2011 4:02 pm

Re: RPi IP Emailer

Wed Sep 19, 2012 8:09 pm

I'm sorry, I placed Diloch's code AFTER the email had been sent - now corrected it so now it shouldn't email twice.

You are correct in that if you plug 1 WIFI adapter in, it should ALWAYS be wlan0, regardless of which port or which adapter it is.

Cheers,
FeersumEndjinn
Morrolan / FeersumEndjinn

"And the lord God said unto John, 'Come forth and receive eternal life', but John came fifth and won a toaster."

User avatar
pmj1979
Posts: 17
Joined: Tue Sep 04, 2012 12:06 pm

Re: RPi IP Emailer

Wed Sep 19, 2012 10:47 pm

I have only just got my wifi working again after upgrading earlier so I haven't tried the new script yet.

Looking at why my wifi was still listed as wlan2 even after doing this
* A final tweak you might like is to edit
/lib/udev/rules.d/75-persistent-net-generator.rules and disable persistent
renaming of wlan interfaces by changing:

CODE: SELECT ALL
# device name whitelist
KERNEL!="eth*|ath*|wlan*[0-9]|msh*|ra*|sta*|ctc*|lcs*|hsi*", GOTO="persistent_net_generator_end"


to:
CODE: SELECT ALL
# device name whitelist
KERNEL!="ath*|msh*|ra*|sta*|ctc*|lcs*|hsi*", \
GOTO="persistent_net_generator_end"
I found this site. http://fixunix.com/ubuntu/508804-make-wlan1-wlan0.html
The second post points to a file listing all connected devices (of a certain type (?) ) and this is where I was able to rename my wlan2 back to 0. If I change the adapter or buy a new one, this will create a new entry with a new name so the script would fail?
Pi from Element14 (still not got the first Pi from RS :( )
Rev2 board | Belkin 7Port powered hub | ASUS N10 WiFi | Logitech Wired Mouse | Behavior Tech Wired KB

User avatar
pmj1979
Posts: 17
Joined: Tue Sep 04, 2012 12:06 pm

Re: RPi IP Emailer

Wed Sep 19, 2012 11:30 pm

Changing

Code: Select all

adapter = 'eth0:'
to

Code: Select all

 adapter = 'wlan0:'
the script does send the IP address of the wireless adapter. :)

The script does not email now shutdown/reboot (as ts going down), only when the Pi is powering on or coming back up.

Even after getting the email with the IP address, I still get two [warning] messages "not found in file". :?


Thanks very much! :)

Peter
Pi from Element14 (still not got the first Pi from RS :( )
Rev2 board | Belkin 7Port powered hub | ASUS N10 WiFi | Logitech Wired Mouse | Behavior Tech Wired KB

wh1p
Posts: 29
Joined: Tue Jul 03, 2012 11:00 pm
Location: South East UK
Contact: Website

Re: RPi IP Emailer

Thu Sep 20, 2012 12:42 am

This is a really good idea except my pi is for the time being a server but if i ever decide to find another use for it or get a second one this will be part of my usual debian build from now on :)

User avatar
FeersumEndjinn
Posts: 148
Joined: Mon Nov 21, 2011 4:02 pm

Re: RPi IP Emailer

Thu Sep 20, 2012 6:26 am

pmj1979 wrote:Changing

Code: Select all

adapter = 'eth0:'
to

Code: Select all

 adapter = 'wlan0:'
the script does send the IP address of the wireless adapter. :)

The script does not email now shutdown/reboot (as ts going down), only when the Pi is powering on or coming back up.

Even after getting the email with the IP address, I still get two [warning] messages "not found in file". :?


Thanks very much! :)


Peter
Peter, are you able to post output from the dmesg command so that I can see what these warnings look like please? :)

FersumEndjinn
Morrolan / FeersumEndjinn

"And the lord God said unto John, 'Come forth and receive eternal life', but John came fifth and won a toaster."

User avatar
pmj1979
Posts: 17
Joined: Tue Sep 04, 2012 12:06 pm

Re: RPi IP Emailer

Thu Sep 20, 2012 9:58 am

Below is what dmesg spits out but the warning message is straight out of the script. "[warning] eth0 not found in file" (i tweaked mine to say adapter instead of eth0) so the warning message is not listed below.

The message shows here http://youtu.be/rzEYBvDu8E4?t=1m45s


pi@raspberrypi ~ $ dmesg
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Linux version 3.2.27+ (dc4@dc4-arm-01) (gcc version 4.7.2 20120731 (prerelease) (crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08) ) #160 PREEMPT Mon Sep 17 23:18:42 BST 2012
[ 0.000000] CPU: ARMv6-compatible processor [410fb767] revision 7 (ARMv7), cr=00c5387d
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
[ 0.000000] Machine: BCM2708
[ 0.000000] Memory policy: ECC disabled, Data cache writeback
[ 0.000000] On node 0 totalpages: 61440
[ 0.000000] free_area_init_node: node 0, pgdat c050d508, node_mem_map c05b5000
[ 0.000000] Normal zone: 480 pages used for memmap
[ 0.000000] Normal zone: 0 pages reserved
[ 0.000000] Normal zone: 60960 pages, LIFO batch:15
[ 0.000000] pcpu-alloc: s0 r0 d32768 u32768 alloc=1*32768
[ 0.000000] pcpu-alloc: [0] 0
[ 0.000000] Built 1 zonelists in Zone order, mobility grouping on. Total pages: 60960
[ 0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=1920 bcm2708_fb.fbheight=1080 bcm2708.boardrev=0x4 bcm2708.serial=0x7c224460 smsc95xx.macaddr=B8:27:EB:22:44:60 dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
[ 0.000000] PID hash table entries: 1024 (order: 0, 4096 bytes)
[ 0.000000] Dentry cache hash table entries: 32768 (order: 5, 131072 bytes)
[ 0.000000] Inode-cache hash table entries: 16384 (order: 4, 65536 bytes)
[ 0.000000] Memory: 240MB = 240MB total
[ 0.000000] Memory: 237748k/237748k available, 8012k reserved, 0K highmem
[ 0.000000] Virtual kernel memory layout:
[ 0.000000] vector : 0xffff0000 - 0xffff1000 ( 4 kB)
[ 0.000000] fixmap : 0xfff00000 - 0xfffe0000 ( 896 kB)
[ 0.000000] vmalloc : 0xcf800000 - 0xe8000000 ( 392 MB)
[ 0.000000] lowmem : 0xc0000000 - 0xcf000000 ( 240 MB)
[ 0.000000] modules : 0xbf000000 - 0xc0000000 ( 16 MB)
[ 0.000000] .text : 0xc0008000 - 0xc04c1ec8 (4840 kB)
[ 0.000000] .init : 0xc04c2000 - 0xc04e1930 ( 127 kB)
[ 0.000000] .data : 0xc04e2000 - 0xc050dba0 ( 175 kB)
[ 0.000000] .bss : 0xc050dbc4 - 0xc05b4ae8 ( 668 kB)
[ 0.000000] NR_IRQS:330
[ 0.000000] sched_clock: 32 bits at 1000kHz, resolution 1000ns, wraps every 4294967ms
[ 0.000000] timer_set_mode: unhandled mode:1
[ 0.000000] timer_set_mode: unhandled mode:3
[ 0.000000] Console: colour dummy device 80x30
[ 0.000000] console [tty1] enabled
[ 0.000943] Calibrating delay loop... 697.95 BogoMIPS (lpj=3489792)
[ 0.060156] pid_max: default: 32768 minimum: 301
[ 0.060631] Mount-cache hash table entries: 512
[ 0.061122] Initializing cgroup subsys cpuacct
[ 0.061215] Initializing cgroup subsys devices
[ 0.061252] Initializing cgroup subsys freezer
[ 0.061283] Initializing cgroup subsys blkio
[ 0.061425] CPU: Testing write buffer coherency: ok
[ 0.061773] hw perfevents: enabled with v6 PMU driver, 3 counters available
[ 0.062986] devtmpfs: initialized
[ 0.073168] NET: Registered protocol family 16
[ 0.074853] vc_mem_connected_init: called
[ 0.074936] bcm2708_vcio: bcm_mailbox_property failed (-19)
[ 0.074980] vc_mem_update: success=-19 resp 0, vcbase=0 vcsize=0 armbase=0 armsize=0
[ 0.075026] vc-mem: mm_vc_mem_phys_addr = 0x00000000
[ 0.075056] vc-mem: mm_vc_mem_size = 0x00000000 (0 MiB)
[ 0.075538] hw-breakpoint: found 6 breakpoint and 1 watchpoint registers.
[ 0.075593] hw-breakpoint: maximum watchpoint size is 4 bytes.
[ 0.075631] mailbox: Broadcom VideoCore Mailbox driver
[ 0.075726] bcm2708_vcio: mailbox at f200b880
[ 0.075815] bcm_power: Broadcom power driver
[ 0.075854] bcm_power_open() -> 0
[ 0.075880] bcm_power_request(0, 8)
[ 0.576563] bcm_mailbox_read -> 00000080, 0
[ 0.576604] bcm_power_request -> 0
[ 0.576630] Serial: AMBA PL011 UART driver
[ 0.576787] dev:f1: ttyAMA0 at MMIO 0x20201000 (irq = 83) is a PL011 rev3
[ 0.902937] console [ttyAMA0] enabled
[ 0.927764] bio: create slab <bio-0> at 0
[ 0.932941] SCSI subsystem initialized
[ 0.937038] usbcore: registered new interface driver usbfs
[ 0.942660] usbcore: registered new interface driver hub
[ 0.948288] usbcore: registered new device driver usb
[ 0.954219] Switching to clocksource stc
[ 0.958575] FS-Cache: Loaded
[ 0.961755] CacheFiles: Loaded
[ 0.977984] NET: Registered protocol family 2
[ 0.982906] IP route cache hash table entries: 2048 (order: 1, 8192 bytes)
[ 0.990979] TCP established hash table entries: 8192 (order: 4, 65536 bytes)
[ 0.998500] TCP bind hash table entries: 8192 (order: 3, 32768 bytes)
[ 1.005144] TCP: Hash tables configured (established 8192 bind 8192)
[ 1.011555] TCP reno registered
[ 1.014721] UDP hash table entries: 256 (order: 0, 4096 bytes)
[ 1.020633] UDP-Lite hash table entries: 256 (order: 0, 4096 bytes)
[ 1.027409] NET: Registered protocol family 1
[ 1.032603] RPC: Registered named UNIX socket transport module.
[ 1.038736] RPC: Registered udp transport module.
[ 1.043460] RPC: Registered tcp transport module.
[ 1.048174] RPC: Registered tcp NFSv4.1 backchannel transport module.
[ 1.054929] bcm2708_dma: DMA manager at cf808000
[ 1.059714] bcm2708_gpio: bcm2708_gpio_probe c04e9c18
[ 1.065163] vc-mem: Videocore memory driver
[ 1.070183] audit: initializing netlink socket (disabled)
[ 1.075668] type=2000 audit(0.740:1): initialized
[ 1.193122] VFS: Disk quotas dquot_6.5.2
[ 1.197269] Dquot-cache hash table entries: 1024 (order 0, 4096 bytes)
[ 1.204854] FS-Cache: Netfs 'nfs' registered for caching
[ 1.211156] msgmni has been set to 464
[ 1.216937] io scheduler noop registered
[ 1.221049] io scheduler deadline registered (default)
[ 1.226357] io scheduler cfq registered
[ 1.274824] Console: switching to colour frame buffer device 240x67
[ 1.309913] kgdb: Registered I/O driver kgdboc.
[ 1.324245] brd: module loaded
[ 1.332467] loop: module loaded
[ 1.336548] vcos: [1]: vchiq_init_state: slot_zero = 0xffd80000, is_master = 0
[ 1.344289] vcos: [1]: vchiq_init_state: called
[ 1.349796] vcos: [1]: vchiq: initialised - version 2 (min 2), device 252.0
[ 1.357641] usbcore: registered new interface driver smsc95xx
[ 1.363695] cdc_ncm: 04-Aug-2011
[ 1.367140] usbcore: registered new interface driver cdc_ncm
[ 1.373003] dwc_otg: version 3.00a 10-AUG-2012 (platform bus)
[ 1.579132] Core Release: 2.80a
[ 1.582406] Setting default values for core params
[ 1.587349] Finished setting default values for core params
[ 1.793294] Using Buffer DMA mode
[ 1.796721] Periodic Transfer Interrupt Enhancement - disabled
[ 1.802738] Multiprocessor Interrupt Enhancement - disabled
[ 1.808490] OTG VER PARAM: 0, OTG VER FLAG: 0
[ 1.812970] Dedicated Tx FIFOs mode
[ 1.816758] dwc_otg: Microframe scheduler enabled
[ 1.816810] dwc_otg bcm2708_usb: DWC OTG Controller
[ 1.821951] dwc_otg bcm2708_usb: new USB bus registered, assigned bus number 1
[ 1.829550] dwc_otg bcm2708_usb: irq 32, io mem 0x00000000
[ 1.835232] Init: Port Power? op_state=1
[ 1.839309] Init: Power Port (0)
[ 1.842736] usb usb1: New USB device found, idVendor=1d6b, idProduct=0002
[ 1.849763] usb usb1: New USB device strings: Mfr=3, Product=2, SerialNumber=1
[ 1.857170] usb usb1: Product: DWC OTG Controller
[ 1.862033] usb usb1: Manufacturer: Linux 3.2.27+ dwc_otg_hcd
[ 1.867936] usb usb1: SerialNumber: bcm2708_usb
[ 1.873488] hub 1-0:1.0: USB hub found
[ 1.877395] hub 1-0:1.0: 1 port detected
[ 1.882142] dwc_otg: FIQ enabled
[ 1.882159] dwc_otg: NAK holdoff enabled
[ 1.882180] Module dwc_common_port init
[ 1.882534] usbcore: registered new interface driver uas
[ 1.888006] Initializing USB Mass Storage driver...
[ 1.900428] usbcore: registered new interface driver usb-storage
[ 1.913777] USB Mass Storage support registered.
[ 1.926021] usbcore: registered new interface driver libusual
[ 1.939569] mousedev: PS/2 mouse device common for all mice
[ 1.953446] bcm2835-cpufreq: min=700000 max=1000000 cur=700000
[ 1.959434] bcm2835-cpufreq: switching to governor powersave
[ 1.972467] bcm2835-cpufreq: switching to governor powersave
[ 1.985665] cpuidle: using governor ladder
[ 2.004159] cpuidle: using governor menu
[ 2.015320] sdhci: Secure Digital Host Controller Interface driver
[ 2.028826] sdhci: Copyright(c) Pierre Ossman
[ 2.040444] sdhci: Enable low-latency mode
[ 2.051618] bcm_power_open() -> 1
[ 2.062277] Registered led device: mmc0::
[ 2.063711] mmc0: SDHCI controller on BCM2708_Arasan [platform] using platform's DMA
[ 2.078899] Indeed it is in host mode hprt0 = 00021501
[ 2.098409] mmc0: BCM2708 SDHC host at 0x20300000 DMA 2 IRQ 77
[ 2.128474] sdhci-pltfm: SDHCI platform and OF driver helper
[ 2.158625] Registered led device: led0
[ 2.167974] usbcore: registered new interface driver usbhid
[ 2.198322] usbhid: USB HID core driver
[ 2.219941] mmc0: new high speed SDHC card at address b368
[ 2.234096] TCP cubic registered
[ 2.249914] Initializing XFRM netlink socket
[ 2.261637] mmcblk0: mmc0:b368 7.46 GiB
[ 2.274465] NET: Registered protocol family 17
[ 2.286224] Registering the dns_resolver key type
[ 2.298570] mmcblk0: p1 p2
[ 2.308797] VFP support v0.3: implementor 41 architecture 1 part 20 variant b rev 5
[ 2.325701] registered taskstats version 1
[ 2.337283] drivers/rtc/hctosys.c: unable to open rtc device (rtc0)
[ 2.368570] usb 1-1: new high-speed USB device number 2 using dwc_otg
[ 2.385049] Indeed it is in host mode hprt0 = 00001101
[ 2.399337] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
[ 2.428453] VFS: Mounted root (ext4 filesystem) on device 179:2.
[ 2.468092] devtmpfs: mounted
[ 2.489236] Freeing init memory: 124K
[ 2.648750] usb 1-1: New USB device found, idVendor=0424, idProduct=9512
[ 2.663816] usb 1-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[ 2.679508] hub 1-1:1.0: USB hub found
[ 2.691854] hub 1-1:1.0: 3 ports detected
[ 2.978686] usb 1-1.1: new high-speed USB device number 3 using dwc_otg
[ 3.109067] usb 1-1.1: New USB device found, idVendor=0424, idProduct=ec00
[ 3.137956] usb 1-1.1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[ 3.163742] smsc95xx v1.0.4
[ 3.244037] smsc95xx 1-1.1:1.0: eth0: register 'smsc95xx' at usb-bcm2708_usb-1.1, smsc95xx USB 2.0 Ethernet, b8:27:eb:22:44:60
[ 3.358607] usb 1-1.2: new high-speed USB device number 4 using dwc_otg
[ 3.500501] usb 1-1.2: New USB device found, idVendor=05e3, idProduct=0608
[ 3.518557] usb 1-1.2: New USB device strings: Mfr=0, Product=1, SerialNumber=0
[ 3.533874] usb 1-1.2: Product: USB2.0 Hub
[ 3.548211] hub 1-1.2:1.0: USB hub found
[ 3.566695] hub 1-1.2:1.0: 4 ports detected
[ 3.678670] usb 1-1.3: new high-speed USB device number 5 using dwc_otg
[ 3.811684] usb 1-1.3: New USB device found, idVendor=0b05, idProduct=1786
[ 3.841382] usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 3.869480] usb 1-1.3: Product: ASUS EZ N Network Adapter
[ 3.895998] usb 1-1.3: Manufacturer: Manufacturer Realtek
[ 3.928425] usb 1-1.3: SerialNumber: 00e04c000001
[ 4.038918] usb 1-1.2.4: new high-speed USB device number 6 using dwc_otg
[ 4.170470] usb 1-1.2.4: New USB device found, idVendor=05e3, idProduct=0608
[ 4.198467] usb 1-1.2.4: New USB device strings: Mfr=0, Product=1, SerialNumber=0
[ 4.237167] usb 1-1.2.4: Product: USB2.0 Hub
[ 4.257700] hub 1-1.2.4:1.0: USB hub found
[ 4.279353] hub 1-1.2.4:1.0: 4 ports detected
[ 4.318992] udevd[137]: starting version 175
[ 4.568838] usb 1-1.2.4.3: new low-speed USB device number 7 using dwc_otg
[ 4.713020] usb 1-1.2.4.3: New USB device found, idVendor=046d, idProduct=c05b
[ 4.748423] usb 1-1.2.4.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 4.784418] usb 1-1.2.4.3: Product: USB Optical Mouse
[ 4.797900] usb 1-1.2.4.3: Manufacturer: Logitech
[ 4.839803] input: Logitech USB Optical Mouse as /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4.3/1-1.2.4.3:1.0/input/input0
[ 4.903212] generic-usb 0003:046D:C05B.0001: input: USB HID v1.11 Mouse [Logitech USB Optical Mouse] on usb-bcm2708_usb-1.2.4.3/input0
[ 5.048915] usb 1-1.2.4.4: new low-speed USB device number 8 using dwc_otg
[ 5.211707] usb 1-1.2.4.4: New USB device found, idVendor=046e, idProduct=5500
[ 5.258446] usb 1-1.2.4.4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 5.307882] usb 1-1.2.4.4: Product: USB Multimedia Keyboard
[ 5.348468] usb 1-1.2.4.4: Manufacturer: BTC
[ 5.417882] input: BTC USB Multimedia Keyboard as /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4.4/1-1.2.4.4:1.0/input/input1
[ 5.488619] generic-usb 0003:046E:5500.0002: input: USB HID v1.10 Keyboard [BTC USB Multimedia Keyboard] on usb-bcm2708_usb-1.2.4.4/input0
[ 5.524947] input: BTC USB Multimedia Keyboard as /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4.4/1-1.2.4.4:1.1/input/input2
[ 5.525595] generic-usb 0003:046E:5500.0003: input,hiddev0: USB HID v1.10 Device [BTC USB Multimedia Keyboard] on usb-bcm2708_usb-1.2.4.4/input1
[ 7.812310] r8712u: module is from the staging directory, the quality is unknown, you have been warned.
[ 7.955034] r8712u: DriverVersion: v7_0.20100831
[ 8.039101] r8712u: register rtl8712_netdev_ops to netdev_ops
[ 8.159526] r8712u: USB_SPEED_HIGH with 4 endpoints
[ 8.250623] r8712u: Boot from EFUSE: Autoload OK
[ 9.037704] r8712u: CustomerID = 0x0010
[ 9.051584] r8712u: MAC Address from efuse = 10:bf:48:50:a3:e3
[ 9.067296] r8712u: Loading firmware from "rtlwifi/rtl8712u.bin"
[ 9.085841] usbcore: registered new interface driver r8712u
[ 14.076183] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)
[ 14.879398] bcm2835 ALSA card created!
[ 22.489043] r8712u: 1 RCR=0x153f00e
[ 22.489924] r8712u: 2 RCR=0x553f00e
[ 34.109035] r8712u: [r8712_got_addbareq_event_callback] mac = 2c:b0:5d:e5:d5:62, seq = 0, tid = 0
[ 70.132389] bcm2835-cpufreq: switching to governor ondemand
[ 70.132418] bcm2835-cpufreq: switching to governor ondemand
[ 76.190597] Adding 102396k swap on /var/swap. Priority:-1 extents:1 across:102396k SS
[ 76.635083] Bluetooth: Core ver 2.16
[ 76.635254] NET: Registered protocol family 31
[ 76.635271] Bluetooth: HCI device and connection manager initialized
[ 76.635289] Bluetooth: HCI socket layer initialized
[ 76.635302] Bluetooth: L2CAP socket layer initialized
[ 76.635345] Bluetooth: SCO socket layer initialized
[ 76.690930] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[ 76.690962] Bluetooth: BNEP filters: protocol multicast
[ 76.704832] Bluetooth: RFCOMM TTY layer initialized
[ 76.704881] Bluetooth: RFCOMM socket layer initialized
[ 76.704896] Bluetooth: RFCOMM ver 1.11
Pi from Element14 (still not got the first Pi from RS :( )
Rev2 board | Belkin 7Port powered hub | ASUS N10 WiFi | Logitech Wired Mouse | Behavior Tech Wired KB

User avatar
FeersumEndjinn
Posts: 148
Joined: Mon Nov 21, 2011 4:02 pm

Re: RPi IP Emailer

Thu Sep 20, 2012 10:19 am

Sorry that's my mistake because I can't test it at the moment - I have updated the gist, but basically I didn't modify the final print statement:

Code: Select all

    else:
        print '[' + '\033[31;1m' + 'warning'  + '\033[0m' + ']' + adapter +' not found in file!'
If this still keeps printing a warning, you can always do this:

Code: Select all

    else:
        pass
        # print '[' + '\033[31;1m' + 'warning'  + '\033[0m' + ']' + adapter +' not found in file!'
This will comment out the warning until I can figure out why you're still receiving it even if the adapter is present.

However, I do believe it to be an indetation error - check the gist for an updated version again, as usual.

Cheers,
FeersumEndjinn
Morrolan / FeersumEndjinn

"And the lord God said unto John, 'Come forth and receive eternal life', but John came fifth and won a toaster."

mcgyver83
Posts: 358
Joined: Fri Oct 05, 2012 11:49 am

Re: RPi IP Emailer

Mon Dec 10, 2012 1:16 pm

Hi,
I added

Code: Select all

### BEGIN INIT INFO
# Provides: Plesk Firewall
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# X-Start-Before:
# X-Stop-After:
# Default-Start:
# Default-Stop:
# X-Interactive: true
# Short-Description: Plesk Firewall (IP forwarding)
# Description:
### END INIT INFO
after import line but when I run

Code: Select all

sudo update-rc.d check_ip.py defaults 19
I have

Code: Select all

insserv: warning: current start runlevel(s) (2 3 4 5) of script `check_ip.py' overrides LSB defaults (empty).
insserv: warning: current stop runlevel(s) (0 1 6) of script `check_ip.py' overrides LSB defaults (empty).

User avatar
FeersumEndjinn
Posts: 148
Joined: Mon Nov 21, 2011 4:02 pm

Re: RPi IP Emailer

Mon Dec 10, 2012 7:53 pm

What distro are you trying this on?
Morrolan / FeersumEndjinn

"And the lord God said unto John, 'Come forth and receive eternal life', but John came fifth and won a toaster."

mcgyver83
Posts: 358
Joined: Fri Oct 05, 2012 11:49 am

Re: RPi IP Emailer

Wed Dec 12, 2012 3:18 pm

raspbian wheezy

mcgyver83
Posts: 358
Joined: Fri Oct 05, 2012 11:49 am

Re: RPi IP Emailer

Fri Dec 14, 2012 11:02 am

I don't know why but now it is working.
Only one thing: it prints alway 2 warn lines (not eth0 found) and after it found the "eth0" line.
This is due the structure of the txt file, why it is a "warn event" the fact that there are lines without eth0 in this file?

Code: Select all

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.?.?/25 brd 192.168.?.? scope global eth0

huczas
Posts: 2
Joined: Thu Jan 17, 2013 12:32 am

Re: RPi IP Emailer

Thu Jan 17, 2013 12:40 am

any chance to update code on github or hire?

I use this one on fresh wheezy raspbian:

Code: Select all

#!/usr/bin/python
# The idea behind this script is if plugging a RaspberryPi into a foreign network whilst running it headless
# (i.e. without a monitor/TV), you need to know what the IP address is to SSH into it.
#
# This script emails you the IP address if it detects an ethernet address other than it's usual address
# that it normally has, i.e. on your home network.

import smtplib, string, subprocess, time

### BEGIN INIT INFO
# Provides: check_ip.py
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# X-Start-Before:
# X-Stop-After:
# Default-Start:
# Default-Stop:
# X-Interactive: true
# Short-Description: check the ip and email it.
# Description:
### END INIT INFO

################################################
###### Remember to set all constants here ######
################################################
FIXED_IP = '192.168.1.10'
IP_FILEPATH = '/home/pi/Skrypty/current_ip.txt'
SMTP_USERNAME = 'XXX@gmail.com'
SMTP_PASSWORD = 'ZZZ'
SMTP_RECIPIENT = 'XXX@gmail.com'
SMTP_SERVER = 'smtp.gmail.com'
SSL_PORT = 465
################################################

class bcolors:
    HEADER = '\033[95m'
    INFOBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

    def disable(self):
        self.HEADER = ''
        self.INFOBLUE = ''
        self.OKGREEN = ''
        self.WARNING = ''
        self.FAIL = ''
        self.ENDC = ''

adapter = 'eth0:'
ipaddr_string = 'ip -4 addr > ' + IP_FILEPATH
subprocess.call(ipaddr_string, shell=True)
inet_string = ''

ip_file = file(IP_FILEPATH, 'r')
for line in ip_file:
        
    if adapter in line:
        inet_line = ip_file.next()
        _time = time.asctime()
        inet_string = inet_line[9:(inet_line.index('/'))]
 
        if inet_string != FIXED_IP:
 
            p = subprocess.Popen(["runlevel"], stdout=subprocess.PIPE)
            out, err=p.communicate()
            print out
            print out[2]
            if out[2] == '0':
                print 'Halt detected'
                exit(0)
            if out [2] == '6':
                print 'Shutdown detected'
                exit(0)
 
            SUBJECT = 'IP Address from Raspberry Pi at: %s' % time.asctime()
            TO = SMTP_RECIPIENT
            FROM = SMTP_USERNAME
            text = 'The IP address is: %s' % inet_string
            BODY = string.join((
                'From: %s' % FROM,
                'To: %s' % TO,
                'Subject: %s' % SUBJECT,
                '',
                text
                ), '\r\n')
            server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT)
            server.login(SMTP_USERNAME, SMTP_PASSWORD)
            server.sendmail(FROM, [TO], BODY)
            server.quit()

        print '[' + bcolors.INFOBLUE + 'info'  + bcolors.ENDC + ']' + ' Emailing eth0 IP address ' + inet_string + ' to ' + TO + ' from ' + FROM

ip_file.close()
but, this is just not working :(

When I run this script there is some error:

Code: Select all

Traceback (most recent call last):
  File "3.py", line 38, in <module>
    subprocess.call(ipaddr_string, shell=True)
NameError: name 'subprocess' is not defined
Could you help correct the code?

pittsat
Posts: 5
Joined: Sun Jan 27, 2013 5:22 am

Re: RPi IP Emailer

Sun Jan 27, 2013 5:34 am

No matter what I do, my RPi only emails me on reboot and poweroff. I really want it to email me its IP address when it turns on or comes back up. Here's my attempt.

Code: Select all

#!/usr/bin/python
# The idea behind this script is if plugging a RaspberryPi into a foreign network whilst running it headless
# (i.e. without a monitor/TV), you need to know what the IP address is to SSH into it.
#
# This script emails you the IP address if it detects an ethernet address other than it's usual address
# that it normally has, i.e. on your home network.

import smtplib, string, subprocess, time

################################################
###### Remember to set all constants here ######
################################################
FIXED_IP = '192.168.0.8'
IP_FILEPATH = '/home/pi/scripts/current_ip.txt'
SMTP_USERNAME = 'xxxx@gmail.com'
SMTP_PASSWORD = 'xxxx'
SMTP_RECIPIENT = 'xxxx@gmail.com'
SMTP_SERVER = 'smtp.gmail.com'
SSL_PORT = 465
################################################
 
ipaddr_string = 'ip -4 addr > ' + IP_FILEPATH
subprocess.call(ipaddr_string, shell=True)
inet_string = ''
 
ip_file = file(IP_FILEPATH, 'r')
for line in ip_file:
        
    if 'eth0:' in line:
        inet_line = ip_file.next()
        _time = time.asctime()
        inet_string = inet_line[9:(inet_line.index('/'))]
 
        if inet_string != FIXED_IP:
        
            SUBJECT = 'IP Address from Raspberry Pi at: %s' % time.asctime()
            TO = SMTP_RECIPIENT
            FROM = SMTP_USERNAME
            text = 'The IP address is: %s' % inet_string
            BODY = string.join((
                'From: %s' % FROM,
                'To: %s' % TO,
                'Subject: %s' % SUBJECT,
                '',
                text
                ), '\r\n')
            server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT)
            server.login(SMTP_USERNAME, SMTP_PASSWORD)
            server.sendmail(FROM, [TO], BODY)
            server.quit()
            print '[' + '\033[36;1m' + 'info'  + '\033[0m' + ']' + ' Emailing eth0 IP address ' + inet_string + ' to ' + TO + ' from ' + FROM
  
        elif inet_string == FIXED_IP:
            print '[' + '\033[32;1m' + ' ok '  + '\033[0m' + ']' + ' Normal IP address' + inet_string + 'found'
 
        else:
            print '[' + '\033[31;1m' + 'warning'  + '\033[0m' + ']' + ' eth0 not found in file!'
 
 
ip_file.close()
I would greatly appreciate any help you guys could offer me. Also, my programming ability is in its infantile stage.

pittsat
Posts: 5
Joined: Sun Jan 27, 2013 5:22 am

Re: RPi IP Emailer

Sun Jan 27, 2013 10:03 pm

pittsat wrote:No matter what I do, my RPi only emails me on reboot and poweroff. I really want it to email me its IP address when it turns on or comes back up. Here's my attempt.

Code: Select all

#!/usr/bin/python
# The idea behind this script is if plugging a RaspberryPi into a foreign network whilst running it headless
# (i.e. without a monitor/TV), you need to know what the IP address is to SSH into it.
#
# This script emails you the IP address if it detects an ethernet address other than it's usual address
# that it normally has, i.e. on your home network.

import smtplib, string, subprocess, time

################################################
###### Remember to set all constants here ######
################################################
FIXED_IP = '192.168.0.8'
IP_FILEPATH = '/home/pi/scripts/current_ip.txt'
SMTP_USERNAME = 'xxxx@gmail.com'
SMTP_PASSWORD = 'xxxx'
SMTP_RECIPIENT = 'xxxx@gmail.com'
SMTP_SERVER = 'smtp.gmail.com'
SSL_PORT = 465
################################################
 
ipaddr_string = 'ip -4 addr > ' + IP_FILEPATH
subprocess.call(ipaddr_string, shell=True)
inet_string = ''
 
ip_file = file(IP_FILEPATH, 'r')
for line in ip_file:
        
    if 'eth0:' in line:
        inet_line = ip_file.next()
        _time = time.asctime()
        inet_string = inet_line[9:(inet_line.index('/'))]
 
        if inet_string != FIXED_IP:
        
            SUBJECT = 'IP Address from Raspberry Pi at: %s' % time.asctime()
            TO = SMTP_RECIPIENT
            FROM = SMTP_USERNAME
            text = 'The IP address is: %s' % inet_string
            BODY = string.join((
                'From: %s' % FROM,
                'To: %s' % TO,
                'Subject: %s' % SUBJECT,
                '',
                text
                ), '\r\n')
            server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT)
            server.login(SMTP_USERNAME, SMTP_PASSWORD)
            server.sendmail(FROM, [TO], BODY)
            server.quit()
            print '[' + '\033[36;1m' + 'info'  + '\033[0m' + ']' + ' Emailing eth0 IP address ' + inet_string + ' to ' + TO + ' from ' + FROM
  
        elif inet_string == FIXED_IP:
            print '[' + '\033[32;1m' + ' ok '  + '\033[0m' + ']' + ' Normal IP address' + inet_string + 'found'
 
        else:
            print '[' + '\033[31;1m' + 'warning'  + '\033[0m' + ']' + ' eth0 not found in file!'
 
 
ip_file.close()
I would greatly appreciate any help you guys could offer me. Also, my programming ability is in its infantile stage.
Actually any python script I write will successfully send me an email if I manually run it from lxterminal. When I add it to /etc/init.d and update-rc.d script default 19 (or [start 99 2]), it only runs when the system is going down, always fails to send when it is coming back up.

Return to “Networking and servers”