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


 
Thread Tools Search this Thread
Top Forums Programming How to write a shell script to connect to another server?
# 1  
Old 04-21-2010
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.
…................................................................................................... .................................................................................................... .....

Code:
* #!/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.

Last edited by pludi; 04-22-2010 at 06:27 AM..
# 2  
Old 04-21-2010
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.
Code:
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.
# 3  
Old 04-21-2010
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?
# 4  
Old 04-21-2010
Quote:
Originally Posted by Angelo
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:
Code:
#!/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!
# 5  
Old 04-22-2010
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:

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


Last edited by pludi; 04-22-2010 at 06:27 AM.. Reason: code tags, please...
# 6  
Old 04-22-2010
Quote:
Originally Posted by Angelo
Thanks scottin for the reply.

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

So please correct the script and rewrite.
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.

Code:
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.

Code:
# 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.

Last edited by Corona688; 04-22-2010 at 12:40 PM..
# 7  
Old 04-22-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not able to connect to Oracle server from shell script

Hi, I am facing an issue in connecting to database from Linux shell script. Here is my code export ORACLE_BASE=xxx export ORACLE_SID=xxx export ORACLE_HOME=xxxx DB_PATH=xxx/'yyy@yy' UserId=`sqlplus -s $DB_PATH <<EOF set head off feedback off select max(USER_ID)+1 from USERS;... (6 Replies)
Discussion started by: yuvi
6 Replies

2. UNIX for Advanced & Expert Users

How to write if condition in shell script for application server?

Hi all, I have a code to create folder in application server through shell script and i want to create if conditional based folder folder=$HOME/test/sample/whatever if ; then echo "$folder already exists, not created." else mkdir -p "$folder" > /dev/null 2>&1 ... (7 Replies)
Discussion started by: Boost
7 Replies

3. Shell Programming and Scripting

Shell script to connect from one server to other

Dear Experts, I am new to the shell scripting. I am looking for a shell script to connect to one Unix/Linux server1 to other Unix/Linux server2 and trigger a SAP Event in that server2 (Which will trigger a job in SAP). Is this possible to connect from one server to the other server securely... (7 Replies)
Discussion started by: Venu V Reddy
7 Replies

4. Shell Programming and Scripting

Connect (SSH) to Windows server via Linux server through a script and passing command.. but failing

I am trying to connect to Windows server via Linux server through a script and run two commands " cd and ls " But its giving me error saying " could not start the program" followed by the command name i specify e g : "cd" i am trying in this manner " ssh username@servername "cd... (5 Replies)
Discussion started by: sunil seelam
5 Replies

5. Shell Programming and Scripting

connect to SQL server from shell script

Hi all, I need to connect to SQL server from shell script. Then need to execute queries from shell script as below 1. To compare values of an array with SQL table's entry. I am using solaris-5.1 to run shell script and want to connect to SQL-5.1 which is installed on SantOS. Kindly let... (3 Replies)
Discussion started by: amitbhelave
3 Replies

6. Solaris

connect to SQL server from shell script

Hi all, I need to connect to SQL server from shell script. Then need to execute queries from shell script as below 1. To compare values of an array with SQL table's entry. Kindly let me know on how to do this Thanks, Double post. Continue here (0 Replies)
Discussion started by: amitbhelave
0 Replies

7. Shell Programming and Scripting

connect to SQL server from shell script

Hi all, I need to connect to SQL server from shell script. Then need to execute queries from shell script. Kindly let me know on how to do this Thanks, Ananthi.U (1 Reply)
Discussion started by: ananthi_ku
1 Replies

8. Shell Programming and Scripting

Shell Script to connect to another server and copying files

Hi Unix Gurus, I have a doubt reg file transfer. I have used the below script to connect to another server and find files having modified for the last 24 hours and have to move the file to another server. While i tried i am getting authentication failed, destination path not found issue. ... (2 Replies)
Discussion started by: incepted
2 Replies

9. Shell Programming and Scripting

How to write a shell script for rsync to remote server?

Hello, Can you help me to write a shell script for rsync a folder from my server to remote server ? i do this in ssh by this command : rsync -avz -e ssh /copy/me/ login@serverip:/where/to i want have a shell script that do this command. and you know that this command ask remote... (0 Replies)
Discussion started by: Desperados
0 Replies

10. Shell Programming and Scripting

shell script for how to connect to a remote server by using ssh

i want to connect to a remote server through ssh. i have to also provide password within that script. after connecting to the remote server i want to do some operations like grep,cd etc can u pls help me to wite a script. Thanks (1 Reply)
Discussion started by: millan
1 Replies
Login or Register to Ask a Question