1. 第一个使用sudo 来run commands.
-
ssh=paramiko.SSHClient()
-
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
-
ssh.connect("127.0.0.1",username="root",password='password')
-
stdin,stdout,stderr=ssh.exec_command("sudo -k netstat -tnlp",get_pty=True)
-
stdin.write('password\n')
-
stdin.flush()
-
#print(dir(stdout))
-
data=stdout.read(65536).decode()
-
print(data)
2. 远程备份mysql , 先在remote 机器上执行mysqldump 命令,然后再把这个dump.sql 文件用sftp 远程拷贝回来。我觉得很奇怪,mysqldump 生成的文件在/root的家目录,意味着如果用其他用户执行mysqldump,会在其他用户的家目录生成dmp.sql文件。当然来而不往非礼也,我也把本地的一个文件拷贝到远端,为了玩玩sftp的 put 方法。
-
# Execute mysqldump
-
cmd="mysqldump -u{0} -p{1} --all-databases --single-transaction > dump.sql".format(mysql_user,mysql_pw)
-
ssh=paramiko.SSHClient()
-
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
-
ssh.connect("127.0.0.1",username="root",password='password')
-
chan=ssh.get_transport().open_session()
-
chan.exec_command(cmd)
-
timestamp=str(int(time.time()))
-
newname="/tmp/{}".format(timestamp)
-
if chan.recv_exit_status()==0:
-
ftp=ssh.open_sftp()
-
ftp.get("/root/dump.sql",newname)
-
ftp.put(localpath="requirements-dev.txt",remotepath="/tmp/requirements-dev.txt",confirm=True)
-
ftp.close()
3. 这个比较搞,我模拟了wall 的命令执行方法,首先 wall \n,然后输入一行str, 'lol\n', 最后再模拟用户输入的CTRL+D, 就是EOF。
参考了很多 的回复,表示感谢
-
ssh=paramiko.SSHClient()
-
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
-
ssh.connect("127.0.0.1",username="kobe",password='Netf1n1ty')
-
#stdin,stdout,stderr=ssh.exec_command("wall",get_pty=True)
-
stdin,stdout,stderr=ssh.exec_command("wall",get_pty=True)
-
stdin.write('lol\n')
-
#stdin.write(bytearray([0x4]))
-
#stdin.channel.send(bytearray([0x4]))
-
stdin.write(bytes([0x4]))
-
#stdin.channel.send(b'0x4')
-
#stdin.write(b'0x4')
-
#stdin.flush()
-
#print(dir(stdout))
-
print(stdout.channel.recv_exit_status())
-
data=stdout.read(1024).decode()
-
-
print(data)
4. 这个代码我是从别人那里看来的。使用select 来判断是否已经读完了,很不错。select 和Epoll 作为底层的系统调用,是做file descriptor multiplex 的成熟工具了。
虽然epoll 要比select 高效很多,当然我也并不清楚python的select 到底用的是epoll 还是select 系统调用。
原始代码地址在这里:
-
import sys
-
import time
-
import select
-
import paramiko
-
-
host='127.0.0.1'
-
i = 1
-
-
#
-
# Try to connect to the host.
-
# Retry a few times if it fails.
-
#
-
while True:
-
print ("Trying to connect to %s (%i/30)" % (host, i))
-
-
try:
-
ssh = paramiko.SSHClient()
-
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
-
ssh.connect('127.0.0.1',username='root',password='password')
-
print ("Connected to %s" % host)
-
break
-
except paramiko.AuthenticationException:
-
print ("Authentication failed when connecting to %s" % host)
-
sys.exit(1)
-
except:
-
print ("Could not SSH to %s, waiting for it to start" % host)
-
i += 1
-
time.sleep(2)
-
-
# If we could not connect within time limit
-
if i == 3:
-
print ("Could not connect to %s. Giving up" % host)
-
sys.exit(1)
-
-
# Send the command (non-blocking)
-
stdin, stdout, stderr = ssh.exec_command("echo good; sleep 3; echo yes ; sleep 3;ls -al /dev")
-
-
# Wait for the command to terminate
-
while not stdout.channel.exit_status_ready():
-
# Only print data if there is data to read in the channel
-
if stdout.channel.recv_ready():
-
rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
-
if len(rl) > 0:
-
# Print data from stdout
-
print (stdout.channel.recv(65535).decode(),end="")
-
#print (stdout.channel.readlines())
-
#
-
# Disconnect from the host
-
#
-
print ("Command done, closing SSH connection")
-
ssh.close()
阅读(2326) | 评论(0) | 转发(0) |