Visit Our UNIX and Linux User Community

Cat a file across ssh?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cat a file across ssh?
# 1  
Old 02-12-2013
Question Cat a file across ssh?

There is a file on a remote host that I want to read across an ssh tunnel. NOT copy... scp won't work here. And this file requires elevated permissions to read.

'ssh -t remotehost sudo cat /path/to/file' will prompt me for my sudo password and read out the file. But I'm coming up blank for a way to capture that output. I really just need to grep and cut and sed my way through the output, so it doesn't matter how the content is available locally... writing to a temporary file is fine, or just streaming it across. But requiring the password to be entered puts the kybosh on every way of piping or redirecting the output I can think of. I can probably use a tar pipe to get it across, but it seems there ought to be an easier way to do this.
# 2  
Old 02-12-2013
Actually, the password prompt shouldn't care about redirection, since it goes to the terminal, not stdout specifically.

Ordinary shell redirection should work, I think.

ssh -t -t remotehost sudo cat /path/to/file > /path/to/localfile

# 3  
Old 02-12-2013
Suppose you could try writing it to stderr:

ssh -t -t remotehost 'sudo cat /path/to/file >&2' 2> /path/to/localfile

# 4  
Old 02-12-2013
I cheated a little bit... Smilie

ssh -t $host 'sudo -v'

And then I was able to read my file with sudo but without the password.
This User Gave Thanks to jnojr For This Post:
# 5  
Old 02-12-2013
Be carefull that sudo isn't just using cached credentials, you may find that if you wait 5 mins sudo will require a password (unless you are allowing passwordless sudo for this user/command combo)

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue in executing cat (remote ssh)

Hi, I need to ssh remotely to a machine and cat a file assign the value to a variable Script: #!/bin/bash -x value=`cat config.txt` echo "$value" ssh "valu='cat /export/home/test.md5'; echo "$valu"" | tee Execution: $ ./x ++ cat config.txt + value='touch me' +... (5 Replies)
Discussion started by: close2jay
5 Replies

2. Shell Programming and Scripting

Ssh cat file output into a file on local computer

Hello, I'm on a remote computer by SSH. How can I get the output of "cat file" into a file on the local computer? I cannot use scp, because it's blocked. something like: ssh root@remote_maschine "cat /file" > /locale_machine/file :rolleyes: (2 Replies)
Discussion started by: borsti007
2 Replies

3. Shell Programming and Scripting

Check if file exists via ssh in ssh (nested)

I'm using redhat and have an odd issue with a nested ssh call. ssh -i ~/.ssh/transfer-key -q transfer@fserver1 ] && ssh -i ~/.ssh/transfer-key transfer@fserver1 "ssh -i ~/.ssh/sftp-key sftpin@ ]" && ssh -i ~/.ssh/transfer-key transfer@fserver1 "scp -i ~/.ssh/sftp-key /home/S/outbox/*... (2 Replies)
Discussion started by: say170
2 Replies

4. UNIX for Dummies Questions & Answers

How to cat via ssh and sed at the same time.?

I am trying to write a script to automatically create conf files and remote servers. I would like to do all this without creating files locally and copying them . Here is what I have tried. sitename=$1 prodserver=$2 ssh $prodserver "cat > /data/$sitename.conf" << cat |sed... (5 Replies)
Discussion started by: macrossm
5 Replies

5. Shell Programming and Scripting

Cat file

how to cat a file by ignoring first line and last line (1 Reply)
Discussion started by: thelakbe
1 Replies

6. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

7. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

8. UNIX for Dummies Questions & Answers

How to cat file

I want to cat a file with only show the line contain '/bin/bash' but don't show the line contain 'load' (don't show if the line contain 'load' and '/bin/bash' together), how to type in the command? thk a lot! (2 Replies)
Discussion started by: zp523444
2 Replies

9. UNIX for Dummies Questions & Answers

Easiest way to cat out first 100 lines of a file into a different file?

Not sure how to do this exactly.. just want to take the first 100 lines of a file and cat it out into a second file. I know I can do a more on a file and > it into a different file, but how can I make it so only the first 100 lines get moved over? (1 Reply)
Discussion started by: LordJezo
1 Replies

Featured Tech Videos