Find the answer to your Linux question:
Results 1 to 4 of 4
Hello All, I have an issue. It would be great if anyone could help; I need to SSH to a remote machine and execute a specific command there and capture ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    17

    SSH on a remote machine with password.


    Hello All,
    I have an issue. It would be great if anyone could help; I need to SSH to a remote machine and execute a specific command there and capture its output.
    As per the standard stuff, i should be able use the following syntax for SSH:
    ssh username IP <command>
    and use expect to supply the password.
    But my remote machine is not a STANDARD unix machine and hence i can not execute the command like this in one step.
    I need to SSH to this server and then execute the command as a second step. I am new to EXPECT. Please suggest.
    Please note that SSH-KEYGEN does not also work with my remote machine.
    It would be really nice if somebody could share a simple expect script for the above requirement.
    Thanks in advance

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    if ssh-keygen does not work with the remote machine, then you can still try to copy the keys manually. it really is better than using expect, if you can manage it.

    on the remote machine, make sure the .ssh directory exists in the home directory of the user you are using to log in, e.g.:

    (local pc) # ssh <user>@<remote_pc>
    (remote pc) # mkdir -p ~/.ssh
    (remote pc) # exit

    then make sure your keys exist on the local machine first:
    (local pc) # ls ~/.ssh/id_dsa*

    if they don't, make them:
    (local pc) # ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ''

    then copy them to the remote machine:
    (local pc) # scp ~/.ssh/id_dsa.pub <user>@<remote_pc>:.ssh/authorized_keys2

    you may need that file to be another name, so you can symlink it:
    (remote pc) # cd ~/.ssh
    (remote pc) # ln -s authorized_keys2 authorized_keys

    now try to ssh in w/o a password from local to remote
    (local pc) # ssh -oPasswordAuthentication=no <user>@<remote_pc>

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    17
    Thanks for the reply. But i do not have any permissions to copy any file/key on the remote machine. So, i think whatever has to done, has to be done on my machine. So i was thinking of using Expect. Please suggest

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by nitesh View Post
    Thanks for the reply. But i do not have any permissions to copy any file/key on the remote machine. So, i think whatever has to done, has to be done on my machine. So i was thinking of using Expect. Please suggest
    you have rights to upload to a specific directory only, then? seems odd...

    anyway, as long as you have a valid username/password, you just need to change the prompt in your expect script to match what the remote system uses for the prompt. For example, on my machine it is "password: ", but perhaps on yours it is "User password: " or something. check your script.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •