如何向expect指令碼里面傳遞引數

2021-03-19 18:19:20 字數 3882 閱讀 6359

1樓:兄弟連教育北京總校

比如下面指令碼用來做ssh無密碼登陸,自動輸入確認yes和密碼資訊,使用者名稱,密碼,hostname通過引數來傳遞

ssh.exp

python**

#!/usr/bin/expect

set timeout 10

set username [lindex $argv 0]

set password [lindex $argv 1]

set hostname [lindex $argv 2]

spawn ssh-copy-id -i .ssh/id_rsa.pub $username@$hostname

expect "yes/no"

send "yes\r"

expect "password:"

send "$password\r"

expect eof

執行指令碼./ssh.exp root pasword hostname1

expect接收引數的方式和bash指令碼的方式不太一樣,bash是通過$0 ... $n 這種方式,而expect是通過set 《變數名稱》 [lindex $argv ],例如set username [lindex $argv 0]

如何向expect指令碼里面傳遞引數

2樓:育知同創教育

比如下面指令碼用來做ssh無密碼登陸,自動輸入確認yes和密碼資訊,使用者名稱,密碼,hostname通過引數來傳遞

ssh.exp

python**

#!/usr/bin/expect

set timeout 10

set username [lindex $argv 0]

set password [lindex $argv 1]

set hostname [lindex $argv 2]

spawn ssh-copy-id -i .ssh/id_rsa.pub $username@$hostname

expect "yes/no"

send "yes\r"

expect "password:"

send "$password\r"

expect eof

執行指令碼./ssh.exp root pasword hostname1

expect接收引數的方式和bash指令碼的方式不太一樣,bash是通過$0 ... $n 這種方式,而expect是通過set 《變數名稱》 [lindex $argv ],例如set username [lindex $argv 0]

怎樣用perl指令碼呼叫python指令碼並傳遞引數

3樓:du知道君

舉例:shell的指令碼:t.

sh內容:echo "this is a test shell with arguments"echo "arg1 = $1; arg2 = $2;"執行指令碼./t.

sh zhao結果如下:[noncode@gnode108 knockdown_workflow]$ ./t.

sh zhao1 zhao2this is a test shell with argumentsarg1 = zhao1; arg2 = zhao2;python指令碼:[noncode@gnode108 knockdown_workflow]$ cat t.py #!

/usr/bin/env pythonimport osimport sysdef main():print 'hello world!'if len(sys.

argv) < 2 : print "usage:%s config log" %(sys.

argv[0]) sys.exit(1)arg0 = sys.argv[0]arg1 = sys.

argv[1]print "arg0 = %s; arg1 = %s" % (arg0, arg1) print "test ./t.sh:

"os.system('./t.

sh ' + arg0 + ' ' + arg1)print "test method of replacing: "t = 't.sh'm = 'zhao'n = 'zhao'cmd = ".

/%s %s %s" % (t,m,n)print "t = %s; m = %s; n = %s; cmd = %s" % (t,m,n,cmd)os.system(cmd)if __name__ == '__main__':main()執行指令碼:

python t.py t.sh執行結果:

[noncode@gnode108 knockdown_workflow]$ python t.py t.shhello world!

arg0 = t.py; arg1 = t.shtest .

/t.sh: this is a test shell with argumentsarg1 = t.

py; arg2 = t.sh;test method of replacing: t = t.

sh; m = zhao; n = zhao; cmd = ./t.sh zhao zhaothis is a test shell with argumentsarg1 = zhao; arg2 = zhao;[noncode@gnode108 knockdown_workflow]$ cat t.

sh echo "this is a test shell with arguments"echo "arg1 = $1; arg2 = $2;"[noncode@gnode108 knockdown_workflow]$ ./t.sh zhao1 zhao2this is a test shell with argumentsarg1 = zhao1; arg2 = zhao2;說明:

兩種方法使用python指令碼呼叫shell指令碼:第一種方法:os.

system('./t.sh ' + arg0 + ' ' + arg1)注:.

/t.sh後面有一個空格,不同的第二種方法:t = 't.

sh'm = 'zhao'n = 'zhao'cmd = "./%s %s %s" % (t,m,n)print "t = %s; m = %s; n = %s; cmd = %s" % (t,m,n,cmd)os.system(cmd)注:

在之前把字串聚合到一起。

shell指令碼里怎樣在呼叫python時傳引數

4樓:育知同創教育

python 可以利用 sys.argv 拿到命令列上的 arguments:

$ python test.py 1 2 3

test.py:

import sys

print(sys.argv)

結果:['test.py', '1', '2', '3']

所以你在 build_using_xctool.sh 中可以這樣排程 python:

python /users/gyd/desktop/auto_send_email.py subject msg toaddrs fromaddr **tpaddr password

然後在 auto_send_email.py 中:

import sys # 自己 import sys...if __name__ == '__main__':

sendmail(*sys.argv[1:])

5樓:局詩奇友安

||是重定向嘛,

echo

'asdfadsf'

先是將'asdfadsf'輸出到標準輸出也就是螢幕然後|將echo的輸出轉到

logcat.py

作為logcat.py

的輸入,也就等於是將

'asdfadsf'

作為logcat.py的輸入引數

shell指令碼里的函式怎麼呼叫

在 複用及可維護性方面,函式有著巨大的優勢,因此,把常用功能封裝成函式是一件非常平常的事。shell指令碼中,怎麼定義函式及使用函式 函式定義 func name 函式名 function func name 或 func name 函式名 func name 函式呼叫 func name parm...

我在指令碼里(指令碼完全由lua語言編寫)看見了這個和對稱的

取bai自programing in lua。如上所說長字串du可zhi以這樣 str this is a long string 如果字串中有 怎麼辦呢dao,這樣str a b 0 可以在專 之間加入任意 與同屬樣格式的右方括號匹配,上面str的值是 a b 0 同樣的操作可以適用於註釋。望採納...

在unity3Dc中怎樣呼叫另外c指令碼里面東西

例 第一 個指令碼bai名字為class 1,第du二個指令碼名字zhi為class 2,class 1呼叫class 2 如果class 2沒有綁dao定在版 任何gameobject上,那在class 1裡寫法 權class 2 c2 new class 2 如果class 2繫結在gameob...