Socket Up Mac OS

Mac mini (Late 2012), OS X Mountain Lion (10.8.4) Posted on Aug 17, 2013 6:50 PM Reply I have this question too (14) I have this question too Me too (14) Me too.

  • If you are writing code for OS X and iOS exclusively, use POSIX networking calls to set up your network sockets. Then, use GCD or CFSocket to integrate the sockets into your run loop. Use pure POSIX networking code with a POSIX-based run loop ( select ) if cross-platform portability with non-Apple platforms is required.
  • I need help for open socket in MAC OS Programming. I want to know how to call and connect with the socket so that i am able to open socket over the view by clicking on the button.
  • I need help for open socket in MAC OS Programming. I want to know how to call and connect with the socket so that i am able to open socket over the view by clicking on the button.

Wake a sleeping Mac from the network 36 comments Create New Account
Click here to return to the 'Wake a sleeping Mac from the network' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.

As Yoda would say, 'Silly rabbits! Know you not about 'Wake 550'? Hmm?'
http://www.versiontracker.com/moreinfo.fcgi?id=13645&db=mac
'Good is the way of waking by mac addresses. And good is the way the force makes the app remember frequently used mac address.'
Yoda and I only wish that the app supported linking a name to each mac address. You know, so you can pick 'PowerBook' or 'G3 Workstation' off your Wake 550 list. Maybe in a future release...

It does work, but I wish it would allow me to set a name for each address so I could easily pick a particular computer from a list. Anything like this out there?

Instead of just wishing, I sent the author, '550 Software', who I suppose is a student at the University of Minnesota. Here's his website:
http://www.tc.umn.edu/~olve0003/wake550.html
I asked if he could please add this feature.
I have included a certain amount of filth to please the gentlemen of the
press. They have proved ungrateful.
Charles Baudelaire

I just got an email back from the author, and he says he's going to add my feature request, and he may release the new version as early as tonight!
Coolio.

The original poster stated that he was remotely turning on his iMac. I wonder if this is a misprint when he probably meant to say waking it from sleep.
The original Wake on Lan, which involves booting up a machine that is turned off, is not supported on the macs. I could be wrong but I think this is the case. PC's that have this capability have an additional power cable from their ethernet cards to the power supply. The ethernet cards are always on when the machine is shutdown. That way they can receive the cmds from an admin to turn them on remotely.
Wake from Sleep over the network has been supported I think beginning with last years machines and OS 9.1. This feature is fine and dandy, but I have never understood while apple hasn't supported the remote booting option of wake on lan. If we can schedule a startup time for the mac, then it shouldn't be that much more to support the booting from shutdown.

Try:
http://www.dslreports.com/wakeup

I am not sure if Apple currently uses a feature of Wake On LAN for turning a Mac on but I do know that Apple had software before Wake On LAN existed (similar to Apple Remote Desktop http://www.apple.com/remotedesktop/ ) that would allow Admins to start a Mac remotely.

I have MAC machine (version iMAC G5). Could anybody tell me that whether I can wake up this from shoutdown using Wake on LAN utilities. I have already started this machine from sleep mode. If anybody have any idea, please let ame know ASAP as its urgent for our project requirements.
Thanks & Regards,
Sandeep Wadhwa

I can wake my dell computer at work, even if it is shut down. But it seems I can only wake my Powermac G4 (Quicksilver) if it is asleep.
Is there a setting somewhere I need to change?
Thanks,
Loren

Nope, and we can blame Apple. Wake on LAN is a feature built into the NIC (the Ethernet card), and then the OS must support it too. This is one of the things, in the PC world, that determines one the differences between a manufacturer's home computer line (like Compaq Prosario), and business computer line (like Compaq Deskpro).
I'm hoping that G4's will eventually get a firmware upgrade that will support this in OS X. But I'm not holding my breath...

I have a Quicksilver 2002 800mhz on 10.2.2 and I have never been able to use the 'wake for Administrative access' function through ssh, eihter from a windowz machine or a Mac over the network. Are there any known issues, and how do I troubleshoot this issue? Apple doesn't really support this function because 'it's in the command line', and nothing I've tried short of a firmware update [which I haven't done] works.
Any help would be much appreciated.
Sean the digital one...

Editor, the python example that was given is not to be run on the machine to wake. It's meant to be run on some other machine that you have access to. The python example could be written in any other language you want. It just sends particular data on a socket. The machine to wake doesn't need to be running anything in particular - just listening on the other end of the socket.
(This is all assuming, of course, that I read the article correctly...)

You read the article correctly, but not the editor's comments. The editor wanted a version of the script written in something else (like a shell script using sh) that's installed on all OS X machines so he could use it. This is because the Editor doesn't have/use python, so he can't use the python script.
As a side note, I second the editor's request.

Spiff up the python script and just compile the python script into an Terminal executable. Works at least on Linux.

there was an author's comment on the version tracker page which should solve this. i quote:
'To [APi]TheMan From Developer: Look under the source folder for a file named 'wakeonlan'. This file is really a compiled perl script which my program uses. Therefore you can use that directly from the command line.'
Many thanks to everyone -- I was needing exactly this feature (wake from cli). I have a server running 24/7 and a sleeping g4/733 workstation. I can only telnet into the server from work and sometimes I need a file from the workstation. This will be far easier than firing up TBk2 on my laptop and dialing up the workstation.
/guy

A couple of months back, when I first discovered that my Cube could be woken from sleep by 'magic' wake-on-LAN packets, I found a short C program that would do just that. It was more of a test program and had the MAC address of the target computer hard-coded. It was evidently written for Linux, but since it used the usual BSD-style socket calls, I did get it to complie and work under OSX. I didn't mess with it any further because of other projects.
If anyone is interested, I can try to look for the source or at least a link to where I found it.

Since at least one person has asked, here's the link to a small C prg that will send the magic WOL packet.
You will need to add the following line:
#include <unistd.h>
to the wakelan.c file, but it should configure/make just fine after that. This is a decent page of info for anyone interested in WOL.

Hi all,
Wake550 does its part fine, but not every Mac responds well. My DP800 hears the magic packet,
cranks up its fan and hard disk ... then freezes and has to be rebooted. Any ideas?
When manually prodded this G4 quicksilver normally wakes from sleep pretty well,
although until I shuffled its RAM I was an
unhappy participant in the thread on Apple's support board (totalling 100+msgs) discussing the
widespread difficulties some G4's have waking up reliably. In OS9 that's often due to a weird
extension but there's no explanation yet for OSX.
Thanks - Ron

I know it would be nice if we all had Macs on our desktops, but the most common situation would be to start your home OS X machine from a PC in your office, running W2K for example.
Any easy solution for that? (I know I could install Python or Pearl and running those scripts from W2K, but that's a huge task. There must be some App to make that easier.)

It's nice if one is able to answer one's question himself ;)
A lot of Windows-tools can be found here:
http://www.gknw.de/wol.html
The one that worked best for me (in my local network at least, waking an iBook from my PC) is the GUI-thingy from AMD, linked here as 'Magic Packet Utility'.

I have discovered another very useful Windows Application that lets you wake up a Mac using the 'Magic packet technology' (I think it is an AMD technology), that does exactly that; sends a 'wake up' packet to the Mac an wake it up. It is a very simple GUI app that asks for a MAC address (you can get it from the network preferences), and an IP. It works for my every time (iMac 500 slot-loading CD). You can get the program here: http://www.depicus.com/wake-on-lan/wake-on-lan-gui.asp

It looks like you are sending this on port 80 of a particular IP address. So, if I have port 80 of my router fowarded to my Mac and I send this request to the IP address of the Router should it then wake up the Mac?
I would however need to send the MAC address of the computer not the Router right?
Would this work? It would be very cool if it does since I am often gone all week for work and it would be nice to be able to turn the computer on and off remotely...

It only allows a sleeping computer to be woken up. If the system is shutdown then this won't work. The MAC address must be that of the computer you wish to wake.

The original post seems to indicate port 80 right after the broadcast address, however according to BroadbandReports.com WakeUp Page, a UDP packet is sent on port 9. Can anyone clarify which port(s) need to be forwarded for WOL to work thru a router? Thanks.

Just about any port you want.

The port you send to is entirely irrelevant. You could send it on another IP protocol such as ICMP that doesn't have ports at all, or on MS or Apple's old systems (NetBEUI/AppleTalk) or even plain raw Ethernet. All that's required is that the network card see a certain sequence of bytes on the wire.
It's common to use UDP port 9 because this is the 'Discard' port from the early days of IP. Using this means WOL won't interfere with anything else you might have running. However, using some other port might be handy if it's already opened to allow another service to run. TCP port 80 would be a good example - anyone who runs a Web server would be able to use WOL on 80 without any further configuration.
Pete

will the script above turn on a modern Mac (G4,iMac,PBK) that is off over a LAN or will it only wake it up from sleep?

...if you're running OS X 10.2 and installed the goodies from the Developer disk. Pull up a shell and type 'python'. It'll run 2.2 for you.

The magic packet has to be sent over UDP because you won't be able to open a TCP connection with a sleeping host. I've written a PHP function which does all the magic: Call the function like this: where the first parameter is the IP address (or hostname) and the second parameter the MAC address of the computer to wake.
It works tip top with my AluBook G4.

Thanks, worked great for me on my home network with one additional change, I needed to also change 192.168.1.255 in the script with the IP address of the computer I wanted to wake, otherwise I got an error... socket.error: (13, 'Permission denied')
Note: I have a standard MacOSX 10.4.8 install with the developer tools and the script just worked. No need to install python.
Here's the steps:
1. paste the script into a new text document
2. changed the MAC address and IP address to the computer I want to wake
3. saved it as wakeonlan
4. made it executable with chmod a+x wakeonlan
5. set the Energy Saver preference to allow wakeonlan on the computer I wante to wake

To get this script to work with the 'broadcast' address (which is useful if you don't know the sleeping machine's IP (when it is dynamically assigned using eg DHCP)), I added one line:
#!/usr/bin/env python
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto('xff'*6+'x00x01x02x03x04x05'*16, ('192.168.1.255', 9))
and also changed the port to 9 (the 'discard' port).

any development on this issue?
I'm working on incorberating a button on a webpage to wake my macmini wich runs a a small rumpus server and then log on to it..
doable?

Thanks a lot for the data - I am a sysadmin and I really needed to wake my macs up to run backups. I wasn't in the office, working over a VPN - very happy when ssh finally got through..
One datum of note: For your python script to run on a Linux system (and I assume Unix [read Mac] as well) you need to add the following line just after you declare the s variable:
s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,True)
By the way, highly important the point Yoda made is; for young apprentice Jedis, better nothing is than GUI.
But listen well, Anakin - when you are not in the office or just can't be bothered to manually back up every night (no matter how nice the GUI is for waking up the remote computer), copy the python code into a file, make it executable, and have your shell script execute it before it runs the backup commands (rsync over ssh, anyone?)
Oh, and don't forget to throw 'sleep 60' after the wakeup script, so that you give your mac(s) time to get out of bed before you try getting in and doing your thing.

This helped out a lot. I couldn't figure out why it wasn't working when going from my Ubuntu server to the MacBook. Also had problems due to not delaying the script. Wish I would have read this first!

Actually your python script also needs to set SO_BROADCAST:
#!/usr/bin/env python
import socket;
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1);
s.sendto('xff'*6+'x00x24x36xf0xc4x69'*16, ('10.0.0.255', 80));
http://blog.esync.org

Article Category:#Code

Posted on

.

Updates

Hello. How are you? Thanks for stopping by.

It used to be tricky to get Docker working on OS X, which is why I wrote this here blog post. With the release of Docker 1.8, it just got way easier. Now there's a new thing, Docker Toolbox, that makes it super easy. You should install that instead of reading this post. Or you can read it if you want, I think it's a pretty good blog post and you'll learn some stuff. No pressure, though.

Have you heard of Docker? You probably have—everybody’s talking about it. It’s the new hotness. Even my dad’s like, “what’s Docker? I saw someone twitter about it on the Facebook. You should call your mom.”

Docker is a program that makes running and managing containers super easy. It has the potential to change all aspects of server-side applications, from development and testing to deployment and scaling. It’s pretty cool.

Recently, I’ve been working through The Docker Book. It’s a top notch book and I highly recommend it, but I’ve had some problems running the examples on OS X. After a certain point, the book assumes you’re using Linux and skips some of the extra configuration required to make the examples work on OS X. This isn’t the book’s fault; rather, it speaks to underlying issues with how Docker works on OS X.

This post is a walkthrough of the issues you’ll face running Docker on OS X and the workarounds to deal with them. It’s not meant to be a tutorial on Docker itself, but I encourage you to follow along and type in all the commands. You’ll get a better understanding of how Docker works in general and on OS X specifically. Plus, if you decide to dig deeper into Docker on your Mac, you’ll be saved hours of troubleshooting. Don’t say I never gave you nothing.

First, let’s talk about how Docker works and why running it on OS X no work so good.

How Docker Works

Docker is a client-server application. The Docker server is a daemon that does all the heavy lifting: building and downloading images, starting and stopping containers, and the like. It exposes a REST API for remote management.

Socket Up Mac Os 11

The Docker client is a command line program that communicates with the Docker server using the REST API. You will interact with Docker by using the client to send commands to the server.

The machine running the Docker server is called the Docker host. The host can be any machine—your laptop, a server in the Cloud™, etc—but, because Docker uses features only available to Linux, that machine must be running Linux (more specifically, the Linux kernel).

Docker on Linux

Suppose we want to run containers directly on our Linux laptop. Here’s how it looks:

Docking on Linux

The laptop is running both the client and the server, thus making it the Docker host. Easy.

Docker on OS X

Here’s the thing about OS X: it’s not Linux. It doesn’t have the kernel features required to run Docker containers natively. We still need to have Linux running somewhere.

Enter boot2docker. boot2docker is a “lightweight Linux distribution made specifically to run Docker containers.” Spoiler alert: you’re going to run it in a VM on your Mac.

Here’s a diagram of how we’ll use boot2docker:

Docking on OS X

We’ll run the Docker client natively on OS X, but the Docker server will run inside our boot2docker VM. This also means boot2docker, not OS X, is the Docker host, not OS X.

Make sense? Let’s install dat software.

Installation

Step 1: Install VirtualBox

Go here and do it. You don’t need my help with that.

Step 2: Install Docker and boot2docker

You have two choices: the offical package from the Docker site or homebrew. I prefer homebrew because I like to manage my environment from the command line. The choice is yours.

Step 3: Initialize and start boot2docker

First, we need to initialize boot2docker (we only have to do this once):

Next, we can start up the VM. Do like it says:

Step 4: Set the DOCKER_HOST environment variable

The Docker client assumes the Docker host is the current machine. We need to tell it to use our boot2docker VM by setting the DOCKER_HOST environment variable:

Your VM might have a different IP address—use whatever boot2docker up told you to use. You probably want to add that environment variable to your shell config.

Step 5: Profit

Let’s test it out:

Mac Os Download

Great success. To recap: we’ve set up a VirtualBox VM running boot2docker. The VM runs the Docker server, and we’re communicating with it using the Docker client on OS X.

Bueno. Let’s do some containers.

Common Problems

We have a “working” Docker installation. Let’s see where it falls apart and how we can fix it.

Problem #1: Port Forwarding

The Problem: Docker forwards ports from the container to the host, which is boot2docker, not OS X.

Let’s start a container running nginx:

This command starts a new container as a daemon (-d), automatically forwards the ports specified in the image (-P), gives it the name ‘web’ (--name web), and uses the nginx image. Our new container has the unique identifier 0092c03e1eba....

Verify the container is running:

Under the PORTS heading, we can see our container exposes port 80, and Docker has forwarded this port from the container to a random port, 49153, on the host.

Let’s curl our new site:

It didn’t work. Why?

Remember, Docker is mapping port 80 to port 49153 on the Docker host. If we were on Linux, our Docker host would be localhost, but we aren’t, so it’s not. It’s our VM.

The Solution: Use the VM’s IP address.

boot2docker comes with a command to get the IP address of the VM:

Let’s plug that into our curl command:

Success! Sort of. We got the web page, but we got The VM’s Host only interface IP address is:, too. What’s the deal with that nonsense.

Turns out, boot2docker ip outputs the IP address to standard output and The VM's Host only interface IP address is: to standard error. The $(boot2docker ip) subcommand captures standard output but not standard error, which still goes to the terminal. Scumbag boot2docker.

This is annoying. I am annoyed. Here’s a bash function to fix it:

Stick that in your shell config, then use it like so:

Groovy. This gives us a reference for the IP address in the terminal, but it would be nice to have something similar for other apps, like the browser. Let’s add a dockerhost entry to the /etc/hosts file:

Now we can use it everywhere:

Great success. Make sure to stop and remove the container before continuing:

VirtualBox assigns IP addresses using DHCP, meaning the IP address could change. If you’re only using one VM, it should always get the same IP, but if you’re VMing on the reg, it could change. Fair warning.

Mac Os Versions

Bonus Alternate Solution: Forward all of Docker’s ports from the VM to localhost.

If you really want to access your Docker containers via localhost, you can forward all of the ports in Docker’s port range from the VM to localhost. Here’s a bash script, taken from here, to do that:

By doing this, Docker will forward port 80 to, say, port 49153 on the VM, and VirtualBox will forward port 49153 from the VM to localhost. Soon, inception. You should really just use the VM’s IP address mmkay.

Problem #2: Mounting Volumes

The Problem: Docker mounts volumes from the boot2docker VM, not from OS X.

Docker supports volumes: you can mount a directory from the host into your container. Volumes are one way to give your container access to resources in the outside world. For example, we could start an nginx container that serves files from the host using a volume. Let’s try it out.

First, let’s create a new directory and add an index.html:

(Make sure to replace /Users/Chris with your own path).

Next, we’ll start another nginx container, this time mounting our new directory inside the container at nginx’s web root:

We need the port number for port 80 on our container:

Let’s try to curl our new page:

Well, that didn’t work. The problem, again, is our VM. Docker is trying to mount /Users/Chris/web from the host into our container, but the host is boot2docker, not OS X. boot2docker doesn’t know anything about files on OS X.

The Solution: Mount OS X’s /Users directory into the VM.

By mounting /Users into our VM, boot2docker gains a /Users volume that points to the same directory on OS X. Referencing /Users/Chris/web inside boot2docker now points directly to /Users/Chris/web on OS X, and we can mount any path starting with /Users into our container. Pretty neat.

boot2docker doesn’t support the VirtualBox Guest Additions that allow us to make this work. Fortunately, a very smart person has solved this problem for us with a custom build of boot2docker containing the Guest Additions and the configuration to make this all work. We just have to install it.

First, let’s remove the web container and shut down our VM:

Next, we’ll download the custom build:

Finally, we share the /Users directory with our VM and start it up again:

Replacing the boot2docker image won’t erase any of the data in your VM, so don’t worry about losing any of your containers. Good guy boot2docker.

Let’s try this again:

Great success! Let’s verify that we’re using a volume by creating a new file on OS X and seeing if nginx serves it up:

Sweet damn. Make sure to stop and remove the container:

If you update index.html and curl it, you won’t see your changes. This is because nginx ships with sendfile turned on, which doesn’t play well with VirtualBox. The solution is simple—turn off sendfile in the nginx config file—but outside the scope of this post.

Problem #3: Getting Inside a Container

The Problem: How do I get in there?

Socket

So you’ve got your shiny new container running. The ports are forwarding and the volumes are ... voluming. Everything’s cool, until you realize something’s totally uncool. You’d really like to start a shell in there and poke around.

The Solution: Linux Magic

Enter nsenter. nsenter is a program that allows you to run commands inside a kernel namespace. Since a container is just a process running inside its own kernel namespace, this is exactly what we need to start a shell inside our container. Let’s make it so.

This part deals with shells running in three different places. Trés confusing. I’ll use a different prompt to distinguish each:

  • >for OS X
  • $for the boot2docker VM
  • %for inside a Docker container

First, let’s SSH into the boot2docker VM:

Next, install nsenter:

(How does that install it? jpetazzo/nsenter is a Docker image configured to build nsenter from source. When we start a container from this image, it builds nsenter and installs it to /target, which we’ve set to be a volume pointing to /var/lib/boot2docker in our VM.

In other words, we start a prepackaged build environment for nsenter, which compiles and installs it to our VM using a volume. How awesome is that? Seriously, how awesome? Answer me!)

Finally, we need to add /var/lib/boot2docker to the docker user’s PATH inside the VM:

We should now be able to use the installed binary:

Let’s start our nginx container again and see how it works (remember, we’re still SSH’d into our VM):

Time to get inside that thing. nsenter needs the pid of the running container. Let’s get it:

The moment of truth:

Great success! Let’s confirm we’re inside our container by listing the running processes (we have to install ps first):

We can see two nginx processes, our shell, and ps. How cool is that?

Getting the pid and feeding it to nsenter is kind of a pain. jpetazzo/nsenter includes docker-enter, a shell script that does it for you:

The default command is sh, but we can run any command we want by passing it as arguments:

This is totally awesome. It would be more totally awesomer if we could do it directly from OS X. jpetazzo’s got us covered there, too (that guy thinks of everything), with a bash script we can install on OS X. Below is the same script, but with a minor change to default to bash, because that’s how I roll.

Just stick this bro anywhere in your OS X PATH (and chmod +x it, natch) and you’re all set:

Let’s test it out:

Yes. YES. Cue guitar solo.

Don’t forget to stop and remove your container (nag nag nag):

The End

You now have a Docker environment running on OS X that does all the things you’d expect. You’ve also hopefully learned a little about how Docker works and how to use it. We’ve had some laughs, and we’ve learned a lot, too. I’m glad we’re friends.

If you’re ready to learn more about Docker, check out The Docker Book. I can’t recommend it enough. Throw some money at that guy.

The Future Soon

Docker might be the new kid on the block, but we’re already thinking about ways to add it to our workflow. Stay tuned for great justice.

Was this post helpful? How are you using Docker? Let me know down there in the comments box. Have a great. Call your mom.