Sending files to SSH client over SSH using SCP - linux

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.

Related

Shell script remotely

I have one script running on server and doing some job on other server
I have many scp commands and ssh commands, this is why each time I have to enter the remote server password at each remote command.
is there any way to establish ssh connection between the servers so I type the remote password only once?
thanks
I would suggest to setup an ssh config together with ssh keys. In a nutshell the config will hold an alias for one or more remote servers.
ssh remote_server1
ssh remote server2
While your config file will look something like this:
Host remote_server1
Hostname 192.168.1.12
user elmo
IdentityFile ~/.ssh/keys/remote.key
...
If an ssh config file is not for you (although I can highly recommend it), you can use sshpass as well.
sshpass -p 't#uyM59bQ' ssh username#server.example.com
Do note that the above does expose your password. If someone else has access to your account, the history command will show the code snippet above.

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

Download a file from a Server with double ssh

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 .

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