How to transfer file from one machine to another machine - shell

I want to send files from one machine to another machine using SFTP and need to automate this (bash).
Example: Need to send files from my IP address XXX.XXX.XXX.XXX to another IP address XXX.XXX.XXX.XYY.
How to do this?

You can use scp:
scp ~/my.log remote_user#remote_host.com:~/remote_path

Related

Shell Script to copy files from linux machine to windows machine

I would like to copy a file from Linux machine to Windows machine periodically (both the machines are accessed remotely).
Suppose I have the following.
Linux machine named host1 and the username is user1
Windows machine named host2 and the username is user2. The windows machine also has the password to logged in.
I want to copy files from /home/admin of host1 to D:\admin of host2.
Can anyone please help me in creating a shell script to perform this task.
One way to copy files from one machine to other is using SCP utility.
scp username#host_ip:/home/user/your_file /cygdrive/D/admin/
Copy the file "foobar.txt" from a remote host to the local host
scp your_username#remotehost.edu:foobar.txt /cygdrive/D/admin
Simple generic command.
Copy the file to the /home/project/ directory on your device. Note: provide IP is just an example.
scp youfile.txt user#192.168.10.2:project/
I suggest you check this SCP commands.

Copy file with rsync or scp over multiple level or hops of SSH

I need to transfer around 4.2 GB of files from my local computer to a server B. However to ssh into server B, I need to ssh into server A.
Currently I'm copying files from my local computer to server A and then from server A to server B.
So the flow goes like this:
rsync -avz --del ~/Desktop/abc/ <my-user-name>#<server-A>:~/abc
rsync -avz --del ~/Desktop/abc/ <my-user-name>#<server-B>:~/abc
This is slow and copies 4.2 gb of data two times instead of one!
Can I transfer files with rsync from my local computer to directly server B ?
You can always use ssh with proxy command, which allows you to transfer files transparently. Using this config (~/.ssh/config):
Host <server-A>
User <user-A>
Host <server-B>
User <user-B>
ProxyCommand ssh <server-A> -W %h:%p
You can call your rsync:
rsync -avz --del ~/Desktop/abc/ <server-B>:~/abc
The data will be only "routed" over the middle host.
What you want is to use port-forwarding to forward the ssh/rsync port (generally port 22) from server B to alternate ports on server A so when you call rsync -e "ssh -p altport" serverA:/sourcedir /destdir, you are actually invoking rsync from serverB.
There are many good howtos available on StackExchange and other sites. For example:
How to forward a port from one machine to
another?
or
How To Forward Ports through a Linux Gateway with
Iptables
will get you started. Using port-forwarding, you are essentially using serverA as a pass-through host so you will only have to transfer your 4.2G once.
Yes, you can copy the files (and even folders) directly without making any intermediate copies on the contact/login server, which is by default the machine known to the outside world, or contacted to get access to a specific local network.
Below is a simple demonstration using scp without any unnecessary complications. On the local machine, simply do the following:
$ scp -r -o ProxyCommand="ssh -W %h:%p your_username#contact-server.de" your_username#machine_name:/file/path/on/this/machine ~/destination/path/to/save/the/copied/folder
-r option instructs scp to copy the contents of the entire folder.
your_username need not be the same on both machines.
If it is successful, you'll be asked for your passwords on both machines for authentication.
In the above command it is assumed that the typical way to access the machine named as "machine_name" would be via the contact server.
Note:
The above command also works for transferring data from a source remote machine (e.g. s) to a target remote machine (say t). In such a scenario, first ssh to the source remote machine (s) and navigate to the path where the data resides. After that you can simply think of/treat that remote machine as a local/source machine and then simply use the same scp command listed above for copying folders.
For copying individual files, just remove the -r option and provide the path to the specific file that you want to copy.

SCP, download a file from website to local pc

I am using putty to connect to the website. I know how to copy a file from a website to my computer using normal terminal in my PC but my question is if I am already logged in to the website and I want to copy a file to my local PC, do I need to put my computer IP address??
Here is easier to describe the problem:
scp kk.txt [my local pc]
now what should I substitute instead of [my local pc]??
If u have static ip -
scp kk.txt username#STATIC_IP:directory_to_copy_file
e.g scp kk.txt user#1.2.3.4:~ (will copy it to ur home folder)
if you have dynamic ip, you can not scp from website to ur local pc, but you can do otherway round, like
scp username#webiste_address:file_path ur_local_directory
e.g. scp me#somesite.com:~/kk.txt . (this will copy file kk.txt to your current directory)
the second approach is always recommended, as you dont have to worry about ur ip address, you just need to know the address of the remote site from where you are copying data.

(ba)sh - Determine (based on machine name) whether a computer is on the local network

Working on a shell script that takes a machine name as an argument and then determines if the host is on the local network (same network as the machine that ran the script).
How can I get the IP address from the machine name? Once I get that I should be able to compare that IP with the local one to see if they're on the same subnet.
You can use nslookup (http://linux.die.net/man/1/nslookup), dig (http://linux.die.net/man/1/dig) or host (http://linux.die.net/man/1/host) command-line utilities.
For example, here is the result of running host for getting A-records for stackoverflow.com from DNS server:
$ host -tA stackoverflow.com
stackoverflow.com has address 69.59.197.21
What do you mean by local network? subnet or (windows) domain or within LAN?
You may also have a look at traceroute utility.

FTP - `Name or service not know`

I have an app, on my iphone, which allows me to transfer songs too and from my linux desktop via ftp. The app indicates that the host address is 192.168.0.4 and the port to be used is 5021. I can connect fine to the device from filezilla but when I try to connect via the command line it fails with the error mentioned in the title.
The command i'm using is ftp 192.168.0.4:5021 but it fails. I'm totally stumped. When I search for help on the internet the answer is to simply do what I've done already.
Does anyone have an any idea on what to do?
The ftp command does not support the :5021 syntax for port numbers. Instead, there must be another way.
With some FTP clients, you can use a -P command line option:
ftp -P 5021 192.168.0.4
With others, you need to put the port number after the host address with a space:
ftp 192.168.0.4 5021
Use man ftp to see which yours supports.
add domain and ip to hosts file of you computer.

Resources