Getting hostname using ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting hostname using ssh
# 1  
Old 06-30-2011
Getting hostname using ssh

I am writing a script and need to get the hostname of the remote server in order to zip that file... the syntax in which the file is named is "hostname.html."
This is the line I am trying to use
Code:
ssh $host "/bin/gzip /tmp/`hostname`.html" >> $PATH_TMP/Linux_cfg2html.log

but whenever I run it `hostname` is replaced for the value in my local server and not the remote server, how can I fix this.
# 2  
Old 06-30-2011
Try this:

Code:
ssh "$host" '/bin/gzip /tmp/"$(hostname)".html' >> "$PATH_TMP"/Linux_cfg2html.log

If you're using the old Bourne shell:

Code:
ssh "$host" '/bin/gzip /tmp/`hostname`.html' >> "$PATH_TMP"/Linux_cfg2html.log

This User Gave Thanks to radoulov For This Post:
# 3  
Old 06-30-2011
excellent, I will consider single quotes next time, thank you!!

---------- Post updated at 12:10 PM ---------- Previous update was at 11:37 AM ----------

what about the syntax for this line...

Code:
scp -p $host:/tmp/`hostname`.html.gz $PATH_TMP/servers/Linux/ >> $PATH_TMP/Linux_cfg2html.log

where I need `hostname` to be the remote hostname, I am getting the same problem of the local value.
Thanks
# 4  
Old 06-30-2011
scp is not a shell, it won't work that way. scp doesn't print files to standard output, either.

There's the old trick of ssh cat filename > localfile though:

Try
Code:
ssh username@host 'cat /tmp/`hostname`.html.gz' > /path/to/local/file

Single quotes are to prevent ` from being evaulated in your local shell. One layer of quotes gets stripped off when you ssh, so it gets it without.

Last edited by Corona688; 06-30-2011 at 02:19 PM..
# 5  
Old 06-30-2011
that is not what i want to do...

I know the name and format of the file i need scp because I am copying that file from the remote server to my local server, that is why I am using scp.

I need the value of "hostname" because that is the name of the file (server1.html)

If not how can I get the hostname of the remote server and store it in a variable so that I can use it locally on my script.
# 6  
Old 06-30-2011
Quote:
Originally Posted by eponcedeleonc
that is not what i want to do...

I know the name and format of the file
No you don't. You know the server knows, but scp doesn't run commands on the server so it can't figure out what `hostname` means. It can't do that because scp is not a shell.
Quote:
i need scp because I am copying that file from the remote server to my local server, that is why I am using scp.
You can copy a file with ssh, using the trick I showed you. cat will spew the file to standard output on the remote host -- which goes into the ssh connection. Travelling across the ssh connection, it returns to your local machine -- where you can redirect it into a local file. This works fine even for binary files.

Code:
ssh username@host 'cat /path/to/remote/file' > /path/to/local/file

You can also do more elaborate piping to transfer multiple files with permissions:

Code:
ssh username@host 'tar -cf - /path/to/remote/files' | tar -xf - -C /path/to/local/dir/

# 7  
Old 06-30-2011
What are you trying to do? You already know the hostname... you use it to connect to the host.

Code:
scp -p $host:/tmp/$host.html.gz ...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Get ip of client, not hostname

Hello i'm trying to get the ip of a telnet session. With who -u I get the hostname of the user connecting to my server, because it checks the reverse DNS. But I only want the ip. Versions: HP-UX HP Release B.11.31 Who command: Hewlett-Packard Company - 4 - HP-UX 11i Version... (2 Replies)
Discussion started by: boltpower
2 Replies

2. UNIX for Advanced & Expert Users

Hostname -f hostname: Unknown host

deleted (0 Replies)
Discussion started by: hce
0 Replies

3. AIX

Hostname for each lpar

Hi Gurus, Can a AIX server with 4 LPARs, each having it's own hostname on the same physical host. Is this possible? Thanks, S (2 Replies)
Discussion started by: svajhala
2 Replies

4. UNIX for Advanced & Expert Users

$HOSTNAME empty while invoking script using SSH

Hi, I am unable to get $HOSTNAME value in a remote script when executed through SSH. Also the scripts hangs and doesnt return to the calling environment. For instance command - ssh user1@box2 "cd /var/home/bin ; ./generateScript.sh" Can any one plese throw light on the issue and... (5 Replies)
Discussion started by: mihirvora16
5 Replies

5. Emergency UNIX and Linux Support

HP UX - ILO Console hostname different than Machine Hostname...

Hi All, So we added a new HP-UX 11.31 machine. Copied OS via Ignite-UX (DVD)over from this machine called machine_a. It was supposed to be named machine_c. And it is when you log in...however when I'm in the ILO console before logging in, it says: It should say: What gives? And how do... (4 Replies)
Discussion started by: zixzix01
4 Replies

6. Solaris

Changed hostname to -a

I was trying to execute the following command ifConfig -a and after a while my hostname changed to -a I checked /etc/hosts and /etc/nodename all seems to be correct. 1. How my hostname changed to -a ? (What i could have done wrong) 2. if you type hostname where does solaris read... (1 Reply)
Discussion started by: 2sb
1 Replies

7. UNIX for Dummies Questions & Answers

Solaris - unknown hostname - how can I change hostname?

Hello, I am new to Solaris. I am using stand alone Solaris 10.0 for test/study purpose and connecting to internet via an ADSL modem which has DHCP server. My Solaris is working on VMWare within winXP. My WinXP and Solaris connects to internet by the same ADSL modem via its DHCP at the same... (1 Reply)
Discussion started by: XNOR
1 Replies

8. UNIX for Advanced & Expert Users

Change of Hostname

Dear All. I will like to know beside the following command "hostname hostname" what other command that can change the hostname of the Unix. Thanks. (4 Replies)
Discussion started by: gelbvonn
4 Replies

9. IP Networking

looking up hostname

Using Solaris 8 (or WINXP). I am trying to look up a specific DNS hostname, but I don't know which DNS server houses that entry. How can I find the hostname? nslookup gives me the following: C:\>nslookup hostname Server: dnsserver Address: x.x.x.x *** dnsserver can't find hostname:... (2 Replies)
Discussion started by: dangral
2 Replies

10. UNIX for Dummies Questions & Answers

Hostname

Hello, I am installing redhat linux 6.2 on an intel based system. Whether i want to know any naming conventions should i follow. ie Any convention to follow to name a linux machine(To give hostname). Simillarly for domain name also. Please suggest in this regard (1 Reply)
Discussion started by: bache_gowda
1 Replies
Login or Register to Ask a Question