LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scripting issue: Reading a file from a remote server (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-issue-reading-a-file-from-a-remote-server-838277/)

cheltz 10-15-2010 08:59 AM

Scripting issue: Reading a file from a remote server
 
I have an situation where I want to read a file into my script that lie on a remote server. I have ssh-keys set up just don't know how to do this.

Thanks if you respond.

TB0ne 10-15-2010 09:05 AM

Quote:

Originally Posted by cheltz (Post 4128511)
I have an situation where I want to read a file into my script that lie on a remote server. I have ssh-keys set up just don't know how to do this.

Thanks if you respond.

Well, post what you've written so far, and we can help. Basic steps are:
  • Connect to remote server
  • Either copy the file to your system locally, or run a "cat" (or other), command to read the file into a variable on the fly.
  • Process according to your wishes

There are MANY bash scripting tutorials you can find on Google, if you're just starting out.

cheltz 10-15-2010 09:32 AM

I tried to ssh to the remote server and cat the file, but when i turned on the trace (set -x) it would sit at the $ prompt of the remote server and the rest of the script did not run.

TB0ne 10-15-2010 09:46 AM

Quote:

Originally Posted by cheltz (Post 4128553)
I tried to ssh to the remote server and cat the file, but when i turned on the trace (set -x) it would sit at the $ prompt of the remote server and the rest of the script did not run.

Ok..again, post what you've written. And have you checked out the scripting tutorials???
http://tldp.org/LDP/abs/html/

Something to try is
Code:

variable = `ssh <user>@<host> "<command string goes here>"`
Those are backticks, not single-quotes. That runs the command, and shoves it into a variable. So if you put "cat myfile.txt", you'll get the ouput of myfile.txt into that variable.

ckoniecny 10-15-2010 02:09 PM

You can set up SMB shares and use "smbmount" to mount the share across the network. That would let you easily read a file on another server.

Or you can look into using the "rcp" command.

joec@home 10-15-2010 11:01 PM

I had a project where I wanted to run the same script on multiple servers from a single point. This became a small application that I am not at liberty to share, but at the heart of it was the following command:

cat remote_script | sshpass -p`echo $PASSWORD` ssh root@$LIST bash

So remote_script would be the script I wanted to run. The catch 22 was the script had to use Java style carriage returns in the script and end with an exit command.

Example

Code:

#!/bin/bash
hostname
hostname -a
uname -a
ls -la

The same code would instead look like

Code:

hostname ;\
hostname -a ;\
uname -a ;\
ls -la ;\
exit


cheltz 10-18-2010 04:33 PM

TB0ne's ssh string was what i was looking for. What had me hung up was that there's no colon after the ip address.

Thanks all.

cheltz 10-18-2010 04:34 PM

I cannot do shares by the way they are forbidden for security.


All times are GMT -5. The time now is 05:22 PM.