Skip to main content

Using SCP, SSH commands over remote servers to acces and transfer data

We often have to interact with linux servers over remote m/c's for deployment or maintenance issues. I would specify very basic commands which comes handy few times .


Running multiple commands on a remote machine after ssh: 

Write the commands in a shell script and scp the file, now ssh to remote server and execute the file.
In my case i have written a ruby script

ssh ubuntu@ec2-addr /bin/bash << EOF
  exec ssh-agent bash ; ssh-add /home/ubuntu/.ssh/key.pem
EOF

system("ssh ubuntu@ec2-address /bin/bash << EOF
     exec ssh-agent bash
     ssh-add /home/ubuntu/.ssh/key.pem
   
     GIT_TRACE=1 git clone ubuntu@ec2-address:/home/ubuntu/vol/story.git
EOF")

refer: Add Private Key permanently with ssh address on ubuntu

Identity file was added in .ssh/config file. Now when i try to take a git pull, it says permission denied as it's trying to use the key place in config file and not id_rsa.pub which was previously set.
The config file is present in .ssh/config. In case config file is not created then create one.

The file could be
HostName host-address
    User username
       IdentityFile ~/.ssh/key.pem

To manage with multiple keys for different host names, use the below configuration in .ssh/config file.


Host ec2-**-***-**-***.ap-southeast-1.compute.amazonaws.com
  HostName ec2-**-***-**-***.ap-southeast-1.compute.amazonaws.com
    User ubuntu
       IdentityFile ~/.ssh/key_dev.pem

#Default GitHub
Host github.com
  HostName github.com
    User git
      IdentityFile ~/.ssh/id_rsa

Host github-public
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_public

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal


Ssh to a remote instance:

ssh -i path-to-private-key username@instance-address

Scp a file or folder to a remote instance from local m/c:

File: scp -i path-to-private-key path-to-file-on-local-m/c username@instance-address:storage-path

Here you have two options with storage-path, replacing "storage-path" with " ."(Dot) will send the file to home folder of ec2 instance or complete path can be provided.

Folder: scp -r -i path-to-private-key path-to-folder-on-local-m/c username@instance-address:storage-path

Scp a file or folder from a remote instance to local m/c:
run the below commands from local m/c

File: scp -i path-to-private-key username@instance-address:path-to-file-on-remote-mac local-path-to-store-file

Folder: scp -r -i path-to-private-key username@instance-address:path-to-folder-on-remote-mac local-path-to-store-folder



Comments

Popular posts from this blog

Starting with Ruby Metaprogamming

Metaprogramming is a programming technique in which computer programs have the ability to treat programs as their data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself while running.(wikipedia)
 Using simple code samples let's give basic start to metaprogramming which helps to relate how code base can be simplified.
 Considering the below class.

Now, using some metaprogramming concepts above code can be modified, notice the assign_attributes method used while the o/p in both the examples is same

Adding some more examples, below simple code helps to generate methods on the fly instead of defining in individually


Metaprogramming should be used with caution and not make the code complicated and difficult to read. It should be easy to understand and modular to change in future.

upload images to AWS::S3 in ruby using aws sdk gem

Using gem aws-sdk for a ROR application for uploading images to s3
Uploading images to a fixed bucket with different folders for each object or application. The s3 keeps a limitation on the number of buckets creation whereas there is no limitation for content inside a bucket. This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public, so that the images uploaded are directly accessible. The input is takes is the image complete path where it is present, folder in which it should be uploaded and user_id for whom it should be uploaded. def save_screenshot_to_s3(image_location, folder_name,user_id) service = AWS::S3.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY) bucket_name = "app-images" if(service.buckets.include?(bucket_name)) bucket = service.buckets[bucket_name] else bucket = service.buckets.create(bucket_name) …