[Help] Function to return the users external ip?

blov · · 635 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So i am trying to have my Go program connect to <a href="http://checkip.amazonaws.com/">http://checkip.amazonaws.com/</a> and return the IP address shown, so the main thread can do things with it (example Println)</p> <p>Every time i try doing this with http.get i get a return of </p> <blockquote> <p>0x401360</p> </blockquote> <p>This needs to be a separate function from main, so it can be called when needed.</p> <p>I am new to GoLANG, my main language is VB.net so its a little hard for me.</p> <hr/>**评论:**<br/><br/>iamedwards: <pre><p>Please provide your code, sounds like you&#39;re printing the pointer to your function and not the results. </p></pre>DeedleFake: <pre><p>I&#39;m not sure why you&#39;re trying to do this, but this potentially inefficient implementation should work:</p> <p>+<a href="/u/CompileBot" rel="nofollow">/u/CompileBot</a> go --include-errors</p> <pre><code>package main import ( &#34;bytes&#34; &#34;fmt&#34; &#34;io/ioutil&#34; &#34;net/http&#34; ) func checkIP() (string, error) { rsp, err := http.Get(&#34;http://checkip.amazonaws.com&#34;) if err != nil { return &#34;&#34;, err } defer rsp.Body.Close() buf, err := ioutil.ReadAll(rsp.Body) if err != nil { return &#34;&#34;, err } return string(bytes.TrimSpace(buf)), nil } func main() { ip, err := checkIP() if err != nil { panic(err) } fmt.Printf(&#34;CheckIP reports %q&#34;, ip) } </code></pre></pre>SaturnsVoid: <pre><p>Huh, didn&#39;t know that Go had ability&#39;s to add quotes to strings and stuff like that.. <a href="https://golang.org/pkg/fmt/" rel="nofollow">https://golang.org/pkg/fmt/</a></p></pre>SaturnsVoid: <pre><p>How is it inefficient? As far as i know this is the only way to get the systems external IP...</p> <p>I need it in another function for calling at anytime, like so;</p> <p>Server tells client to tell its IP -&gt; Client tells IP to server -&gt; Server tells Client to change IP, then asks for the new IP</p> <p>There isn&#39;t a set time i want it to check the IP, just whenever i send to command to do so.</p> <p>I am new so i may not know, is there a way do do this simpler?</p></pre>SpaceDetective: <pre><p>Is this any use?<br/> <a href="http://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go" rel="nofollow">http://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go</a></p></pre>SaturnsVoid: <pre><p>Sadly only gives back the local IP, 192.168.XX.XX</p></pre>SpaceDetective: <pre><p>OK you mean you&#39;re going through a proxy/nat. I guess your original plan is on the right track then...</p></pre>IntellectualReserve: <pre><p>I see the solution you are trying to implement, and I understand the question you are trying to get answered so you can continue with the solution you already have in mind. What is the problem this solution is supposed to solve? </p></pre>CompileBot: <pre><p>Output:</p> <pre><code>panic: Get http://checkip.amazonaws.com: dial tcp: lookup checkip.amazonaws.com: no such host goroutine 1 [running]: main.main() /home/Ua4LWh/prog.go:28 +0x62 goroutine 17 [syscall, locked to thread]: runtime.goexit() /usr/local/go/src/runtime/asm_386.s:2287 +0x1 </code></pre> <p><a href="http://ideone.com/5X6gbd" rel="nofollow"><sup>source</sup></a> <sup>|</sup> <a href="http://www.reddit.com/r/CompileBot/wiki" rel="nofollow"><sup>info</sup></a> <sup>|</sup> <a href="https://github.com/renfredxh/compilebot" rel="nofollow"><sup>git</sup></a> <sup>|</sup> <a href="http://www.reddit.com/message/compose?to=compilebot&amp;subject=Report%20Abuse&amp;message=--report%20https%3A//www.reddit.com/r/golang/comments/3l71g4/help_function_to_return_the_users_external_ip/cv3pj7r%20Include%20your%20reason%20for%20reporting%20here." rel="nofollow"><sup>report</sup></a></p></pre>DeedleFake: <pre><p>It doesn&#39;t work in whatever sandbox CompileBot runs in, though.</p></pre>SaturnsVoid: <pre><p>It wouldn&#39;t, Its setup to block out-going connections for security.</p></pre>sc28: <pre><p>How about this? curl <a href="http://169.254.169.254/latest/meta-data/public-ipv4" rel="nofollow">http://169.254.169.254/latest/meta-data/public-ipv4</a></p> <p>This assumes that your Go program is running inside an EC2 instance - just use that metadata URL</p></pre>SaturnsVoid: <pre><p>Running it as a standalone application an windows exe.</p></pre>Mteigers: <pre><p>Something like <a href="http://github.com/polds/MyIP" rel="nofollow">github.com/polds/MyIP</a> ?</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

635 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传