|
|
 |
 |
 |
 |
TCL(Tool Command Language) Scripting
|
 |
 |
 |
 |
 |
 |
 |
 |
SSH to remote system, su to root, then execute cmds from external file
I'm having some problems trying to pass a file through expect which contain [ or # characters. Is there any significant functional difference in the 2 methods below? Any reason to use one over the other? Ultimately what I'm trying to do is SSH into a remote system, su to root, then execute the commands found in the file as root. The file of external commands included comments and some control structures such as: if [[ "$var" = "today" ]]; then echo "blah blah blah; fi thus far I have not been able to get anything containing [ or [[ to work. Instead i've had to resort to something like this: echo $var | grep today && echo "blah blah blah". As you can imagine this only works for simple cases and is limiting what can be accomplished easily... For some reason the file of external commands is getting messed up when using method 2 below. Any help would be greatly appreciated. --------------------- Method 1 --------------------- set RemoteCommandsFile [lindex $argv 0] set FILE [open $RemoteCommandsFile r] while {[gets $FILE Line] != -1} { puts "$Line\r" }
--------------------- Method 2 --------------------- set RemoteCommandsFile [lindex $argv 0] send "[exec cat $RemoteCommandsFile]\r" Regards, -Inet
In article <1179284815.144261.91@k79g2000hse.googlegroups.com>, inetquestion <inetquest @hotmail.com> wrote: >I'm having some problems trying to pass a file through expect which >contain [ or # characters. Is there any significant functional . . . Briefly, you're working too hard. You'll probably want to read <URL: http://wiki.tcl.tk/2958 >, <URL: http://wiki.tcl.tk/3178 >, and <URL: http://expect.nist.gov/example/ftp-inband >. If those don't make answers to all your questions evident in the next fourteen hours or so, at least one of us will go through your original post in more detail.
>From the examples I see there isn't a similar case to what I'm having
problems with. Below is the procedure i'm using now. Unlike the examples, in my case $FILE is located on the system which initiates the SSH, not the target system. proc LoginAndExecute {SERVER PASS FILE} { system stty -echo spawn ssh -t $SERVER ". ~/.profile; su -" expect { -re "assword.*$" {send "$PASS\r"} } expect { -re ".*$SERVER.*$" { send "[exec cat $FILE]\r" } } send "exit\r" interact system stty echo
}
At 2007-05-15 11:06PM, "inetquestion" wrote:
> I'm having some problems trying to pass a file through expect which > contain [ or # characters. Is there any significant functional > difference in the 2 methods below? > --------------------- > Method 1 > --------------------- > set RemoteCommandsFile [lindex $argv 0] > set FILE [open $RemoteCommandsFile r] > while {[gets $FILE Line] != -1} { > puts "$Line\r" > } > --------------------- > Method 2 > --------------------- > set RemoteCommandsFile [lindex $argv 0] > send "[exec cat $RemoteCommandsFile]\r"
Yes. In (1) you add a carriage return after each line in the file. In (2) you only add a single carriage return after the last line of the file. > Any reason to use one over the > other? Ultimately what I'm trying to do is SSH into a remote system, > su to root, then execute the commands found in the file as root. The
set fid [open [lindex $argv 0] r] while {[gets $fid line] != -1} { exp_send "$line\r" } > thus far I have not been able to get anything containing [ or [[ to > work.
How does it not work? Can you show us what happens? -- Glenn Jackman "You can only be young once. But you can always be immature." -- Dave Barry
This is the local file, whose contents I want to execute on the remote server: if [[ "$LOGNAME" = root ]]; then echo "ROOT" else echo "someone else: $LOGNAME" fi ----------------------------------- When running this through expect procedure this is the output on the remote system: spawn ssh -t serverabcd . ~/.profile; su - Password: serverabcd# if [[ "$LOGNAME" = root ]]; then echo "ROOT" else echo "someone else: $LOGNAME" fi exit > > > > [[: not found
someone else: root serverabcd# Connection to serverabcd closed. ------------------------- I should probably mention I am very new to working with expect, but familiar with scripting in general. This seems like it should be pretty simple, but the nuances of exptect/tcl are throwing me for a bit of a loop. Your assistance much appreciated! -Inet
At 2007-05-16 02:14PM, "inetquestion" wrote:
> This is the local file, whose contents I want to execute on the remote > server: > if [[ "$LOGNAME" = root ]]; then > echo "ROOT" > else > echo "someone else: $LOGNAME" > fi > ----------------------------------- > When running this through expect procedure this is the output on the > remote system: > spawn ssh -t serverabcd . ~/.profile; su - > Password: > serverabcd# if [[ "$LOGNAME" = root ]]; then > echo "ROOT" > else > echo "someone else: $LOGNAME" > fi > exit > > > > > [[: not found
I think this error has nothing to do with expect, but with root's shell. I bet root's shell is /sbin/sh or some shell that does not know about [[ Try using single brackets instead. -- Glenn Jackman "You can only be young once. But you can always be immature." -- Dave Barry
Absolutley! forgot [[ will work in KSH, but isn't standardized. [ works just fine as you suggested The following statements cause strange output as well when in the input file. Does it need to have < or >> excaped possibly? cat >>EOM blabla blabla EOM cat somefile.txt >> output.txt
At 2007-05-16 10:05PM, "inetquestion" wrote: > The following statements cause strange output as well when in the > input file. Does it need to have < or >> excaped possibly? > cat >>EOM > blabla > blabla > EOM > cat somefile.txt >> output.txt
Again, you'll need to show us some output. As you're simply reading lines from a file and passing the strings to exp_send, expect/tcl shouldn't be getting in your way. So show us your expect script, your data and your output and we'll see what we can do. Remember, when you're reporting a problem, you really can't provide too many details. -- Glenn Jackman "You can only be young once. But you can always be immature." -- Dave Barry
On May 17, 4:05 am, inetquestion <inetquest@hotmail.com> wrote: > The following statements cause strange output as well when in the > input file. Does it need to have < or >> excaped possibly? > cat >>EOM > blabla > blabla > EOM
It's a bad day for your Shell practice ;-) I think you mean cat <<EOM instead of cat >>EOM -Alex
yeah... I practice trial/error shell programming :) Any reason why the << or >> get garbled when passing through expect?
On May 18, 3:26 am, inetquestion <inetquest@hotmail.com> wrote: > yeah... I practice trial/error shell programming :) > Any reason why the << or >> get garbled when passing through expect?
So far you provided no evidence that they are. Again, if you don't tell us more than "... produce strange output", good luck and fare well. -Alex
|
 |
 |
 |
 |
|