gavioto / parallel-ssh

Automatically exported from code.google.com/p/parallel-ssh
Other
1 stars 1 forks source link

Pass host name/ip to which pssh is currently connecting to the command being executed by pssh? #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi!  Love pssh.  It would be great if I could pass the host name/IP as a 
variable to the command being passed, something to the effect of:

pssh -h pssh_hosts.txt -l root -v -o out -e err "sed -i 
's/foobar.example.com/$PSSH_HOST/g' /etc/apticron/apticron.conf"

Example:

I used pssh recently to install apticron.  apticron runs 'apt-get update' once 
a day and emails a list of packages which have updates to the email address 
specified in its configuration file /etc/apticron/apticron.conf

pssh allowed me to easily set the email address I'd like to apticron to send 
those daily reports (sed -i ...).

However, there is also a value which one also can set in the configuration file 
for the name of the server.  If this value is not set, the server will identify 
itself by 'hostname -f'.  I would prefer pass in the sed command to insert the 
name of the host to which pssh is connecting.  

Your help would be greatly appreciated.

Thanks,

Phil

Original issue reported on code.google.com by phi...@armorgames.com on 19 Jan 2012 at 10:34

GoogleCodeExporter commented 9 years ago
In general, it seems that this information should be available on the host.  
For example, would the following command be suitable?

pssh -h pssh_hosts.txt -l root -v -o out -e err 'sed -i 
"s/foobar.example.com/$(hostname -s)/g" /etc/apticron/apticron.conf'

Or is the output of `hostname -s` different than the name to which pssh is 
connecting?

Original comment by amcna...@gmail.com on 19 Jan 2012 at 11:16

GoogleCodeExporter commented 9 years ago
We're using EC2, so hostname -s and hostname -f both show the internal IP.  
We'd rather show the external IP or external FQDN.

Original comment by phi...@armorgames.com on 19 Jan 2012 at 11:19

GoogleCodeExporter commented 9 years ago
That makes sense. So, it turns out that this is a terrifically easy feature to 
add. Would you mind testing the attached patch? If it works, I'll commit it 
right away.

Thanks.

Original comment by amcna...@gmail.com on 19 Jan 2012 at 11:25

Attachments:

GoogleCodeExporter commented 9 years ago
I made the change, deleted /usr/local/bin/pssh, and re-ran the install command, 
then tried something simple:
pssh -h pssh_hosts.txt -l root -v -o out -e err "echo $PSSH_HOST"

Ran it, and the response was a series of blank lines.

Ran:
pssh -h pssh_hosts.txt -l root -v -o out -e err "set | grep PSSH"

Response was a series of lines, all the same:
BASH_EXECUTION_STRING='set | grep PSSH'

(Which means the PSSH_NODENUM is not being created as an environment variable 
on the connected-to-host either...)

Ran:
pssh -h pssh_hosts.txt -l root -v -o out -e err "set | grep BASH"

Output was the following repeated:
BASH=/bin/bash
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progco
mp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_EXECUTION_STRING='set | grep BASH'
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="5" [3]="1" [4]="release" 
[5]="x86_64-pc-linux-gnu")
BASH_VERSION='4.1.5(1)-release'
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_EXECUTION_STRING='set | grep BASH'
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="39" [3]="1" [4]="release" 
[5]="i486-pc-linux-gnu")
BASH_VERSION='3.2.39(1)-release'

(which means that the 'set | grep' command does work)

as does:
pssh -h AllEC2Hosts.txt -l root -v -o out -e err "echo $BASH"

Result, repeated several times:
/bin/bash

(so the 'echo' command also works)

Did you try the same patch on your configuration?  Why would mine fail and your 
succeed?  We use Debian 5 & 6.

Original comment by phi...@armorgames.com on 20 Jan 2012 at 2:01

GoogleCodeExporter commented 9 years ago
You have a few interesting things going on here.

1) pssh -h pssh_hosts.txt -l root -v -o out -e err "echo $PSSH_HOST"

In this case, the PSSH_HOST environment variable is being evaluated on your 
local machine rather than the remote host. If you change the double quotes to 
single quotes, it will make it evaluate on the remote host.

2) After actually testing this change myself, I noticed that I had omitted an 
important change.  In bin/pssh, the line with "SendEnv" should be changed to 
"'-o', 'SendEnv=PSSH_NODENUM PSSH_HOST']". You can either change this by hand, 
or you can grab commit 6631acf, which contains both of the changes associated 
with this issue. My apologies for not taking the time to test the change 
earlier.

3) I believe that you are also running into a problem with sshd trimming down 
the environment on the remote host. In my sshd_config, I have "AcceptEnv 
PSSH_NODENUM PSSH_HOST", which instructs sshd to allow these variables to pass 
through. I will add a comment about this to the pssh.1 man page.

Thanks for your patience and your thorough report which helped me to track 
these issues down very quickly.

Can you confirm that 

Original comment by amcna...@gmail.com on 23 Jan 2012 at 5:04

GoogleCodeExporter commented 9 years ago
Sorry, my post cut off at the end.

Can you confirm that the feature works after making the change to bin/ssh, 
updating the sshd_config, and making sure to use single quotes?

Thanks.

Original comment by amcna...@gmail.com on 23 Jan 2012 at 5:32

GoogleCodeExporter commented 9 years ago
Success!  Using (1) single quotes, (2) git pull, (3) modified sshd_config, I 
was able to echo back $PSSH_HOST.

Thanks!

Original comment by phi...@armorgames.com on 23 Jan 2012 at 5:53

GoogleCodeExporter commented 9 years ago
Great. I'll mark this as done, and it will end up in the next release of pssh. 
Thanks for your participation and help.

Original comment by amcna...@gmail.com on 23 Jan 2012 at 6:36