Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > SecureCRT on the Mac

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 09-12-2018, 09:36 AM
rleon rleon is offline
Registered User
 
Join Date: Jan 2008
Posts: 103
script to SSH to host

Code:
def main():
        crt.Screen.Synchronous = False
with open('/Users/rleon16/hosts') as hosts_:
        for host in hosts_:
                crt.Screen.WaitForString("working # ")
                crt.Screen.Send("ssh -oStrictHostKeyChecking=no root@" + host + ps -ef | grep gofer + '\r')
                crt.Screen.WaitForString("word: ")
                crt.Screen.Send("mypass!" + chr(13))

main()
/Users/rleon16/hosts is a list of hostnames.

I am trying to string a ssh command but getting an error.

I just want it to run the command and loop through.

Any ideas?
Reply With Quote
  #2  
Old 09-12-2018, 10:11 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 836
Quote:
Originally Posted by rleon View Post
I am trying to string a ssh command but getting an error.
Usually the most important aspect of knowing how to address an error is knowing details about the error itself.

What is the exact text of the error you're seeing?

I can share things I see wrong with your code, but without any details about the error you're seeing, it's possible that none of them will have anything to do with the error you're seeing.
Here are my in-the-dark-because-you-haven't-said-what-the-error-is observations:
  1. The indentation of your code seems to be off, which python is particularly sensitive about:
    Incorrect indentation:
    Code:
    def main():
            crt.Screen.Synchronous = False
    with open('/Users/rleon16/hosts') as hosts_:
            for host in hosts_:
                    crt.Screen.WaitForString("working # ")
                    crt.Screen.Send("ssh -oStrictHostKeyChecking=no root@" + host + ps -ef | grep gofer + '\r')
                    crt.Screen.WaitForString("word: ")
                    crt.Screen.Send("mypass!" + chr(13))
    
    main()
    Correct Indentation:
    Code:
    def main():
        crt.Screen.Synchronous = False
        with open('/Users/rleon16/hosts') as hosts_:
            for host in hosts_:
                crt.Screen.WaitForString("working # ")
                crt.Screen.Send("ssh -oStrictHostKeyChecking=no root@" + host + ps -ef | grep gofer + '\r')
                crt.Screen.WaitForString("word: ")
                crt.Screen.Send("mypass!" + chr(13))
    
    main()
  2. Also, you're trying to concatenate a string so that your ssh command tells the remote ssh host to run a command there on that secondary host (the 'ps -eaf | grep gofer' portion). However, you failed to enclose that portion in ""s, so the python interpreter is choking on that portion of your statement.

    This would be one way of correcting the mistake:
    Code:
    crt.Screen.Send("ssh -oStrictHostKeyChecking=no root@" + host + " \"ps -ef | grep gofer\"\r")
    Here's another way to correctly concatenate strings, using the format function and substitution params {0} and {1}:
    Code:
    crt.Screen.Send("ssh -oStrictHostKeyChecking=no root@{0} \"{1}\"\r".format(host, "ps -ef | grep gofer"))
    Here's yet another example, which breaks things up into smaller chunks and builds the command using those chunks:
    Code:
    strSSHcmd = "ssh -oStrictHostKeyChecking=no root@" + host
    strRexec = "ps -ef | grep gofer"
    strFullCmd = "{0} \"{1}\"\r".format(strSSHcmd, strRexec)
    crt.Screen.Send(strFullCmd)
  3. If you want the 'grep' performed on the remote server (the 'host' as read in from your file), then you need to enclose the entire command in embedded ""s. Otherwise, the grep will be done on the jump host instead of on the remote host. This problem has been fixed for you in the above examples as well. If you wanted the grep to be conducted in the shell of your jump host server, just remove the \" components from the above examples.

__________________
Jake Devenport
VanDyke Software
Technical Support
support@vandyke.com
http://www.vandyke.com/support
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 08:35 AM.