Execute a local script against a remote server

I am unable to run the below script against a remote server due to syntax error (then unexpected), but i am able to run it locally. Am i executing it correctly or is there any other way to execute it.

ssh username@servernname ksh -s < scriptname
#!/bin/ksh
function record
{
((end = $(date +%s) + $second))
while [ $(date +%s) -lt $end ]
do
echo $end
echo $(date +%s)
(ps aux|head -10 && echo " " ) >> testing.txt
sleep 1
done
}
echo " This script displays cpu utilization of currently running processes on the remote server and record the the data for a period if required"
op=`ps aux | head -10`
echo " "
sleep 2
echo " if you want to record the cpu utilization for specific time period press y else press enter "
read ip
if [[ "$ip" == "y" ]]
then
echo " enter the time to be recorded in seconds eg:5"
read second
echo " cpu utilization is being recorded at ~/cpuutilreport.txt, open the file after `expr 10 + $second ` seconds"
sleep 2
echo "$op"
record
sleep 1
else
echo "cpu utilization of currently running processes (top 10 cpu consuming only)"
echo " "
sleep 2
echo "$op"
fi

Hi ,

It seems to be problem in getting input through ssh. You can copy the script file to remote location and call through ssh. Other wise pass number of seconds as argument.

ssh username@servernname "ksh /temp/scriptname.sh"

somehow it is still not working

i am still getting the error 0403-057 Syntax error at line 22 : `then' is not expected.

Try this:

ssh username@servernname ksh -s <<EOF
#!/bin/ksh
function record
{
((end = $(date +%s) + $second))
while [ $(date +%s) -lt $end ]
do
echo $end
echo $(date +%s)
(ps aux|head -10 && echo " " ) >> testing.txt
sleep 1
done
}
echo " This script displays cpu utilization of currently running processes on the remote server and record the the data for a period if required"
op=`ps aux | head -10`
echo " "
sleep 2
echo " if you want to record the cpu utilization for specific time period press y else press enter "
read ip
if [[ "$ip" == "y" ]]
then
echo " enter the time to be recorded in seconds eg:5"
read second
echo " cpu utilization is being recorded at ~/cpuutilreport.txt, open the file after `expr 10 + $second ` seconds"
sleep 2
echo "$op"
record
sleep 1
else
echo "cpu utilization of currently running processes (top 10 cpu consuming only)"
echo " "
sleep 2
echo "$op"
fi
EOF

seems the read make the trouble.

replaced by:

ip=$1
second=$2

then run the command with IP_adress and Second as input directly.

ssh username@servernname ksh -s < scriptname 10.1.1.1  234
#!/bin/ksh
function record
{
((end = $(date +%s) + $second))
while [ $(date +%s) -lt $end ]
do
echo $end
echo $(date +%s)
(ps aux|head -10 && echo " " ) >> testing.txt
sleep 1
done
}
echo " This script displays cpu utilization of currently running processes on the remote server and record the the data for a period if required"
op=`ps aux | head -10`
echo " "
sleep 2
echo " if you want to record the cpu utilization for specific time period press y else press enter "
ip=$1
if [[ "$ip" == "y" ]]
then
echo " enter the time to be recorded in seconds eg:5"
second=$2
echo " cpu utilization is being recorded at ~/cpuutilreport.txt, open the file after `expr 10 + $second ` seconds"
sleep 2
echo "$op"
record
sleep 1
else
echo "cpu utilization of currently running processes (top 10 cpu consuming only)"
echo " "
sleep 2
echo "$op"
fi

you are trying to run an interactive script non-interactively ...

echo " if you want to record the cpu utilization for specific time period press y else press enter "
read ip

you need to make the script able to run non-interactively ... modifications to your original script in red font ...

also, if not already there -- please get into the habit of using indents as that really helps everybody debug your code ...

to run script remotely ... notice the shortened ssh command line with the y argument ...

ssh username@servernname "/dir/scriptname y"
#!/bin/ksh
ip=$1
function record
{
((end = $(date +%s) + $second))
while [ $(date +%s) -lt $end ]
do
    echo $end
    echo $(date +%s)
    (ps aux|head -10 && echo " " ) >> testing.txt
    sleep 1
    done
}
echo " This script displays cpu utilization of currently running processes on the remote server and record the the data for a period if required"
op=`ps aux | head -10`
echo " "
sleep 2
if [ ! $ip ]
then
    echo " if you want to record the cpu utilization for specific time period press y else press enter "
    read ip
fi
if [[ "$ip" == "y" ]]
then
    echo " enter the time to be recorded in seconds eg:5"
    second=$2
    echo " cpu utilization is being recorded at ~/cpuutilreport.txt, open the file after `expr 10 + $second ` seconds"
    sleep 2
    echo "$op"
    record
    sleep 1
else
    echo "cpu utilization of currently running processes (top 10 cpu consuming only)"
    echo " "
    sleep 2
    echo "$op"
fi

exit 0
1 Like