變量替換我需要與期望的腳本運行這個CMD:防止預計對遠程shell命令
echo users.1.password=`grep %user% /etc/passwd | awk -F: '{print $2}'` >> /tmp/system.cfg.new
但它的錯誤了,因爲$ 2吧。我該如何解決?我需要該變量只對我發送cmd的設備可見。
這裏是通過腳本上UBNT設備密碼更改的完整的腳本(通過SSH的作品,而不是作爲腳本,因爲2 $):
#!/usr/bin/expect
set timeout 30
#Edit for User
set user user
#Edit for Old Password
set old oldpassword
#Edit for New Password
set new newpassword
#get IP List from iplist.txt
set f [open "/iplist.txt"]
set data [read $f]
close $f
foreach line [split $data \n] {
if {$line eq {}} continue
spawn ssh [email protected]$line
expect {
"assword:" {
send "$old\r"
expect {
"assword:" {
close
continue
}}
expect {
"*" {
send "passwd $user\r"
expect "assword:"
send "$new\r"
expect "assword:"
send "$new\r"
expect "*"
send "grep -v users.1.password= /tmp/system.cfg > /tmp/system.cfg.new\r"
expect "*"
send "echo users.1.password=`grep $user /etc/passwd | awk -F: '{print $2}'` >> /tmp/system.cfg.new\r"
expect "*"
send "cp /tmp/system.cfg.new /tmp/system.cfg\r"
expect "*"
send "save && reboot\r"
close
continue
}}}}
expect {
"*" {
close
continue
}}
expect eof
}
登錄後到SSH,你想在遠程shell執行這個命令? – Dinesh
正確。 %user%實際上被替換爲$ user作爲期望變量,但是我需要打印$ 2才能被遠程shell看到。一直在研究這個腳本,這是唯一的事情(至少我認爲這是直到我得到一個新的錯誤信息),這是阻止我的工作。 –
我加了完整的代碼 –