Getting hostname using ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting hostname using ssh
# 15  
Old 06-30-2011
Sorry, my mistake!

Code:
scp $host:/tmp/$host*.whatever ...

Or change whatever generates the file to give it a consistent filename.

hostname -s, or hostname -f, for example.

I guess everything depends where the value of $host comes from. A name server? /etc/hosts?

Last edited by Scott; 06-30-2011 at 02:55 PM.. Reason: Pasted same command, corrected now
# 16  
Old 06-30-2011
this is the problem with coronas solution...

Code:
REMOTE_FILE=$(ssh $host 'cat /tmp/$(hostname).html.gz')

Code:
·BÂÙìKH5´/ßcpeæÒ}úôAû·ÞØp¾LLè;ÃLfËIQ>Òszåø±\Ss¤d£
                                               í¼«C»ov{ºí¡étaÔÍÔê}@HxNÁYÇ*¨ÍÖI³
©mr%v4è¶1µ&5mhµ@ñoýÞ
(Ƶ=e-ñM!±ò\qñ®Ð¶´Ö-ü4!EyRÌd':Ëæ_Çpqy?¯ËIð ùì¡ìÅK?Âïï5µq?µü©Õ§¶5¢â³^ϳ§É-Fá©þµµO³¹
×<7òaM{ǧ
¾´ ®Iê»+B¸î6ý´@Sþ]¸÷¾ä¸¾Òewh½#XÕr)Ô<ÕéØ6ÁþlðT AÀyÒRÔÈ<VÒOmÃ"N7e5«Õdµ.çÈ"Ðrêà¥Äå[`ÄYJI*»ww^Æ.Ý%
          S.Ρð±*¿LĪ%üü»Ð}úµ°¾zçrJ>ñâ´H®QÜà2ʯ1ã>,òI +×ã¸mÐÌÃ8mAJüs8)ôµ·i³%M*¹æ¯@Éyíl`é»R¬BùLDDáÏtG"8=áçñµ_h+Å6ѱåXÎÀØ%ôb/[KÃ?xÖ´7R;/iì0°F:8ÌWN*H*«#j^v* øt5Ϲ)ɪ¬Éæ-x¯=¼´By)MòjÜMlpÊCÒ©xHN!ÜÛ¼T(¬0¦Ñ"®è/Kf{XYq!cßÁJ÷ç,öX<c54,9âðÒç ,r~ f<o1¿ÞÇ°¾ks0XînH]¶kf DpY&"m>4ÂtW7ÂØ ñèz¸J,
                                      Ë ]Iµ(^¼êK«`/6öÛ@^lç¹Äo=édlnÔaM8§Ñí;h·æ>ljçùAaì¹øÌÖѧv©¼fºfSÚTôWlRY?YH8ÔVtr{fPÄøïo³¹

# 17  
Old 06-30-2011
That's not a problem.

It just went to the screen instead of to a file.

If you had directed it to a file (also as Corona suggested), everything would be just fine.

Code:
ssh ...... > $host.html.gz

# 18  
Old 06-30-2011
the value of $host comes from a file that has names of servers..ie
server1
server1
server3

this is my whole script
Code:
for host in `cat $PATH_TMP/servers/host_linux2_test`
    do        
        echo "----------------    RUNNING HEALTH CHECK FOR $host   ----------------" >> $PATH_TMP/Linux_cfg2html.log        
        ssh $host "cfg2html-linux -xApo /tmp/" >> $PATH_TMP/Linux_cfg2html.log
        echo "*****************    COMPRESSING FILE in $host   *********************" >> $PATH_TMP/Linux_cfg2html.log
        ssh "$host" '/bin/gzip /tmp/"$(hostname)".html' >> "$PATH_TMP"/Linux_cfg2html.log
        echo "*****************    COPYING FILE TO harp   *********************" >> $PATH_TMP/Linux_cfg2html.log
        scp -p $host:/tmp/"$(hostname)".html.gz $PATH_TMP/servers/Linux/ >> $PATH_TMP/Linux_cfg2html.log
        echo "*****************    REMOVING FILES from $host   *********************" >> $PATH_TMP/Linux_cfg2html.log
        ssh $host "rm -fr /tmp/`hostname`*" >> $PATH_TMP/Linux_cfg2html.log
        echo "----------------------------    DONE    -----------------------------" >> $PATH_TMP/Linux_cfg2html.log
        echo >> $PATH_TMP/Linux_cfg2html.log
    done

and the line i am getting problem is this one...
Code:
scp -p $host:/tmp/"$(hostname)".html.gz $PATH_TMP/servers/Linux/ >> $PATH_TMP/Linux_cfg2html.log

because as corona mentioned, scp cannot execute a command, but my file is named in this syntax "server1.domain.com.html.gz" but if for any reason the vaule of hostname on a server does not contain the domain name, then the file would change.. so i cant put the "domain.com" as a hard value . I hope this explains a bit more.
# 19  
Old 06-30-2011
Have you even tried anything I've suggested? They do what you want, even though that changes every other post.

If your script is getting complicated enough, you might want to avoid doing multiple ssh and scp things per server -- that can take a long time -- by combining it all into one script which gets sent to the server. ssh lets you run entire scripts on the remote server easily enough.

Code:
ssh username@host exec /bin/sh -s arg1 arg2 < local-file.sh

will act like you've ran local-file.sh on the remote host, with the arguments arg1 arg2. If you need to pass strings from the local system to the remote one, the arguments are the place to do it. It will print stream data to stdout as usual, which can be redirected in the ways I've already shown you into a local file, or multiple local files, or a local variable.

Code:
$ cat script.sh
#!/bin/sh

echo Remote hostname is `hostname`
echo Arguments are $*
exit 0
$ ssh username@server exec /bin/sh -s $HOSTNAME qwertyuipo < script.sh
Remote hostname is steward
Arguments are localname qwertyuipo
$

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

Quote:
Originally Posted by eponcedeleonc
the value of $host comes from a file that has names of servers..ie
server1
server1
server3

this is my whole script
Code:
for host in `cat $PATH_TMP/servers/host_linux2_test`

useless use of cat, and useless use of backticks. We've got to discover who keeps teaching this to people.

You also don't need to reopen the logfile n+1 separate times, you can just redirect the entire loop's output once.

Code:
while read host
    do        
        echo "----------------    RUNNING HEALTH CHECK FOR $host   ----------------"        
        ssh $host "cfg2html-linux -xApo /tmp/"
        echo "*****************    COMPRESSING FILE in $host   *********************"
        ssh "$host" '/bin/gzip /tmp/"$(hostname)".html'
        echo "*****************    COPYING FILE TO harp   *********************"
        ssh $host 'cat /tmp/"$(hostname)".html.gz' > $PATH_TMP/servers/Linux/$host.html.gz
        echo "*****************    REMOVING FILES from $host   *********************"
        ssh $host "rm -fr /tmp/`hostname`*"
        echo "----------------------------    DONE    -----------------------------"
        echo
    done < $PATH_TMP/servers/host_linux2_test  >> $PATH_TMP/Linux_cfg2html.log

And as I suspected, you're doing four ssh/scp things per loop which won't break it, but is going to make the script very slow; and causes all the problems you've been struggling with, like the inability to have a local variable on the remote server. I'd suggest a script like this instead:

Code:
while read host
    do
            # Here-document is an alternative to feeding it a script file
            # Everything between <<"EOF" and EOF gets fed into the remote host's shell.
            #
            # the script creates the html, and compresses it to stdout.
            # we take stdout and put it into ${host}-config.gz
            # we take stderr and put it into the log file, >&1
            ssh $host exec /bin/sh -s $HOSTNAME 2>&1 > ${host}-config.html.gz <<"EOF"
                # If you echo anything here, send it into >&2, i.e. stderr
                echo "----------------    RUNNING HEALTH CHECK FOR $HOSTNAME   ----------------" >&2
                echo "----------------    (Remote host is $1)   ----------------" >&2
                # Everything in this block gets sent literally.  $(hostname) is remote.
                cfg2html-linux -xApo /tmp/
                # gzip to standard output, instead of resaving to file.gz
                # standard output goes across the ssh connection and is saved into ${host}-config.html.gz
                /bin/gzip - < /tmp/"$(hostname)".html
                rm -f /tmp/$(hostname).html
# MUST be at the beginning of the line.  Do not indent!
EOF

    done < $PATH_TMP/servers/host_linux2_test  >> $PATH_TMP/Linux_cfg2html.log

Everything red is run only on the remote host. The green highlights how I'm getting the local HOSTNAME into the remote $1. (HOSTNAME is a variable that gets set for you by your shell.)

---------- Post updated at 12:29 PM ---------- Previous update was at 12:10 PM ----------

Okay I'm really done editing it now I thinkSmilie

Last edited by Corona688; 06-30-2011 at 03:28 PM.. Reason: many small improvements.
This User Gave Thanks to Corona688 For This Post:
# 20  
Old 06-30-2011
The idea of waht I want to do is there..
Quote:
run the cfg2html command, zip the file created, copy the file created to local server and erasing the file in the remote server.
However, when I try tu unzip the file (gzip -d ) i am getting this error...
Code:
gzip: scxcdb4-config.html.gz: not in gzip format

# 21  
Old 06-30-2011
Hmm. Maybe your cfg2html command is printing useless chatter that ends up in the gz file? Try hexdump -C corrupted.gz | less to see if there's readable text on the top of it.

If there is, here's how you fix it.

Code:
while read host
    do
            # Here-document is an alternative to feeding it a script file
            # Everything between <<"EOF" and EOF gets fed into the remote host's shell.
            #
            # the script creates the html, and compresses it to stdout.
            # we take stdout and put it into ${host}-config.gz
            # we ignore stderr.
            ssh $host exec /bin/sh -s $HOSTNAME 2>/dev/null > ${host}-config.html.gz <<"EOF"
                # If you echo anything here, send it into >&2, i.e. stderr
                echo "----------------    RUNNING HEALTH CHECK FOR $HOSTNAME   ----------------" >&2
                echo "----------------    (Remote host is $1)   ----------------" >&2
                # Everything in this block gets sent literally.  $(hostname) is remote.
                # redirecting everything in case cfg2html is chattering
                cfg2html-linux -xApo /tmp/ >/dev/null 2>/dev/null
                # gzip to standard output, instead of resaving to file.gz
                # standard output goes across the ssh connection and is saved into ${host}-config.html.gz
                /bin/gzip - < /tmp/"$(hostname)".html
                rm -f /tmp/$(hostname).html
# MUST be at the beginning of the line.  Do not indent!
EOF

    done < $PATH_TMP/servers/host_linux2_test  >> $PATH_TMP/Linux_cfg2html.log

Ignoring stderr up top there may not be necessary but this should cover all the bases. Put it back to 2>&1 once it works so you can get log messages out too.
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