Run a script on remote host

Hi,

I wish to run a script located on a remote host machineB from machineA.

I am using ssh and running the below on machineA.

However, the ssh does not seem to work and freezes at

ssh -l wlsadmin machineB -v
Sun_SSH_1.1.2, SSH protocols 1.5/2.0, OpenSSL 0x0090704f
debug1: Reading configuration data /home/wlsadmin/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Rhosts Authentication disabled, originating port will not be trusted.
debug1: ssh_connect: needpriv 0
debug1: Connecting to machineB [129.181.181.72] port 22.

I will provide any inputs required to debug this.

Kindly help.

I think the default timeout for ssh is 30 seconds. Did you wait that long? Maybe add a -o ConnectTimeout=3 to see if there is anything happening after 3 seconds.
Make sure sshd running on machineB, that there is no firewall inbetween etc. If you have nmap, you can also try to do following:

nmap machineB -p22

It timesout at the desired time. The problem is why does it not work. I do not have nmap. Anything else I can use to test if it can work?

Thanks much.

You did not answer that you made sure, that sshd is running on machineB. There is no firewall between them? Can you reach machineB on any other port than ssh? Start with ping for basic network reachability. Then maybe add ftp or whatever daemons you have up and running on machineB.

The ping works and so does scp.

How can i check if sshd is running on a machine ?

---------- Post updated at 06:14 AM ---------- Previous update was at 06:04 AM ----------

Ok, It is working one way.

ssh -l wlsadmin machineB
Successful...

However, as i need a script to get executed on machineB, how can i pass that with the 'ssh' command ?

Visa Versa fails with the below.

ssh -l wlsadmin machineA

wlsadmin@machineB> ssh -l wlsadmin machineA
Permission denied (publickey,keyboard-interactive).
wlsadmin@machineB> ssh -l wlsadmin machineA -v
Sun_SSH_1.1.3, SSH protocols 1.5/2.0, OpenSSL 0x0090704f
debug1: Reading configuration data /home/wlsadmin/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Rhosts Authentication disabled, originating port will not be trusted.
debug1: ssh_connect: needpriv 0
debug1: Connecting to machineA [162.81.181.25] port 22.
debug1: Connection established.
debug1: identity file /home/wlsadmin/.ssh/identity type -1
debug1: identity file /home/wlsadmin/.ssh/id_rsa type -1
debug1: identity file /home/wlsadmin/.ssh/id_dsa type 2
debug1: Logging to host: machineA
debug1: Local user: wlsadmin Remote user: wlsadmin
debug1: Remote protocol version 2.0, remote software version OpenSSH_4.3
debug1: match: OpenSSH_4.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-Sun_SSH_1.1.3
debug1: use_engine is 'yes'
debug1: pkcs11 engine initialized, now setting it as default for RSA, DSA, and symmetric ciphers
debug1: pkcs11 engine initialization complete
debug1: Failed to acquire GSS-API credentials for any mechanisms (No credentials were supplied, or the credentials were unavailable or inaccessible
Unknown code 0
)
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: Peer sent proposed langtags, ctos:
debug1: Peer sent proposed langtags, stoc:
debug1: We proposed langtags, ctos: i-default
debug1: We proposed langtags, stoc: i-default
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: dh_gen_key: priv key bits set: 140/256
debug1: bits set: 1037/2048
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'machineA' is known and matches the RSA host key.
debug1: Found key in /home/wlsadmin/.ssh/known_hosts:49
debug1: bits set: 1025/2048
debug1: ssh_rsa_verify: signature correct
debug1: newkeys: mode 1
debug1: set_newkeys: setting new keys for 'out' mode
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: newkeys: mode 0
debug1: set_newkeys: setting new keys for 'in' mode
debug1: SSH2_MSG_NEWKEYS received
debug1: done: ssh_kex2.
debug1: send SSH2_MSG_SERVICE_REQUEST
debug1: got SSH2_MSG_SERVICE_ACCEPT
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: /home/wlsadmin/.ssh/identity
debug1: Trying private key: /home/wlsadmin/.ssh/id_rsa
debug1: Trying public key: /home/wlsadmin/.ssh/id_dsa
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: No more authentication methods to try.
Permission denied (publickey,keyboard-interactive).
debug1: Calling cleanup 0x34c04(0x0)

I am not sure if I understand completely.

  • Your scp was working but ssh not. That is strange but at least shows, that your sshd is up and working since scp does use the same.
  • Now your ssh works on the command line. Did you supply a password or did you exchange keys? If you exchanged keys and it worked, it should work in a script too without problems. If this is the case, post the script using code tags.
  • If you supplied a password to ssh on the command line, then you have to create a password-less key pair on machineA, add the public key of those to machineB's .ssh/authorized_keys or .ssh/authorized_keys2 to have this way working to connect via ssh/scp/sftp without being asked to provide a password.

Hi,

I am not providing any password to ssh. However, not sure how could I as i do not see any option to provide password.

ssh
Usage: ssh [options] host [command]
Options:
  -l user     Log in using this user name.
  -n          Redirect input from /dev/null.
  -F config   Config file (default: ~/.ssh/config).
  -A          Enable authentication agent forwarding.
  -a          Disable authentication agent forwarding (default).
  -X          Enable X11 connection forwarding.
  -x          Disable X11 connection forwarding (default).
  -i file     Identity for public key authentication (default: ~/.ssh/identity)
  -t          Tty; allocate a tty even if command is given.
  -T          Do not allocate a tty.
  -v          Verbose; display verbose debugging messages.
              Multiple -v increases verbosity.
  -V          Display version number only.
  -q          Quiet; don't display any warning messages.
  -f          Fork into background after authentication.
  -e char     Set escape character; ``none'' = disable (default: ~).
  -c cipher   Select encryption algorithm
  -m macs     Specify MAC algorithms for protocol version 2.
  -p port     Connect to this port.  Server must be on the same port.
  -L listen-port:host:port   Forward local port to remote address
  -R listen-port:host:port   Forward remote port to local address
              These cause ssh to listen for connections on a port, and
              forward them to the other side by connecting to host:port.
  -D port     Enable dynamic application-level port forwarding.
  -C          Enable compression.
  -N          Do not execute a shell or command.
  -g          Allow remote hosts to connect to forwarded ports.
  -1          Force protocol version 1.
  -2          Force protocol version 2.
  -4          Use IPv4 only.
  -6          Use IPv6 only.
  -o 'option' Process the option as if it was read from a configuration file.
  -s          Invoke command (mandatory) as SSH2 subsystem.
  -b addr     Local IP address.

Can you help me with what option do i use?

Meanwhile, it will be a great help if you could let me know how can i supply the script to be executed on machineB with ssh as I am successful in ssh from machineA to machineB. I will troubleshoot ssh not working other way round post this.

There should be no non-interactive passing of passwords to ssh - that's what keys are for. Passing passwords always means to have them unencrypted in your filesystem. You could do it though with expect. But as already said I guess there could be something wrong with exchanged keys.
If your ssh login works, you can issue the command for the script the same way, if the script is deployed on the target machine:

ssh wlsadmin@machineB "/path/toyour/script.sh"
1 Like

Can I also pass arguments to the script as ssh wlsadmin@machineB "/path/toyour/script.sh myfile.txt"

Please correct me if my syntax is incorrect.

Thanks much zaxxon :slight_smile:

Sure, that is absolutely no problem. Just try it out:

ssh wlsadmin@machineB "ls -la /tmp| head"

If your script accepts parameters, go on :slight_smile: