With Skype configured as a startup program, whenever the pc is booted, Skype manages to connect itself. So is a potentially good way to get machine's IP.
After a bit of digging in google and a some customization, i ended up with a small ruby script that instantiates an win32ole skype object, and can listen to incoming events like chats or calls, in this case i'm listening to a chat, whenever gets a text message with "IP", it runs the ipconfig command and returns the output as an answer to the chat.
Ruby Code:
# This simple example echos whatever chat message that is sent to you
#make it wait a bit before, skype connects
#sleep(60)
puts "Ready..."
# create the new Skype automation object
skype = WIN32OLE.new 'Skype4COM.Skype'
# some constants for easy viewing
$RECEIVED= skype.Convert.TextToChatMessageStatus("RECEIVED")
$SAID = skype.Convert.TextToChatMessageType("SAID")
# uncomment the below if you want to run in silent mode. Only works from Skype 2.6 and above
# skype.SilentMode = true
# attach to Skype
skype.attach
# A default handler for any events. You can use this to handle all miscellanous events or if
# you don't have any specific behaviors you want to trigger on
case event
# When someone calls
when "CallStatus"
# do nothing for now
# When any online status changes
when "OnlineStatus"
# do nothing
end
end
# Or you can use a specific method to catch this event
# echo whatever is send to you in a chat message
if status == $RECEIVED && msg.Type == $SAID
case msg.body
#request for IP
when "IP"
puts "request for IP Address from " + msg.FromDisplayName
s = `ipconfig`.scan(/IP Add[^\r\n]*/).to_s
#msg.chat.SendMessage(s.scan(/IP Add[^\r\n]*/))
msg.chat.SendMessage(s)
end
end
#msg.chat.SendMessage msg.body if status == $RECEIVED && msg.Type == $SAID
end
# register to receive events from Skype
event = WIN32OLE_EVENT.new(skype, '_ISkypeEvents')
# on any event at all, go to the default handler
event.on_event {|*args| default_handler(*args)}
# for this specific event
event.on_event("MessageStatus") {| *args| message_status args[0], args[1]}
# loop forever
catch(:done) do
loop do
WIN32OLE_EVENT.message_loop
end
end
No comments:
Post a Comment