linux or windows shell script to upload a file and copy it to another server - linux

I am struggling with a problem. I have:
ServerA (is closer to me and much faster)
ServerB (is my website where I want the final file to stay)
so what I want to do with the shell script (either Windows batch or linux, I have cygwin installed) is, passing the filename as parameter:
1) upload with FTP a file to ServerA
2) login with ssh on serverB and wget the file from serverA
I managed to do 1 with a shell script, but I don't understand how to do step2 in the shell?
Thanks

I would recommend using scp to accomplish step 2. You can use the syntax:
scp path/to/file serverb#hostname:/path/to/destination.
You can read more about the syntax for scp here: http://www.hypexr.org/linux_scp_help.php

You could use TeraTerm which has a powerful scripting language to automate both tasks.

Related

copy file from one server to another in linux

how to run commands like ftp or sftp or scp in background? Also how to setup password less connection for running these command?
Look for manual pages for scp or rsync, which both can do this job well, if not being forced you don't want to use sftp or even the non encrypted ftp file transfer!
something like the following, for example:
rsync [some other parameters] -e ssh SOURCE TARGET
Assuming these commands are coming from a bash script , you would need to make sure that the two (or more ) systems have ssh certificates generated that allow you to access said systems without providing a "password" per se.
Briefly, you could do it by running this command on one system:
ssh-keygen
following through, this will generate a key. Then run:
ssh-copy-id user#some-remote-system
to copy it to the remote system, which will allow passwordless access, enabling scripts to go about their business without stalling for password prompts.

How to run shell script in another server in a script?

No where online can i find a way to run a shell script on a remote server from another script. This is for automation, so the script on the host machine will automatically trigger another script on a different server. The server that my script will ssh to will either have a password prompt or have RSA key pair set up
Thanks!
Just pass the command as an argument to ssh.
ssh someserver /path/to/some/script.bsh
Let's say you want to execute a script on node2 but you have your script on node1 file name of script is sp over location /home/user/sp. Simply
ssh node2 < /path-of-the-script-including-the-filename
Another way, using expect package.
Disclaimer: This you can use for testing environments since it has an
open password. but depends on your usecase
If your server does not have expect, you may add the package then. run the command. You can also put this command inside an .sh script.
expect -c 'spawn ssh user#10.11.12.13 "/path/to/my.sh"; expect "assword:"; send "Y0urp#ssw0rd\r"; interact'

Replicating SCP command in Shell script in linux?

Hi I have been given a task of copying files from a given server to local machine. Even I can do it manually using the command line but I need to write a script to automate it. I dont have any clue how to do it using shell, how to give the password which we would have done manually. I went through other posts but did not get the precise answer.
Are there better ways than using SCP command?
Thanks in advance
The preferred + more secure way to do this is to set up ssh key pairs
That being said, if there's a specific need to supply passwords as part of your shell script, you can use pscp, which is part of putty-tools:
If you are on ubuntu, you can install it by:
sudo apt-get install putty-tools
(Or use equivalent package managers depending on your system)
Here's an example script of how to use pscp:
#!/bin/bash
password=hello_world
login=root
IP=127.0.0.1
src_dir=/var/log
src_file_name=abc.txt
dest_folder=/home/username/temp/
pscp -scp -pw $password $login#$IP:$src_dir/$src_file_name $dest_folder
This copies /var/log/abc.txt from the specified remote server to your local /home/username/temp/

Make a script to download file and a cron job for it through ssh

I'm trying to create a script to download a file daily with the older version overwritten.
I'm pretty sure I need a cron job, and a shell script with a wget line in it, but that is as far as I know. Also, I need to do all of this through ssh, unless there's another way I'm not aware of.
If I do it through SSH, what commands do I need to use through the various steps in the process? What will the cron and the shell files look like? If there's a better way, please enlighten!
Thanks!
Zeem
From your description, I'm picturing the following:
connect to the server via SSH
find the location of wget
which wget
(on my machine it's /usr/bin/wget)
add the following to your /etc/crontab (or cronjobs file) using a text editor, such as pico or vi:
#daily /usr/bin/wget http://remote-host.name/path/to/file.txt /local/path/to/file.txt
(If you add this to the /etc/crontab, you'll probably need the additional user parameter, but you can see crontab help for that.)
hope that helps.
Implement password-less ssh authentication between the hosts.
http://www.linuxproblem.org/art_9.html
So host A can create/implement an script or cronjob on host B using ssh.
To create a cronjob by using a script, your script create (for example) an textfile at /etc/cron.d/CronJobName. It is important, that the content of the file corresponds to the corn format: http://en.wikipedia.org/wiki/Cron#Examples
(I hope, I understand your question right)
Thanks for your answers, Thankfully it was much simpler. I was able to add a cron job via cpanel, and the wget line went straight in there.

telnet Unix/Linux and download the file to local Windows system

Is it possible to do so:
I am on Windows system and telnet Unix/Linux remotely. Then I would like to download file from telnet mode and download to Windows system. Any Unix/Linux command are able to do so? like rcp or ftp. How to do so? Does it require any configuration on both system?
i try to write the shell script on Unix/linux side. and i telnet Unix/linux system remotely from local Windows machine and log in to Unix/linux system, run the script on Unix/linux side. some files will be transfered or download automatically to my windows system.
assume permission is not a problem
You can use x/y/zmodem to transfer file if both ends support that.
On Linux/Unix, you can use sz command to send file via zmodem.
On Windows, both Secure CRT (commercial) or Le Putty (open source) are capable of zmodem.
I think you want PSCP (command line) or WinSCP (GUI).
Usually people use ftp session to transfer files. If permissions is not a problem why don't you run ftp from your Windows computer?
You can create a windows batch script to load your file. Try something like
ftp hostname
user
password
cd /path/to/file
get myfile
quit
If you must do it through the existing connection, look at protocols such as ZMODEM and the rz and sz commands for them on Linux. You will need a terminal program on Windows that supports that protocol though.
For small files, I just uuencode (or gzip -c | uuencode name.gz) on the command line over the telnet session, then cut and paste to uudecode on my local windows computer (using cygwin).
You can use this:
telnet hostname 80 > filename_to_download_in
GET /path/to/file HTTP/1.0

Resources