Dan Kaib
asked on
Expect script causing Invalid Switch message
I have a script that works on a local system.
When I try to execute that script from an expect script using ssh on another system, I get an Invalid Switch message on the following statement:
sort -ru oldfile > newfile
The spawn statement from the expect script is as follows:
spawn ssh "$USER@$IP" "$CMD" "$PARAMS"
This is running from Cygwin.
Is there something in expect or ssh that is preventing the switches on remote command execution to be recognized?
Any suggestions would be greatly appreciated.
TIA,
Dan
When I try to execute that script from an expect script using ssh on another system, I get an Invalid Switch message on the following statement:
sort -ru oldfile > newfile
The spawn statement from the expect script is as follows:
spawn ssh "$USER@$IP" "$CMD" "$PARAMS"
This is running from Cygwin.
Is there something in expect or ssh that is preventing the switches on remote command execution to be recognized?
Any suggestions would be greatly appreciated.
TIA,
Dan
your problem is very probbaly an escaping issue.
let's assume the command you type on your localhost is
sort -ru oldfile > newfile
if you want to run this command via ssh you had to type
ssh user@host "sort -ru oldfile > newfile" # assuming newfile should be created on the remote host
then if you use tcl / expect
spawn ssh "user@host" "\"sort -ru oldfile > newfile\""
let's assume the command you type on your localhost is
sort -ru oldfile > newfile
if you want to run this command via ssh you had to type
ssh user@host "sort -ru oldfile > newfile" # assuming newfile should be created on the remote host
then if you use tcl / expect
spawn ssh "user@host" "\"sort -ru oldfile > newfile\""
ASKER
Hi gelonida,
Thanks for the reply.
Attached are the 3 scripts involed:
Script 1 is run_on_corp.sh - This is run locally from the managers workstation, it executes Script 2 run_ssh.
Script 2 is run_ssh - This is an expect script that issues the SSH command to execute script copy_from_reg.sh on the remote corporate system.
Script 3 is copy_from_reg.sh - This script goes out to different store registers and downloads files via scp from the register to the corporate system.
This is done from the managers workstation because there are over 100 locations and the corporate system could not process the 100 locations in a timely manner if it ran them individually. Plus the 100 locations end there day at different time.
Script 3 copy_from_reg.sh has the sort command that is not working in it.
Thanks,
Dan
copy-from-reg.sh
run-on-corp.sh
run-ssh
Thanks for the reply.
Attached are the 3 scripts involed:
Script 1 is run_on_corp.sh - This is run locally from the managers workstation, it executes Script 2 run_ssh.
Script 2 is run_ssh - This is an expect script that issues the SSH command to execute script copy_from_reg.sh on the remote corporate system.
Script 3 is copy_from_reg.sh - This script goes out to different store registers and downloads files via scp from the register to the corporate system.
This is done from the managers workstation because there are over 100 locations and the corporate system could not process the 100 locations in a timely manner if it ran them individually. Plus the 100 locations end there day at different time.
Script 3 copy_from_reg.sh has the sort command that is not working in it.
Thanks,
Dan
copy-from-reg.sh
run-on-corp.sh
run-ssh
as said in my second post probably an escaping issue.
Very first comment about
copy-from-reg.sh
lwithout knowing the contents of the variables
$datemp and $dalist
I would write
sort -ru "${datemp}" > "${dalist}"
I do not have time to dive through your entire code (it's not really a small example)
I'd suggest to either simplfy your output or to try to give some more info:
For Example:
In line 80 of run-ssh you have
spawn ssh "$USER@$IP" "$CMD" "$PARAMS"
could you please add these above the spawn line
puts "USER <$USER>"
puts "PASS <$PASS>"
puts "IP <$IP>"
puts "CMD <$CMD>"
puts "PARAMS <$PARAMS>"
and then tell me its output (DON'T POST OF COURSE USERNAME AND PASSWORD).
That's just for you for sanity checking
in run_on_corp you have in line 101
${sshcmd} ${ftpuser} ${ftppass} ${ftpip} "$ftpcmd" ${params}
This line does not look good. as you might have blank characters in the password.
I would replace it with
${sshcmd} "${ftpuser}" "${ftppass}" "${ftpip}" "$ftpcmd" "${params}"
could you then please add following lines above
echo "sshcmd <$sshcmd>"
echo "ftpuser <$ftpuser>" # i'm not interested in it's exact output it's enough you tell me
# it's really the user name and whether any special characters are in the string
echo "ftppass <$ftppass>" # same as for line above
echo "ftpip <$ftpip>" # #
echo "ftpcmd <$ftpcmd>"
echo "params <$params>"
Just check whether all values are passed as expected or whether anything looks different in the two scripts
in copyfromreg I don't understand lines 31 / 32
I'm not sure, but I would have written them
sshcmd="bash -c \"cd ${frdir}"
sshcmd="${sshcmd};cp /dev/null ${livfl}"
sshcmd="${sshcmd};ls --time-style=+%Y%m%d_%H%M%
instead of
sshcmd="bash -c \"cd ${frdir}"
sshcmd=${sshcmd}";cp /dev/null ${livfl}"
sshcmd=${sshcmd}";ls --time-style=+%Y%m%d_%H%M%
the difference is the initial double quote in the second and third line.
ame in line 35
I would also add
echo "<$sshcmd>" in line 232 to be sure, this is really what you want to have as ssh command
I can't see recv_ssh . without knowing escaping issues might also be there.
ASKER
copy-from-reg.sh
lwithout knowing the contents of the variables
$datemp=/cygdrive/x/netsha res/fiscal _files/tex t2pdf/st00 44/work05/ datetemp.t xt
$dalist=/cygdrive/x/netsha res/fiscal _files/tex t2pdf/st00 44/work05/ datelist.t xt
With the sort command escaped in copy_from_reg.sh as follows:
"\"sort -ru ${datemp} > ${dalist}\""
Below is the error message:
/cygdrive/x/netshares/fisc al_files/b in/copy_fr om_reg.sh: line 254: "sort -ru /cygdrive/x/netshares/fisc al_files/t ext2pdf/st 0044/work0 5/datetemp .txt > /cygdrive/x/netshares/fisc al_files/t ext2pdf/st 0044/work0 5/datelist .txt": No such file or directory
With the sort command escaped in copy_from_reg.sh as follows:
\"sort -ru ${datemp} > ${dalist}\"
Below is the error message:
/cygdrive/x/netshares/fisc al_files/b in/copy_fr om_reg.sh: line 254: "sort: command not found
Plus it created the following file with the double quote are part of the filename.
-rw-r--r--+ 1 Administrator None 0 2010-07-02 13:54 datelist.txt"
With the sort command escaped in copy_from_reg.sh as follows:
sort -ru "${datemp}" > "${dalist}"
Below is the error message:
Invalid switch.
========================== ========== ========== =====
run_ssh produces the following output:
USER <xxxxxxx> (This contained the correct information)
PASS <ppppppp> (This contained the correct information)
IP <gmfp1>
CMD </cygdrive/x/netshares/fis cal_files/ bin/copy_f rom_reg.sh >
PARAMS <v.0044>
Below is the variable ouput for the spawn command:
spawn ssh xxxxxxx@gmfp1/cygdrive/x/n etshares/f iscal_file s/bin/copy _from_reg. sh v.0044
========================== ========== ========== =====
With your changes made to run_on_corp the following output results:
sshcmd </home/Administrator/bin/r un_ssh>
ftpuser <xxxxxxx>
ftppass <ppppppp!>
ftpip <gmfp1>
ftpcmd </cygdrive/x/netshares/fis cal_files/ bin/copy_f rom_reg.sh >
params <v.0044>
I'm going to reduce the scripts to a bare minimum of commands to make it easier to follow.
I'll add a new comment shortly.
lwithout knowing the contents of the variables
$datemp=/cygdrive/x/netsha
$dalist=/cygdrive/x/netsha
With the sort command escaped in copy_from_reg.sh as follows:
"\"sort -ru ${datemp} > ${dalist}\""
Below is the error message:
/cygdrive/x/netshares/fisc
With the sort command escaped in copy_from_reg.sh as follows:
\"sort -ru ${datemp} > ${dalist}\"
Below is the error message:
/cygdrive/x/netshares/fisc
Plus it created the following file with the double quote are part of the filename.
-rw-r--r--+ 1 Administrator None 0 2010-07-02 13:54 datelist.txt"
With the sort command escaped in copy_from_reg.sh as follows:
sort -ru "${datemp}" > "${dalist}"
Below is the error message:
Invalid switch.
==========================
run_ssh produces the following output:
USER <xxxxxxx> (This contained the correct information)
PASS <ppppppp> (This contained the correct information)
IP <gmfp1>
CMD </cygdrive/x/netshares/fis
PARAMS <v.0044>
Below is the variable ouput for the spawn command:
spawn ssh xxxxxxx@gmfp1/cygdrive/x/n
==========================
With your changes made to run_on_corp the following output results:
sshcmd </home/Administrator/bin/r
ftpuser <xxxxxxx>
ftppass <ppppppp!>
ftpip <gmfp1>
ftpcmd </cygdrive/x/netshares/fis
params <v.0044>
I'm going to reduce the scripts to a bare minimum of commands to make it easier to follow.
I'll add a new comment shortly.
ASKER
I renamed run_on_corp.sh to scr1.sh
I renamed copy_from_reg.sh to scr3.sh
I eliminated the expect script run_ssh all together for this test and incorporated the ssh command directly in scr1.sh
This should be easier to follow.
scr1.sh
I renamed copy_from_reg.sh to scr3.sh
I eliminated the expect script run_ssh all together for this test and incorporated the ssh command directly in scr1.sh
This should be easier to follow.
scr1.sh
ASKER
Forgot to add scr3.sh on last comment.
scr3.sh
scr3.sh
ASKER
I just manually executed the following command locally on the system that contains scr3.sh
$ ssh Administrator@gmfp1 /cygdrive/x/netshares/fisc al_files/b in/scr3.sh v.0044
Administrator@gmfp1's password:
Sorting file /cygdrive/x/netshares/fisc al_files/t ext2pdf/st 0044/work0 5/datetemp .txt
Invalid switch.
So this is SSHing to itself and getting the same errors escaped or not.
$ ssh Administrator@gmfp1 /cygdrive/x/netshares/fisc
Administrator@gmfp1's password:
Sorting file /cygdrive/x/netshares/fisc
Invalid switch.
So this is SSHing to itself and getting the same errors escaped or not.
ASKER
I find this hard to believe, given the error message of Invalid switch but if I change the copy_from_reg.sh script to:
/usr/bin/sort -ru "${datemp}" > "${dalist}"
It works perfectly.
I would have expected to see command not found or something of that nature as the error message not Invalid switch.
Thanks for all of your help, I do appreciate it.
Dan
/usr/bin/sort -ru "${datemp}" > "${dalist}"
It works perfectly.
I would have expected to see command not found or something of that nature as the error message not Invalid switch.
Thanks for all of your help, I do appreciate it.
Dan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Neither do:
${datemp} =/cygdrive/x/netshares/fis cal_files/ text2pdf/s t0044/work 05/datetem p.txt
${dalist}=/cygdrive/x/nets hares/fisc al_files/t ext2pdf/st 0044/work0 5/datelist .txt
Changing from: sort -ru "${datemp}" > "${dalist}"
to: /usr/bin/sort -ru "${datemp}" > "${dalist}"
Made this work.
The Invalid switch error message is misleading.
Thanks for your help,
Dan
${datemp} =/cygdrive/x/netshares/fis
${dalist}=/cygdrive/x/nets
Changing from: sort -ru "${datemp}" > "${dalist}"
to: /usr/bin/sort -ru "${datemp}" > "${dalist}"
Made this work.
The Invalid switch error message is misleading.
Thanks for your help,
Dan
for example.
what did you set CMD to
and what did you set PARAMS to?