Download a file from a Server with double ssh - linux

I connect to a Server with ssh
Step 1:
$ ssh userid#something.com
and then it asks for password and everything is ok
Then I connect to a DB
Step2:
$ssh user1#smthing_else
and then it asks for password and everything is ok
Now when I type ls I can see the file that I want to download...
How can I download this file on my Desktop..??

You need to scp the file twice in order to bring it to local m/c if you don't have direct access. First ssh to the server 1 and run the command to download it. Then run this command again from your local m/c.
scp -r -i path-to-secret-key ubuntu#ec2-address:/home/ubuntu/app-folder-location /home/user/local-mc-location
As you don't have the key, use the below command
scp -r ubuntu#ec2-address:/home/ubuntu/app-folder-location /home/user/local-mc-location
Update:
path-to-secret-key is the private key address which is used in ec2 instances to ssh. They are used for authentication and are present in home/.ssh/private-key. They have a permission of 400 and are either .pem extension for unix m/c's or ppk extension for windows m/c's

I guess you can't directly reach the "inner" host from the outside? In that case you have to ssh into the outer host, then you can use scp to copy the file from the inner host to the outer one. Accordingly, you can then copy the file to your local pc from the outer host with scp (or whatever you can use in that case).
scp works like this (to copy a local file to a remote host):
scp myfile.txt user#somehost.com:/home/user/whatever
resp. like this (remote to local):
scp user#somehost.com:/home/user/whatever/myfile.txt .

Related

Sending files to SSH client over SSH using SCP

The situation is that my client machine does not use static IP but the server machine is using static IP.
I connect to the server machine using ssh from the client machine.
Are there any easy ways to send files from the server machine to the client machine using scp or other commands?
I want to execute commands inside the server.
You can scp from the client machine and get the file from the server.
client$ scp user#server:< path to file > < path to destination folder in client >
eg, if you want file1 in the home directory from the server copied to the current directory in your client, you can do the following:
$ scp user#server:~/file1 .
ref: https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/
If you don't really need to connect using the ssh command, you can connect using the sftp command (ftp-like interface through an ssh connection):
$ sftp user#server
And then just use the command get:
sftp> get <file>
Commands like ls, cd and pwd also work in this sftp interface.

Copy file from local machine to remote machine

I have a pretty novice question, but I'm really out of resources right now.
I'm trying to send a script to a remote machine. This script is in my local machine and it's called model.py.
I can access the remote machine by typing ssh csousa#headtop.ncc.unesp.br (the remote machine knows my public key)
I read about the usage of the scp and rsync commands. I tried:
rsync -v -e ssh /home/ecaue/ParticlePhysics/TCC/model.py csousa#headtop.ncc.unesp.br
and
scp -r csousa#headtop.ncc.unesp.br /home/ecaue/ParticlePhysics/TCC/model.py
but what I get is just a copy of my original file with another name ("csousa#headtop.ncc.unesp.br") in the same folder of my original file (like a clone with a different name).
I can access the remote machine and create conda environments and all kind of stuff(install tensorflow, keras,etc), but I'm not able to send my scrip and my dataset.
I would really appreciate any help!!
The scp command should be:
scp PATH_TO_YOUR_SCRIPT/SCRIPT_NAME user#host:DESTINATION_PATH
So in you case:
scp /home/ecaue/ParticlePhysics/TCC/model.py csousa#headtop.ncc.unesp.br:$HOME

Transfer files between local to remote server using ssh without password authentication

I want to transfer some files from my local to remote, like github does it. I want to happend it very smooth like in shell script. I tried creating one shell script which automates the process of ssh authentication without password but for first time it exposes my remote server password. I dont want to do it that way. Like in git we can't see their server password. Is there any possible way that we can do ?
I used this article script to automate ssh login. http://www.techpaste.com/2013/04/shell-script-automate-ssh-key-transfer-hosts-linux/
As i mentioned, you can use the scp command, like this:
scp /local_dir/some*.xml remote_user#remote_machine:/var/www/html
This requires that you need connect to the remote machine without password, only with ssh key-authentication.
Here is a link: http://linuxproblem.org/art_9.html to help you.
The important steps: (automatic login from host A / user a to Host B / user b.)
a#A:~> ssh-keygen -t rsa
a#A:~> ssh b#B mkdir -p .ssh
a#A:~> cat .ssh/id_rsa.pub | ssh b#B 'cat >> .ssh/authorized_keys'

Copy files from Linux server using ssh client with different user name

I have this linux machine with ssh server installed, I can access the server using username="ubuntu". ssh server blocks clients that try to connect using "root" username.
So connection can be made by:
ssh -i mykey ubuntu#myserver
I can get files that belong to "ubuntu" using :
scp -i mykey ubuntu#myserver:<file location> ./
However, what I really want is to get files that belong to "root" username, (Note: I can't access the server with username "root" for obvious security reasons).
so is there a way to do download files that are under "root" username?
I was thinking to do some magic in the server side that enables me to do that.(I don't know how :) )
if this help: I have root access and also I can create files on my server side. but I'm not allowed to change the file permission under the root(if someone get hold of these files I'll be fired)
You can try monster like this
ssh ubuntu#myhost 'sudo cat /path/to/file | uuencode' | uudecode > path/to/local
You should have uuencode and uudecode on coresponding hosts.
Or if file is text you can skip uuencode part
ps: see related topic
You could do it the other way around.
Log into the the pc with the file you want with
ssh ubuntu#myserver
Then gain superuser privileges
sudu su
and then copy the files you want
scp /the_file_you_want ubuntu#myhost:/the_location_and_filename_you_want
Some other ways you can find here
https://unix.stackexchange.com/questions/106480/how-to-copy-files-from-one-machine-to-another-using-ssh
enable ssh on your machine
(if fedora) (for ubuntu you can find command on google easily)
service sshd on
From your local machine
ssh -i ubuntu#myserver
change to root
su
enter password
and copy files using scp
scp somefile.extension randomuser#localmachine:/some/path/
I hope it helps

How to run a shell script over ssh with resource(.txt files) in one machine and the script in another machine?

I want to run a shell script using SSH which takes resource from other machine while the script is in some other machine, all on the same network. I don't want to copy the resource to the local machine.
Note: The shell script takes .txt file as input
If you have script.sh on server1 and file.txt on server2, you can connect through ssh to server1, and then do:
[user#server1]$ ssh user#server2 "cd mydir && cat file.txt" | ./script.sh
Try this:
ssh USER_NAME#HOST_ADDRESS "BASH_SCRIPT_FILE_PATH"
You will need to provide password whenever required.
If your script is in Machine A, you can't run that on Machine B without copying it over. First, copy the script over to Machine B using scp
[user#machineA]$ scp /path/to/script user#machineB:/home/user/path
Then, just run the script
[user#machineA]$ ssh user#machineB "/home/user/path/script"
This will work if you have given executable permission to the script.
OR
Try this one..
<hostA_shell_prompt>$ ssh user#hostB "ls -la"
That will prompt you for password, unless you have copied your hostA user's public key to the authorized_keys file on the home of user .ssh's directory. That will allow for passwordless authentication (if accepted as an auth method on the ssh server's configuration)
I not fully understand your question. Other answers gave "How to run remote script?"
But i think question is Remote script has to take remote file, even I not sure about this
Login Remote PC using ssh.
Install sshfs if not installed .
Then mount other remote machine directory which has the file you want to use in script to local directory. This can be done using sshfs
Then run the script with file from locally mounted directory
Then unmount the directory when you finished.
Somewhat large procedure.
Mounting remote directory with sshfs
man sshfs

Resources