Embedding a command with SSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Embedding a command with SSH
# 1  
Old 07-12-2008
Embedding a command with SSH

Hi

I am trying to run a script centrally that will go out and set the network management ip address on all my Sun boxes running Solaris. We have decided that the network management address will be the boxes main IP address but the first octet as a 172 rather than a 10, so for example ifconfig -a will look like this

Code:
[root@a-server # ifconfig -a
lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000
dmfe0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        inet 10.36.14.45 netmask ffff0000 broadcast 10.32.255.255
        ether 0:3:ba:5:a8:1a

the command to set the management IP address locally on these machines would be

Code:
/usr/sfw/bin/ipmitool -I bmc lan set 1 ipaddr 172.36.14.45

so I have written a little script that and will do a sed replace against the output of ifconfig, switch the 10 for a 172 blah blah and throw the output into the ipmitool command listed above, and when run locally on the target machine it works like a dream ...here it is

Code:
/usr/sfw/bin/ipmitool -I bmc lan set 1 ipaddr `ifconfig -a | grep inet | grep -v 127.0.0.1 | head -1 | sed s/^.*inet\ // | sed s/\ netmask.*$// | sed s/^10/172/`


So as I say, rather than running this on every box locally, ive written another script that goes out to all machines (listed in a file called serverlist) using SSH and sets it remotely, the problem is, when I do this, the embedded ifconfig/sed bit seems to run locally on my central servers IP address and not on the target box, so in other words it sets the remote boxes management IP to my central boxes main IP (with the 10 switched for a 172) ....I just cant seem to get it to properly embed and run the command on the target box

here is my script


Code:
#!/bin/ksh

for hostname in `cat /data/serverlist`
do

       echo $hostname
       ssh $hostname "/usr/sfw/bin/ipmitool -I bmc lan set 1 ipaddr `ifconfig -a | grep inet | grep -v 127.0.0.1 | head -1 | sed s/^.*inet\ // | sed s/\ netmask.*$// | sed s/^10/172/`"

done

When run ...

Code:
[root@central-server] # ./configure.sh
a-server
Setting LAN IP Address to 172.1.0.45  << this should be 172.36.14.45

does anybody know why this is happening....ive tried changing the " for a ' but then it stops working altogether

any help would be greatly appreciated
# 2  
Old 07-13-2008
As a guess-
Code:
ssh $hostname \
"ifconfig -a | grep inet | grep -v 127.0.0.1 | head -1 | sed s/^.*inet\ // | sed s/\ netmask.*$// | sed s/^10/172/ " | read value
ssh $hostname "/usr/sfw/bin/ipmitool -I bmc lan set 1 ipaddr $value"

# 3  
Old 07-13-2008
Quote:
Originally Posted by hcclnoodles
...
So as I say, rather than running this on every box locally, ive written another script that goes out to all machines (listed in a file called serverlist) using SSH and sets it remotely, the problem is, when I do this, the embedded ifconfig/sed bit seems to run locally on my central servers IP address and not on the target box, so in other words it sets the remote boxes management IP to my central boxes main IP (with the 10 switched for a 172) ....I just cant seem to get it to properly embed and run the command on the target box
....

The script gets initially parsed by the current shell, then run remotely. To get it properly run in the remote boxes, first you need to escape the special characters, say backquotes `, $ signs,....
For instance from your ifconfig output, using nawk, this code might help:

Code:
while read host
do
echo "$host"
ssh "$host" <<EOF
/usr/sfw/bin/ipmitool -I bmc lan set 1 ipaddr \`ifconfig -a | nawk '/ether/{f=0}f{sub(/10/,"172");print \$2}\$1=="dmfe0:"{f=1}'\`
EOF
done < /data/serverlist

If ifconfig output is different in each box, then the command might need modifications. There are quite a few threads here that deal with this issue.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Embedding JPEG image to the body file

hi, I am trying to embed an image to the body of the email, but the image is not visible. echo "<html> <body> <style> body {background-color:blue} </style> <h1>hello</h1> <center> <img... (1 Reply)
Discussion started by: ATWC
1 Replies

2. AIX

Embedding Runtime Search Path into Library on AIX

My product has 2 libraries say "x & y". x depends on y. During the installation of my products package, user will be prompted for his own location to copy my product libraries. Installation will copy libraries "x & y" and create my product specific ENV variable say "MYPATH" pointing to User... (4 Replies)
Discussion started by: erra_krishna
4 Replies

3. Shell Programming and Scripting

PHP embedding functions inside strings?

How to do a simple shell command like: echo "Today's date is `date +%D`"with PHP? Basically I'm looking to embed PHP library functions calls inside of PHP strings in much the same manner as above. Thanks (7 Replies)
Discussion started by: stevensw
7 Replies

4. UNIX for Advanced & Expert Users

Embedding code into ssh keys

Hi Its been a long time since I worked with ssh keys containing embedded shell commands and cannot remember how it is done. Does anyone know of any sites that have a good tutorial on the subject? I'm not having much luck searching Google for it. Incidentally, searching this forum for the... (6 Replies)
Discussion started by: steadyonabix
6 Replies

5. Shell Programming and Scripting

Difficulty embedding variable within AWK

Hi, I am working on a parsing script but cannot figure out how to accomplish this. Here is a simplified version of the script: #!/bin/bash DS=$1 DS=`expr $DS \* 2` DS=`expr $DS + 7` cat $FILENAME | awk '/<row><v> +/' | awk '{printf("%.0f %.0f\n", $6, $9)}' The problem is that I want the... (2 Replies)
Discussion started by: Nisrak
2 Replies

6. Shell Programming and Scripting

Need help embedding Unix commands in a shell script

Hi Folks, I have a text file which may or may not have any data. I would like to email the file, via a Korn shell script, if the file is not empty. I am fiddling around with the wc -l command, but no luck so far. The pseudo code is as follows count=`wc -l test.txt` if cat test.txt... (4 Replies)
Discussion started by: rogers42
4 Replies

7. Shell Programming and Scripting

Embedding HTML in Perl script

My webpage is hosted from perlscript(homepage.pl), i want to add piece of html code in the footer of the homepage. I simply pasted the html code at the end of the perl script as below... ======================================================== close(OUTSQL); ... (4 Replies)
Discussion started by: paventhan
4 Replies

8. UNIX Desktop Questions & Answers

Embedding file output into a script

Hello. I found a Unix script on this site that calculates a date that is 2 months earlier from today. I'm using that script and writing the value to a file called 2monthsago.txt. I want to use that value in another script. Below is my attempt at doing that and the results. My Script: ... (1 Reply)
Discussion started by: Colel2
1 Replies

9. Programming

Embedding xnest in C code

I hope I am posting this in the right section. I have c file that is using the motif GUI toolkit to draw widgets and things of that sort. I also have another program that runs with xnest. I need to figure out a way to place that xnest program in my c code so that it exists in the window that the... (4 Replies)
Discussion started by: lesnaubr
4 Replies

10. Shell Programming and Scripting

Embedding Perl construct in ksh...

Hi, I have an embedded Perl construct in a korn script. However, I cannot seem to access the shell variables that were declared outside this Perl section. This is how my script is written....I have also tried back-ticks where I assign the shell variable to my local perl variable, still... (1 Reply)
Discussion started by: svetlur
1 Replies
Login or Register to Ask a Question