|
#1
|
|||
|
|||
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() 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? |
#2
|
||||
|
||||
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:
__________________
Jake Devenport VanDyke Software Technical Support YouTube Channel: https://www.youtube.com/vandykesoftware Email: support@vandyke.com Web: https://www.vandyke.com/support |
#3
|
|||
|
|||
Sorry to dig up an old thread but something is wrong with the code.
Code:
# $language = "python" # $interface = "1.0" def main(): crt.Screen.Synchronous = False with open('/Users/rleon/hosts') as hosts_: for host in hosts_: crt.Screen.WaitForString("nfs # ") crt.Screen.Send("ssh -oStrictHostKeyChecking=no root@{0} \"{1}\"\r".format(host, "ps -ef | grep gofer")) crt.Screen.WaitForString("word: ") crt.Screen.Send("mypass" + chr(13)) main() when it runs Code:
# ssh -oStrictHostKeyChecking=no root@myhost "ps -ef | grep gofer" it should be all one line # ssh -oStrictHostKeyChecking=no root@myhost "ps -ef | grep gofer" Last edited by rleon; 04-17-2019 at 12:30 PM. Reason: typo |
#4
|
||||
|
||||
Quote:
If using this host variable to compose your command (which doesn't have any literal \r or \n in the string between the hostname and the rexec portion of the command) results in output as you describe with the rexec portion appearing on a separate line, then it must mean that your host variable has some embedded \r and/or \n character(s) that cause the entire command to be sent to the remote as two separate lines. Consider using the Python-provided strip() method on your host variable to remove any unwanted \r or \n characters, as demonstrated in this example demonstrating a before and after. Then use the same pattern to fix your code. Code:
# $language = "python" # $interface = "1.0" import os def main(): crt.Screen.Synchronous = False with open(os.path.expanduser('~') + '/hosts') as hosts_: for host in hosts_: hostbefore = host # get rid of \r and \n characters host = host.strip('\r\n') # The following messagebox will show the # value of the hostbefore and host variables # but surrounded by <>s. If there are any # embedded \r or \n chars, both <>s won't be # on the same line in the msgbox. crt.Dialog.MessageBox("host(before): <{0}>\r\n\r\nhost(after): <{1}>".format(hostbefore, host)) main() Code:
# $language = "python" # $interface = "1.0" import os def main(): crt.Screen.Synchronous = False with open(os.path.expanduser('~') + '/hosts') as hosts_: for host in (x.strip('\r\n') for x in hosts_): hostbefore = host # The following messagebox will show the # value of the hostbefore and host variables # but surrounded by <>s. If there are any # embedded \r or \n chars, both <>s won't be # on the same line in the msgbox. crt.Dialog.MessageBox("host(before): <{0}>\r\n\r\nhost(after): <{1}>".format(hostbefore, host)) main()
__________________
Jake Devenport VanDyke Software Technical Support YouTube Channel: https://www.youtube.com/vandykesoftware Email: support@vandyke.com Web: https://www.vandyke.com/support Last edited by jdev; 04-17-2019 at 02:38 PM. |
![]() |
Thread Tools | |
Display Modes | |
|
|