How to write a shell script to connect to another server?

Hello friends

I want to write a script in which I will connect to my friends network.
I want to use SSH.
Even they can use the script to log into my network and copy files.

ssh user@hostname command

I know the following command will help me to log into Google's servers and see all the people who are logged into their system.
ssh Angelo@www.google.com who

I guess the following program will help to copy the files.
�............................................................................................................................................................................................................

* #!/bin/sh

** $ cat mycp
*#

*# copy a file

*#

* if [ "$#" -ne 2 ]
* then

*** echo "Usage: mycp from to"
*** exit 1
* fi

* from="$1"

* to="$2"


*#
*# See if the destination file exists
*#

*if [ -e "$to" ]
*then
*** echo "$to already exists; overwrite (yes/no)?"
******* read answer


****** if [ "$answer" !=yes ]
****** then
********* echo "Copy not performed"
********* exit 0
****** fi
*fi


*#
*# Either destination doesn't exist or "yes" was typed
*#

* cp $from $to**** # proceed with the copy

* $

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I need your help to write a script to this end.
I can log into their networks and copy files and exit.
I want to use SSH to log into the networks.
Your help is deeply appreciated.

This will log into a server with ssh and run a script file from your computer with the BASH shell. You can substitute whatever shell you want as long as the server has it.

ssh user@host /bin/bash < /path/to/script

This will of course still ask for a password. If you want to login with no password, you can use a pre-shared key.

Thanks Corona for the reply.

The script I have written doesn't work.
There is an error message. I have tried in vain to rectify it.
Could you please look at it too?

Hi.

What's the error? And what's with all those asterisks? I was about to edit your post, fixing a couple of things along the way, to look more like this:

#!/bin/sh

# ???? $ cat mycp
#

# copy a file

#

if [ "$#" -ne 2 ]
then
  echo "Usage: mycp from to"
  exit 1
fi

from="$1"

to="$2"


#
# See if the destination file exists
#

if [ -e "$to" ]
then
  echo "$to already exists; overwrite (yes/no)?"
  read answer


  if [ "$answer" != yes ]
  then
    echo "Copy not performed"
    exit 0
  fi
fi


#
# Either destination doesn't exist or "yes" was typed
#

cp $from $to**** # proceed with the copy  # **** means?

but thought maybe I'd missed something!

Thanks scottin for the reply.

I am a beginner when it comes to scripting in Linux.

So please correct the script and rewrite.

The script should copy directories and files from my friend's computer. Afterwards it should terminate the activity.
I know it will ask a password; that is all right.

The following is the error message:

[htr@localhost ~]$ ./mycp
./mycp: line 7: $: command not found
Usage: mycp from to
[htr@localhost ~]$

We're not trying to be obstinate. Your script is so incomplete and garbled there's really no fixing it at this point. If you've posted it as you actually have it -- crazy asterisks and all -- then it should be thrown out and rebuilt from scratch. If not, please post how you actually have it, for we are not psychic here.

The script should copy directories and files from my friend's computer.

Why not use scp? It connects via ssh, which you say you have, and does nearly exactly what you want.

# copy file/dir from local host to remote host
scp -r localsource username@host:/path/to/remotedestination
# copy file/dir from remote host to local host
scp -r username@host:/path/to/remotesource localdestination

So easy you might not need to build a script around it at all. It doesn't and can't prompt about overwriting files, unfortunately.

Thanks Corona for the reply.
You wrote the following:
If not, please post how you actually have it, for we are not psychic here.

It seems you are annoyed by my questions. I am a beginner here.
I am trying to learn from you all.
Please be patient. I badly need your help.
If my friends have Windows networks, does the command scp works fine?

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I just came home from work. I went to the gym after work. I am tired
I need more help. I will post tomorrow again. I must sleep now.

I was a touch annoyed since you seemed to be repeating the same questions hoping for different answers. I also suspect your approach to the problem won't work, but can't be sure until I can be completely sure what it actually is.

If you can ssh to a system, you can scp to it. It connects to the same service.

If you're asking if there's a windows version of scp, it's available here as pscp.

It won't work out of the box; You need additional software like cygwin or SFU (just to mention a few).

Could you post your script without all those gaudy asterisks and strange echoes?

I thank both Corona and Verde for the replies.

Corona, I need some more information.

1.First I have to install SSH
2.Afterwards I have to install SCP

I hope the above is correct. Please tell me if I am wrong.

Even my friends have to do the same thing. Otherwise they can't copy files and folders from my system. Please tell me if I am wrong.
I don't know very much about these things. I am here to learn.
�...............................................................................................................................
Verde wants me to repost the script. I will do so when time permits.
Time is the biggest enemy for me. I work and study. I am studying a course on server virtualization. On top of this I go to the gym 5 times a week for circuit-training. I come home everyday around 10 o'clock. No time at all!

It seems Verde has some experience of Windows environment too. I remember cygwin. I had it some years ago. It mimics the Linux command line.

So both cygwin and pscp are necessary for Windows clients.
I run Linux. If my friends have Microsoft, they must install both cygwin and pscp.
Please tell me if I am wrong.

I don't know exactly what you're doing, so I can't say.

What is your system? You were writing a script for ssh before. If your system lets people ssh to it, it lets people scp to it.

If you're scp-ing from Windows computers to UNIX ones, all you need is pscp. pscp is a standalone program. It doesn't even need to be installed, it's a sole executable file.

hi
one doubt inthe above script if [ -e "$to" ]. what is the option -e used for ?

Evaluates to TRUE if file $to exists.

Introduction to if

:smiley:

I still do not get exactly what you want to do.

As corona already posted, either you can use pscp to connect from Windows to Linux (from a cmd prompt) or WinSCP if you want a GUI.