# Open File Remotely

**URL:** <https://www.ruby-forum.com/t/open-file-remotely/197110>\
**Category:** Ruby\
**Created:** [November 12, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/open-file-remotely/197110 "2009-11-12T16:30:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mishoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mishoo](https://www.ruby-forum.com/u/mishoo)\
**Post date:** [November 12, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/open-file-remotely/197110/1 "2009-11-12T16:30:09Z")

</div>

I know that this question looks like a Webistrano question but since I  
donâ€™t know much about Ruby, I thought it might be a simple Ruby question  
too.  
We have installed Webistrano and Capistrano install on some server  
(letâ€™s say Server1). I have created a Webistrano project and inside it I  
have defined my Host to be â€˜Server2â€™. I have this Ruby code inside my  
project:  
require ‘open-uri’  
writeout = open("/home/test/test.txt", “wb”)  
writeout.write(â€œTESTâ€)  
writeout.close

The problem is Webistrano is giving me an error saying that  
/home/test/test.txt does not exist. I believe what Itâ€™s trying to do is  
to create test.txt in Server1 (Capistrano Server) instead of my defined  
Host.  
Is there any way in Ruby to say Open the file remotely (e.g.  
â€œServer2:/home/test/test.txtâ€) (assuming there is no Server of any kind  
installed on Server2).  
Strange is if I have a command like run “bash --login -c ‘touch  
test.txt’”, it will create test.txt in the right defined Host (Server2).  
But â€˜openâ€™ somehow is failing.

Thanks in Advance,

Homer

---

<div class="post-metadata">

**Author:** ![mishoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mishoo](https://www.ruby-forum.com/u/mishoo)\
**Post date:** [November 12, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/open-file-remotely/197110/2 "2009-11-12T18:13:07Z")

</div>

> Is there any way in Ruby to say Open the file remotely (e.g.  
> â€œServer2:/home/test/test.txtâ€) (assuming there is no Server of any kind  
> installed on Server2).

I doubt it. You could try farming out to ssh, though…

---

<div class="post-metadata">

**Author:** ![mishoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mishoo](https://www.ruby-forum.com/u/mishoo)\
**Post date:** [November 12, 2009, 10:02pm UTC](https://www.ruby-forum.com/t/open-file-remotely/197110/3 "2009-11-12T22:02:02Z")

</div>

On 11/12/09, Homer N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there any way in Ruby to say Open the file remotely (e.g.  
> “Server2:/home/test/test.txt”) (assuming there is no Server of any kind  
> installed on Server2).

Try open-uri, maybe? It’s in the standard lib.

---

<div class="post-metadata">

**Author:** ![mishoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mishoo](https://www.ruby-forum.com/u/mishoo)\
**Post date:** [November 13, 2009, 12:38am UTC](https://www.ruby-forum.com/t/open-file-remotely/197110/4 "2009-11-13T00:38:19Z")

</div>

On Thursday 12 November 2009 09:30:14 am Homer N. wrote:

> Is there any way in Ruby to say Open the file remotely (e.g.  
> â€œServer2:/home/test/test.txtâ€) (assuming there is no Server of any kind  
> installed on Server2).

Short answer: No. That’s really not a Ruby question, either – would you  
expect that to work in any language?

> Strange is if I have a command like run “bash --login -c ‘touch  
> test.txt’”, it will create test.txt in the right defined Host (Server2).  
> But â€˜openâ€™ somehow is failing.

Ah, now this is a Capistrano question. FYI, there is a Capistrano group.

First, how are you running this command? Ruby has two main ways of  
running  
commands: system and backticks. It seems you’re doing neither of these,  
but  
rather, “run”, which is a Capistrano command.

Run sends that command over to the remote server, over the ssh  
connection, to  
run there.

Ruby code in Cap, however, runs locally. Try running the same command  
with  
“system” and see what happens.

If all you’re wanting to do is programmatically generate a file as you  
upload  
it, I seem to remember some Capistrano recipes for that. It’s been  
awhile,  
though, and I’m feeling lazy, so ask the Capistrano group.

---

<div class="post-metadata">

**Author:** ![mishoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mishoo](https://www.ruby-forum.com/u/mishoo)\
**Post date:** [November 17, 2009, 4:28am UTC](https://www.ruby-forum.com/t/open-file-remotely/197110/5 "2009-11-17T04:28:36Z")

</div>

On Monday 16 November 2009 09:06:55 am Homer N. wrote:

> Thanks All. Finally I’ve found an easier way to fix this problem. I SSH  
> to the box and do WGET to get the remote file. I am not sure this is the  
> best way but it seems working.

It’s not that far from how Capistrano was originally designed to work,  
with  
one problem: You’re probably not doing any kind of authentication with  
that  
wget, which is probably a Bad Idea. That’s why I usually send things via  
“put”  
with Capistrano – you’ve already got an encrypted, authenticated  
channel with  
SSH, why not use it?

> I guess I need to learn some  
> Ruby/Capistrano in meanwhile.

Also, don’t top-post.

---

<div class="post-metadata">

**Author:** ![mishoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mishoo](https://www.ruby-forum.com/u/mishoo)\
**Post date:** [November 16, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/open-file-remotely/197110/6 "2009-11-16T16:06:51Z")

</div>

Thanks All. Finally I’ve found an easier way to fix this problem. I SSH  
to the box and do WGET to get the remote file. I am not sure this is the  
best way but it seems working. I guess I need to learn some  
Ruby/Capistrano in meanwhile.

Homer

David M. wrote:

> On Thursday 12 November 2009 09:30:14 am Homer N. wrote:
> 
> > Is there any way in Ruby to say Open the file remotely (e.g.  
> > â€œServer2:/home/test/test.txtâ€) (assuming there is no Server of any kind  
> > installed on Server2).
> 
> Short answer: No. That’s really not a Ruby question, either – would you  
> expect that to work in any language?
> 
> > Strange is if I have a command like run “bash --login -c ‘touch  
> > test.txt’”, it will create test.txt in the right defined Host (Server2).  
> > But â€˜openâ€™ somehow is failing.
> 
> Ah, now this is a Capistrano question. FYI, there is a Capistrano group.
> 
> First, how are you running this command? Ruby has two main ways of  
> running  
> commands: system and backticks. It seems you’re doing neither of these,  
> but  
> rather, “run”, which is a Capistrano command.
> 
> Run sends that command over to the remote server, over the ssh  
> connection, to  
> run there.
> 
> Ruby code in Cap, however, runs locally. Try running the same command  
> with  
> “system” and see what happens.
> 
> If all you’re wanting to do is programmatically generate a file as you  
> upload  
> it, I seem to remember some Capistrano recipes for that. It’s been  
> awhile,  
> though, and I’m feeling lazy, so ask the Capistrano group.
